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

有些话、适合烂在心里 提交于 2019-12-04 20:42:43

问题


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 to change my code and send the data to the broadcast address 255.255.255.255.

Here is my code :

NSString *bchost = @"255.255.255.255";
NSString *host = @"10.1.0.1";
int udpPort = 6001;

AsyncUdpSocket *udpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
[udpSocket bindToPort:udpPort error:nil];
[udpSocket enableBroadcast:YES error:nil]; 
NSError *error = nil;
if ([udpSocket connectToHost:bchost onPort:udpPort error:&error])
{
[udpSocket receiveWithTimeout:10 tag:0];
[self sendToUDPServer:@"HELLO"];
}

So, the problem is that it works with "host" but not with "bchost". On both cases I see on the server side that the data is received and the answer is sent to the address of the sender (which should be the iOS device) but on the device I do not get the data when I send it to bchost.

Any idea what I am missing ?


回答1:


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.




回答2:


You are 'connecting' to the host. Per the Unix socket FAQ, this means you'll only get UDP packets back that have a source IP address of 255.255.255.255. Connecting establishes a 1-to-1 relationship for UDP, such that received packets whose source addresses differ from the connected address will be filtered.

If you don't connect (you'll have to change your send line to target the broadcast address), it should work. You'll send toHost:bcHost -- and then your receive should get all packets destined for its port.




回答3:


Based on Arthur's answer, here's the working code. I'm wondering if receive should start before the send, just to make sure we don't miss a very fast reply before receive is ready, but so far it doesn't seem necessary in my situation. Also, reference this post on how to create receiving methods.

AsyncUdpSocket *udpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
[udpSocket bindToPort:1234 error:nil ];
[udpSocket enableBroadcast:YES error:nil];

NSData* data=[messageToSend dataUsingEncoding:NSUTF8StringEncoding];

if ([udpSocket sendData:data toHost:@"255.255.255.255" port:4321 withTimeout:3 tag:0])
{
    //2 second timeout. onUdpSocket methods will provide results
    [udpSocket receiveWithTimeout:2 tag:0];
}



回答4:


I could be completely crazy, but it seems like this is a standing issue with AsyncUdpSocket on iOS.

There's several error reports similar and even identical to yours on their Google Code page; people have complained that they are unable to receive Udp packets after broadcasting, and in some cases, even at all.

http://code.google.com/p/cocoaasyncsocket/issues/list?can=2&q=AsyncUdpSocket+receive



来源:https://stackoverflow.com/questions/5790149/little-problem-with-asyncudpsocket-receiving-data-after-connecting-to-broadcast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!