连接广播而不是服务器后AsyncUdpSocket接收数据的小问题
我的 AsyncUdpSocket 有问题。
我曾经连接到服务器,发送一些数据并获得一些响应。现在,由于我不知道服务器的实际地址,我不得不更改代码并将数据发送到广播地址 255.255.255.255。
这是我的代码:
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"];
}
所以,问题是它适用于“host”,但不适用于“bchost”。在这两种情况下,我在服务器端看到数据已收到,并且答案已发送到发送者的地址(应该是 iOS 设备),但在设备上,当我将数据发送到 bchost 时,我没有收到数据。
知道我缺少什么吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在“连接”到主机。根据 Unix 套接字常见问题解答,这意味着您只会收到具有源IP地址255.255.255.255。连接为 UDP 建立一对一的关系,这样接收到的源地址与连接地址不同的数据包将被过滤。
如果您没有连接(您必须更改发送线路以定位广播地址),它应该可以工作。您将发送
toHost:bcHost
——然后您的接收应该收到发往其端口的所有数据包。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.根据亚瑟的回答,这是工作代码。我想知道接收是否应该在发送之前开始,只是为了确保我们不会在接收准备好之前错过非常快的回复,但到目前为止,在我的情况下似乎没有必要。另外,请参考这篇文章了解如何创建接收方法。
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.
我可能完全疯了,但这似乎是 iOS 上 AsyncUdpSocket 的一个长期问题。
在他们的 Google 代码页上有几个与您的错误报告相似甚至相同的错误报告;人们抱怨说,他们在广播后无法接收 Udp 数据包,在某些情况下,甚至根本无法接收。
http://code.google.com/p /cocoaasyncsocket/issues/list?can=2&q=AsyncUdpSocket+receive
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
好吧,不幸的是所有回复都对我不起作用,但我终于找到了解决方案;)
如果它后面有服务器,它将触发响应,这也将允许从服务器获取 IP 以便进一步处理。
Ok, unfortunately all reply's do not work for me but I found the solution, finally ;)
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.