2013-03-14 254 views
-2

我試圖爲iOS創建一個應用程序,在那裏我可以連接到UDP服務器並從它接收數據。我的情況:連接到UDP服務器

  1. 我在Windows機器上有一個UDP服務器(IP = 192.168.1.6)。
  2. 我有iPad(IP = 192.168.1.5);
  3. UDP服務器發送到虛擬地址(IP = 239.254.1.2)和端口(7125)消息,如「你好!!!!!我在這裏!!!!」

我需要爲iOS應用程序添加一些邏輯,在那裏我可以連接到虛擬IP地址(IP = 239.254.1.2)和端口(7125)並接收消息「你好!!!!!我在這裏!!!!」來自UDP服務器。

有沒有人有任何建議?

UPDATE1:

對於UDP連接,我使用GCDAsyncUdpSocket

這裏我的代碼:

@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 < 0 || port > 65535) { 
      portField.text = @""; 
      port = 0; 
     } 

     NSError *error = nil; 

     if (![udpSocket bindToPort:7125 error:&error]) { 
      [self logError:FORMAT(@"Error starting server (bind): %@", error)]; 
      return; 
     } 

     if (![udpSocket joinMulticastGroup:@"239.254.1.2" error:&error]) { 
      [self logError:FORMAT(@"Error join Multicast Group: %@", error)]; 
      return; 
     } 

     if (![udpSocket beginReceiving:&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:&host port:&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]; 
} 

當我按日誌中的 「開始」 按鈕,我看到消息「Udp Echo服務器端口7125啓動」,但代理方法

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data 
                fromAddress:(NSData *)address 
              withFilterContext:(id)filterContext 

從未解僱,應用程序不接收來自虛擬IP地址的任何消息。 你能幫我解決這個問題嗎?

謝謝。

回答

1

239.254.1.2是UDP服務器正在向其發送數據包的多播地址。在該地址上收聽的任何人都將收到數據包。所以:

  • 創建UDP套接字
  • 您的插座端口綁定7125
  • 加入多播組239.254.1.2
  • 您的應用程序將開始接收UDP數據包

也許應該只要提到UDP是無連接協議,即不能連接到UDP服務器。

+0

你好,RichardBrock 謝謝你的回覆。 我用我的測試源代碼更新我的問題。你能回顧一下嗎? 謝謝。 – kroumvud 2013-03-15 00:52:55

0

RichardBrock是正確的,來自UPDATE1的示例代碼完美地工作。問題是我的家庭網絡,當我嘗試在工作網絡中的這段代碼一切正常!

謝謝你