title: Connect to UDP server
body: <p>I'm try to create an application for iOS, where I can connect to a UDP Server and receive data from it. My situation:</p>
  
  <ol>
  <li>I have a UDP server on Windows machine (IP = 192.168.1.6).</li>
  <li>I have iPad (IP = 192.168.1.5);</li>
  <li>UDP server sends to virtual address (IP = 239.254.1.2) and port (7125) messages, something like "HELLO!!!!! I'm here!!!!"</li>
  </ol>
  
  <p>I need add some logic for the iOS app, where I can connect to virtual IP address (IP = 239.254.1.2) and port (7125) and receive messages "HELLO!!!!! I'm here!!!!" from UDP server.</p>
  
  <p>Does anyone have any suggestions?</p>
  
  <p><strong>UPDATE1:</strong></p>
  
  <p>For UDP connection I'm use <strong>GCDAsyncUdpSocket</strong></p>
  
  <p>Here my code:</p>
  
  <pre><code>@interface ViewController () {
      GCDAsyncUdpSocket *udpSocket;
  }
  
  - (void)viewDidLoad {
      [super viewDidLoad];
      udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
  }
  
  - (IBAction)startStop:(id)sender {
      if (isRunning) {
          // STOP udp echo server
          [udpSocket close];
  
          [self logInfo:@"Stopped Udp Echo server"];
          isRunning = false;
  
          [portField setEnabled:YES];
          [startStopButton setTitle:@"Start" forState:UIControlStateNormal];
      } else {
          // START udp echo server
  
          int port = [portField.text intValue];
          if (port &lt; 0 || port &gt; 65535) {
              portField.text = @"";
              port = 0;
          }
  
          NSError *error = nil;
  
          if (![udpSocket bindToPort:7125 error:&amp;error]) {
              [self logError:FORMAT(@"Error starting server (bind): %@", error)];
              return;
          }
  
          if (![udpSocket joinMulticastGroup:@"239.254.1.2" error:&amp;error]) {
              [self logError:FORMAT(@"Error join Multicast Group: %@", error)];
              return;
          }
  
          if (![udpSocket beginReceiving:&amp;error])
          {
              [udpSocket close];
  
              [self logError:FORMAT(@"Error starting server (recv): %@", error)];
              return;
          }
  
          [self logInfo:FORMAT(@"Udp Echo server started on port %hu", [udpSocket localPort])];
          isRunning = YES;
  
          [portField setEnabled:NO];
          [startStopButton setTitle:@"Stop" forState:UIControlStateNormal];
      }
  }
  
  - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
                                                 fromAddress:(NSData *)address
                                           withFilterContext:(id)filterContext
  {
      if (!isRunning) return;
  
      NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
      if (msg)
      {
          /* If you want to get a display friendly version of the IPv4 or IPv6 address, you could do this:
  
          NSString *host = nil;
          uint16_t port = 0;
          [GCDAsyncUdpSocket getHost:&amp;host port:&amp;port fromAddress:address];
  
          */
  
          [self logMessage:msg];
      }
      else
      {
          [self logError:@"Error converting received data into UTF-8 String"];
      }
  
      [udpSocket sendData:data toAddress:address withTimeout:-1 tag:0];
  }
  </code></pre>
  
  <p>When I press "Start" button in the log I see message "Udp Echo server started on port 7125", but delegate method </p>
  
  <pre><code>- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
                                                     fromAddress:(NSData *)address
                                               withFilterContext:(id)filterContext
  </code></pre>
  
  <p>never fired and application dont receive any messages from virtual IP address.
  Can you help me with this issue?</p>
  
  <p>Thank you.</p>