10

在第一种方法中,我创建了基于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
}

组播

但后来我想要将发送给许多接收者的客户端。我知道 sendre 或 listener 应该使用 [GCDAsyncUdpSocket joinMulticastGroup:error]; 的表单。我通过 stackoverflow、google 叔叔和 CococaAsyncSocket(没有关于 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
}

更新:

事实证明,当我使用一些未使用的 IPaddress例如@“224.0.1.1”时,它会工作,但感觉有点奇怪。我做对了吗?

4

1 回答 1

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

  2. 任何希望从多播组接收数据的主机都需要加入该组。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.
    

将 239.0.0.0-239.255.255.255 用于您的专用本地网络。避免 224... 您可以从上面的链接中了解更多信息。

于 2013-08-10T18:42:40.397 回答