GCDAsyncUdpSocket和多播发送和接收

在第一种方法中,我创build了基于sampleProject的客户端 – 服务器应用程序,它将一些数据发送到服务器。

Legend: sender address = reciver ip port = reciver port reciver address = null since he is listening port = in my case 55555 

工作代码

跳过错误检查只是为了公共的原因

寄件人

 -(id*)initForSender:(NSString*)address port:(int)port { self = [super init]; if (self) { _port = port; _address = address; tag = 0; _udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; [_udpSocket bindToPort:0 error:&error]; [_udpSocket beginReceiving:&error]; return self; } } -(void)send:(NSData*)data{ [_udpSocket sendData:data toHost:_address port:_port withTimeout:-1 tag:tag]; tag++; } 

接收者/听众

 -(id*)initForReceiver:(NSString*)address port:(int)port { self = [super init]; if (self) { _port = port; _udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; NSError *error = nil; [_udpSocket bindToPort:0 error:&error]; [_udpSocket beginReceiving:&error]; } return self; } - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext { //Do something with receive data } 

组播

但是,我想要客户端将发送到许多接收器。 我知道的formssendre或侦听器应该使用[GCDAsyncUdpSocket joinMulticastGroup:error] ;. 我跑了thover stackoverflow,叔叔谷歌和CococaAsyncSocket(哪里是没有词abou udp),匹配收集的信息,并提出了这个代码。 我很确定,这是行不通的,但我不知道为什么。

 Legend: sender address = sender ip port = sender port in my case 55555 reciver address = sender ip port = sender port in my case 55555 

不工作的代码

 -(id*)initForSender:(NSString*)address port:(int)port { self = [super init]; if (self) { _port = port; _address = address; _udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; NSError *error = nil; [_udpSocket bindToPort:_port error:&error]; [_udpSocket joinMulticastGroup:_address error:&error]; [_udpSocket enableBroadcast:YES error:&error]; } return self; } -(void)send:(NSData*)data{ [_udpSocket sendData:data toHost:_address port:_port withTimeout:-1 tag:tag]; tag++; } 

接收者/听众

 -(id*)initForReceiver:(NSString*)address port:(int)port { self = [super init]; if (self) { _port = port; _address = address; _udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; NSError *error = nil; [_udpSocket bindToPort:_port error:&error]; [_udpSocket joinMulticastGroup:_address error:&error]; [_udpSocket beginReceiving:&error]) } return self; } - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext { //Do something with receive data } 

更新:

结果是,当我使用一些未使用的IP address作为例如@“224.0.1.1”,那么它会发生什么,但它感觉有点奇怪。 我是否正确?

  1. 多播地址是用于多播目的的特殊保留地址。 当一台主机向一个多播地址(组)发送一个数据时,所有join该组的主机都将接收到该数据。

  2. 任何希望从多播组接收数据的主机都需要join该组。 GCDAsyncUdpSocket的三个步骤是:(注意,组地址和组端口组合对于所选的多播组必须是唯一的)

     [_udpSocket bindToPort:_port error:&error]; [_udpSocket joinMulticastGroup:_address error:&error]; [_udpSocket beginReceiving:&error]) 
  3. 对于发件人,您不需要这两行。

     [_udpSocket bindToPort:0 error:&error]; //bindToPort:0 is same as auto-select sender port by default. [_udpSocket beginReceiving:&error]; //if you don't want to received, no need for this. 

为您的私人本地networking使用239.0.0.0-239.255.255.255。 避免224 …你可以从上面的链接阅读更多。