qudp socket信号不触发_没有绑定,QudpSocket不起作用

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 exchange not working at all when using the code:

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

m_udp.waitForConnected();

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();

Only this code works:

m_udp.bind(4001);

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

m_udp.waitForConnected();

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?

解决方案

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.

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.

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.

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.)

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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值