如何接收组播 UDP?
Posted
技术标签:
【中文标题】如何接收组播 UDP?【英文标题】:How to receive Multicast UDP? 【发布时间】:2012-11-19 17:35:19 【问题描述】:我正在使用 GCDAsyncUdpSocket,我可以发送多播或普通 UDP 数据包。我可以正常接收数据包,但无法从其他 ios 设备接收多播数据包。
接收我使用:
- (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");
【问题讨论】:
【参考方案1】:确保正确设置了多播套接字。这是我在多播项目中所做的:
- (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?的主要内容,如果未能解决你的问题,请参考以下文章