没有绑定,QudpSocket无法正常工作 [英] QUdpSocket not working without bind

查看:451
本文介绍了没有绑定,QudpSocket无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过UDP与某些设备进行通信.问题是 QUdpSocket 在没有特殊情况下根本无法工作 bind()的情况.我使用 connectToHost()方法

I have to communicate with some device over UDP. The problem is that QUdpSocket doesn't not work at all without special case of bind(). I use the connectToHost() method for access to read()/write() functions.

使用代码时,UDP交换根本不起作用:

UDP exchange not working at all when using the code:

m_udp.connectToHost(QHostAddress("192.168.100.15"), 4001);    
m_udp.waitForConnected();

我没有收到任何字节. Wireshark中的消息:

The I don't receive any bytes. Message in Wireshark:

下面的代码也不起作用:

The below code doesn't work too:

m_udp.bind(QHostAddress("192.168.100.15"), 4001);
m_udp.connectToHost(QHostAddress("192.168.100.15"), 4001);    
m_udp.waitForConnected();

仅此代码有效:

m_udp.bind(4001);
m_udp.connectToHost(QHostAddress("192.168.100.15"), 4001);    
m_udp.waitForConnected();

但是该代码仅在Qt 5.6.2中有效,而在Qt 5.4.2中无效. 这是我尝试接收的方式:

But the code works only in Qt 5.6.2 and not work in Qt 5.4.2. Here is how I try to receive:

dev->waitForReadyRead(500);
QByteArray ba = dev->readAll();

为什么行为如此奇怪?怎么能理解这一点?

Why the behaviour is so strange? How can one understand this?

推荐答案

您尚未显示另一侧的代码,但是显然,从所示的wireshark图像来看,远程对等体希望您的套接字绑定到端口4001

You haven't shown the code for the other side, but apparently from the wireshark image shown, the remote peer is expecting your socket to be bound to port 4001.

第一个代码段尝试在不指定本地端口的情况下连接到(RemoteAddress,4001)(因此内核分配了任意端口号(根据wireshark,为64875),但是如上所述,另一端是 发送回端口4001.因此,您无法在端口64875上接收它.

The first code snippet attempts to connect to the (RemoteAddress, 4001) without specifying a local port (hence the kernel assigns an arbitrary port number (64875 according to wireshark) but as mentioned above, the other side is expecting to send back to port 4001. Hence you can't receive it on port 64875.

在UDP中,没有诸如实际连接之类的东西.当您使用connectToHost时,您实际上只在指定对端的IP地址和端口号.确保远程端发回正确的端口是应用程序的责任.这可以通过(a)总是发送到特定端口(显然是在这里完成)来完成,或者(b)另一端可以查看实际从中接收数据报的地址/端口并发送回该地址/端口;换句话说,远程端可以确定它接收到的数据报是从端口64875发送的,并将其答复定位为该端口.

In UDP, there is no such thing as an actual connection. When you use connectToHost you're really only specifying the remote side's IP address and port number. It is the application's responsibility to make sure the remote side sends back to the correct port. That can be done by (a) always sending to a particular port (as is apparently being done here), or (b) the other side can look at the address/port from which the datagram is actually received and send back to that; in other words, the remote side could determine that the datagram it received had been sent from port 64875 and target its reply to that port.

在第二次尝试中,您试图将本地终结点绑定到另一台机器上的地址,这根本没有任何意义. (我想如果您将绑定更改为本地计算机的IP地址m_udp.bind(QHostAddress("192.168.100.3"), 4001);,此代码段将起作用.)

In the second try, you're attempting to bind your local endpoint to an address on the other machine which simply doesn't make any sense. (I'm guessing this snippet would work if you changed the bind to m_udp.bind(QHostAddress("192.168.100.3"), 4001); -- your local machine's IP address.)

只有第三个代码段有效,因为在这里您绑定到本地端口4001而不指定IP地址,然后还发送到远程计算机上的端口4001.如果仅在bind中指定端口号,则IP地址将保留为通配符",因此,应该在计算机的任何IP地址上将发送到该端口号的数据包发送出去.

Only the third snippet works because here you're binding to local port 4001 without specifying an IP address, then also sending to port 4001 on the remote machine. If you specify only the port number in the bind, then the IP address will be left as "wildcard" and hence you should get packets sent to that port number on any of your machine's IP addresses.

(不知道为什么您会在早期版本的Qt中看到不同的行为.)

(No idea why you would be seeing different behavior in an earlier version of Qt.)

这篇关于没有绑定,QudpSocket无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆