title: GCDAsyncUdpSocket and multicast sending and receiving
body: <p>In first approach I create client-server app ,based on <a href="http://github.com/robbiehanson/CocoaAsyncSocket/tree/master/GCD/Xcode" rel="noreferrer">sampleProject</a> , which send some data to server. </p>
  
  <pre><code>Legend:
  sender
      address = reciver ip
      port = reciver port
  reciver
      address = null since he is listening
      port = in my case 55555
  </code></pre>
  
  <h2>Working code</h2>
  
  <p>skipping error checking is intentional only for public reasons</p>
  
  <p>Sender</p>
  
  <pre><code>-(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:&amp;error];
          [_udpSocket beginReceiving:&amp;error];
  
        return self;
      }
  }
  
  -(void)send:(NSData*)data{
       [_udpSocket sendData:data toHost:_address port:_port withTimeout:-1 tag:tag];
       tag++;
  }
  </code></pre>
  
  <p>Receiver / Listener</p>
  
  <pre><code>-(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:&amp;error];
         [_udpSocket beginReceiving:&amp;error];
      }
      return self;
  }
  
  - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
        fromAddress:(NSData *)address
  withFilterContext:(id)filterContext
  {
      //Do something with receive data
  }
  </code></pre>
  
  <hr>
  
  <h2>Multicast</h2>
  
  <p>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.  </p>
  
  <pre><code>Legend:
  sender
      address = sender ip
      port = sender port in my case 55555
  reciver
      address = sender ip
      port = sender port in my case 55555
  </code></pre>
  
  <h2>Not working code</h2>
  
  <pre><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:&amp;error];
          [_udpSocket joinMulticastGroup:_address error:&amp;error];
          [_udpSocket enableBroadcast:YES error:&amp;error];
  
      }
      return self;
  }
  
  -(void)send:(NSData*)data{
       [_udpSocket sendData:data toHost:_address port:_port withTimeout:-1 tag:tag];
       tag++;
  }
  </code></pre>
  
  <p>Receiver / Listener</p>
  
  <pre><code>-(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:&amp;error];
          [_udpSocket joinMulticastGroup:_address error:&amp;error];
          [_udpSocket beginReceiving:&amp;error])
  
      }
      return self;
  }
  
  - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
        fromAddress:(NSData *)address
  withFilterContext:(id)filterContext
  {
     //Do something with receive data
  }
  </code></pre>
  
  <h2>UPDATE:</h2>
  
  <p>It's turn out that when I use some unuse IP as <code>address</code> for example @"224.0.1.1" then it wroks, but it feels weird a bit. Am I doing it correct?</p>