GCDAsyncUdpSocket 和多播发送和接收

GCDAsyncUdpSocket and multicast sending and receiving(GCDAsyncUdpSocket 和多播发送和接收)

本文介绍了GCDAsyncUdpSocket 和多播发送和接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In first approach I create client-server app ,based on sampleProject , which send some data to server.

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

工作代码

仅出于公共原因故意跳过错误检查

Working code

skipping error checking is intentional only for public reasons

发件人

-(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 的词)运行,匹配收集到的信息并想出了这段代码.我完全确定,这不起作用,但我不知道为什么.


Multicast

But then I want client which will send to many receivers. Form I know sendre or listener should use [GCDAsyncUdpSocket joinMulticastGroup:error];. I ran throu stackoverflow, uncle google and CococaAsyncSocket (where is no word abou udp), match gathered pieces of information and came up with this code. I'm perfectly sure, that is not working but I don't have a clue why.

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" 时它会工作,但感觉有点奇怪.我做得对吗?

UPDATE:

It's turn out that when I use some unuse IP as address for example @"224.0.1.1" then it wroks, but it feels weird a bit. Am I doing it correct?

推荐答案

  1. 多播地址是为多播目的.当主机向组播地址(组)发送数据时,所有加入该组的主机都会收到该数据.

  1. Multicast addresses are special reserved addresses for multicasting purpose. When a host send a data to a multicast address (group), all hosts that have joined that group will receive the data.

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

Any host that wishes to receive data from a multicast group needs to join that group. The three steps for GCDAsyncUdpSocket are: (note that group address and group port combination must be unique for that selected multicast group)

[_udpSocket bindToPort:_port error:&error];
[_udpSocket joinMulticastGroup:_address error:&error];
[_udpSocket beginReceiving:&error])

  • 对于发件人,你不需要这两行.

  • For a sender, you don't need these two lines as you have.

    [_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... 您可以从上面的链接中了解更多信息.

    Use the 239.0.0.0-239.255.255.255 for your private local network. Avoid 224... You can read more from the link above.

    这篇关于GCDAsyncUdpSocket 和多播发送和接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:GCDAsyncUdpSocket 和多播发送和接收

    基础教程推荐