GCDAsyncUdpSocket和组播发送和接收

Bła*_*żej 10 udp ios cocoaasyncsocket

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

Legend:
sender
    address = reciver ip
    port = reciver port
reciver
    address = null since he is listening
    port = in my case 55555
Run Code Online (Sandbox Code Playgroud)

工作代码

跳过错误检查只是出于公共原因而故意

寄件人

-(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++;
}
Run Code Online (Sandbox Code Playgroud)

接收者/听众

-(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
}
Run Code Online (Sandbox Code Playgroud)

组播

但后来我想要发送给许多接收器的客户端.表格我知道sendre或者监听器应该使用[GCDAsyncUdpSocket joinMulticastGroup:error] ;. 我运行了堆栈溢出,叔叔谷歌和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
Run Code Online (Sandbox Code Playgroud)

不工作的代码

-(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++;
}
Run Code Online (Sandbox Code Playgroud)

接收者/听众

-(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
}
Run Code Online (Sandbox Code Playgroud)

更新:

事实证明,当我使用一些不使用的IP时address,例如@"224.0.1.1",它就会震动,但它感觉很奇怪.我做得对吗?

use*_*234 11

  1. 多播地址是用于多播目的的特殊保留地址.当主机将数据发送到多播地址(组)时,已加入该组的所有主机都将接收数据.

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

    [_udpSocket bindToPort:_port error:&error];
    [_udpSocket joinMulticastGroup:_address error:&error];
    [_udpSocket beginReceiving:&error])
    
    Run Code Online (Sandbox Code Playgroud)
  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.
    
    Run Code Online (Sandbox Code Playgroud)

将239.0.0.0-239.255.255.255用于您的专用本地网络.避免224 ...您可以从上面的链接中阅读更多内容.