【问题标题】:How to receive Multicast UDP?如何接收组播 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");
}
}

【问题讨论】:

    标签: objective-c multicast asyncsocket


    【解决方案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");
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多