GCDAsyncUdpSocket和组播发送和接收 [英] GCDAsyncUdpSocket and multicast sending and receiving

查看:331
本文介绍了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



< h2>工作代码

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

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
}



< hr>

多播



但是我想要发送给许多接收器的客户端。表单我知道sendre或listener应该使用[GCDAsyncUdpSocket joinMulticastGroup:error] ;.我跑了堆栈overflow,叔叔谷歌和CococaAsyncSocket(哪里没有字abou 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



不工作代码



Not working code

-(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
}



< h2> UPDATE:

当我使用一些不使用的IP作为地址时,例如@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和组播发送和接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆