Little problem with AsyncUdpSocket receiving data after connecting to broadcast instead of server

前端 未结 4 2045
别那么骄傲
别那么骄傲 2021-02-15 23:12

I have a problem with AsyncUdpSocket.

I used to connect to a server, send some data and get some response. Now since I do not know the actual address of the server I had

4条回答
  •  执念已碎
    2021-02-15 23:29

    Ok, unfortunately all reply's do not work for me but I found the solution, finally ;)

    NSString *bcHost = @"255.255.255.255";
    NSString *anyHost = @"0.0.0.0";
    
    int UDP_SOCKET_PORT =         6001;
    int DISCOVERY_PORT  =       6003;
    
    udpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
    [udpSocket bindToAddress:anyHost port:DISCOVERY_PORT error:nil];  
    [udpSocket enableBroadcast:YES error:nil]; 
    [udpSocket receiveWithTimeout:10 tag:0];
    [udpSocket sendData:[@"Hello" dataUsingEncoding:NSASCIIStringEncoding] toHost:bcHost port:UDP_SOCKET_PORT withTimeout:-1 tag:0];
    

    If there is an server behind it, it will trigger a response and this will also allow to get the ip from the server for further processing.

提交回复
热议问题