连接到UDP服务器

时间:2013-03-14 23:30:34

标签: ios sockets udp

我正在尝试为iOS创建一个应用程序,在那里我可以连接到UDP服务器并从中接收数据。我的情况:

  1. 我在Windows机器上有一台UDP服务器(IP = 192.168.1.6)。
  2. 我有iPad(IP = 192.168.1.5);
  3. UDP服务器发送到虚拟地址(IP = 239.254.1.2)和端口(7125)消息,类似“HELLO !!!!!我在这里!!!!”
  4. 我需要为iOS应用添加一些逻辑,我可以连接到虚拟IP地址(IP = 239.254.1.2)和端口(7125)并接收消息“HELLO !!!!!我在这里!!! !”来自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 server on port 7125”,但委托方法

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

    永远不会被解雇,应用程序也不会从虚拟IP地址收到任何消息。 你能帮我解决这个问题吗?

    谢谢。

2 个答案:

答案 0 :(得分:1)

239.254.1.2是UDP服务器向其发送数据包的组播地址。收听该地址的任何人都将收到这些数据包。所以:

  • 创建UDP套接字
  • 将您的套接字绑定到端口7125
  • 加入组播组239.254.1.2
  • 您的应用将开始接收udp数据包

可能只要提到UDP是无连接协议,即你无法连接到UDP服务器。

答案 1 :(得分:0)

RichardBrock是对的,UPDATE1的示例代码完美无缺。问题在于我的家庭网络,当我在工作网络中尝试此代码时,一切正常!

谢谢你