如何接收组播UDP? [英] How to receive Multicast UDP?
本文介绍了如何接收组播UDP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用GCDAsyncUdpSocket,并且可以发送多播或常规UDP数据包.我可以正常接收数据包,但不能从其他iOS设备接收多播数据包.
I'm using GCDAsyncUdpSocket and i can send either Multicast or normal UDP packets. I'm receiving normal packets with no problem but I cant receive Multicast packets from another iOS device.
要接收,我使用:
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress: (NSData *)address withFilterContext:(id)filterContext
{ NSString *msg = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSString *host = nil;
uint16_t port = 0;
[GCDAsyncUdpSocket getHost:&host port:&port fromAddress:address];
if (msg)
{
NSLog(@"Message = %@, Adress = %@ %i",msg,host,port);
}
else
{
NSLog(@"Error converting received data into UTF-8 String");
}
}
推荐答案
确保已正确设置套接字以进行多播.这是我在多播项目中正在做的事情:
Make sure the socket is properly set up for multicast. Here's what I'm doing in my multicast project:
- (void)setupSocket
{
udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
if (![udpSocket bindToPort:5555 error:&error])
{
NSLog(@"Error binding to port: %@", error);
return;
}
if(![udpSocket joinMulticastGroup:@"226.1.1.1" error:&error]){
NSLog(@"Error connecting to multicast group: %@", error);
return;
}
if (![udpSocket beginReceiving:&error])
{
NSLog(@"Error receiving: %@", error);
return;
}
NSLog(@"Socket Ready");
}
这篇关于如何接收组播UDP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文