连接广播而不是服务器后AsyncUdpSocket接收数据的小问题

发布于 2024-11-03 12:57:32 字数 719 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抚笙 2024-11-10 12:57:33

您正在“连接”到主机。根据 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.

筱果果 2024-11-10 12:57:33

根据亚瑟的回答,这是工作代码。我想知道接收是否应该在发送之前开始,只是为了确保我们不会在接收准备好之前错过非常快的回复,但到目前为止,在我的情况下似乎没有必要。另外,请参考这篇文章了解如何创建接收方法。

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];
}

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];
}
﹏雨一样淡蓝的深情 2024-11-10 12:57:33

我可能完全疯了,但这似乎是 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

爱*していゐ 2024-11-10 12:57:32

好吧,不幸的是所有回复都对我不起作用,但我终于找到了解决方案;)

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];

如果它后面有服务器,它将触发响应,这也将允许从服务器获取 IP 以便进一步处理。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文