需求是:本地IP 跟 下位机设备处于同一个IP段(192.168.1.1),现在需要发送
数据包给 指定IP(
239.255.255.250) 端口(8002)
显示数据包能成功到达下位机设备,但是接收这边 接收不了 下位机发回来的数据包,通过截图可以看到 下位机是有发回反馈信息的,也是反馈到了指定IP(
239.255.255.250) 端口(8002),请教下,应该怎么来接收反馈回来的数据呢?截图中 192.168.1.128是本机IP 192.168.1.7是下位机设备。
部分代码:
localHostAddr = new QHostAddress("192.168.1.128");
udpSocket->bind(*localHostAddr, 8002, QUdpSocket::
ShareAddress | QUdpSocket::
ReuseAddressHint); //绑定
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(receive()), Qt::DirectConnection); //连接
/*发送数据包*///发送给指定IP 指定端口
udpSocket->writeDatagram(data, data.length(), *remoteHostAddr, 8002);
/*接收函数*/
void MainWindow::receive()
{
while (udpSocket->hasPendingDatagrams())
{
QByteArray Redata;
Redata.resize(udpSocket->pendingDatagramSize());
/*读取数据*/ //读取发送端IP 跟 端口
udpSocket->readDatagram(Redata.data(), Redata.size(), remoteHostAddr, port_p);
QMessageBox::information(this, "information", Redata);
QMessageBox::information(this, "information", remoteHostAddr->toString());
}
}
测试发现,接收不到下位机传回来的数据包,
后来采用广播方式发送也能到达下位机,但是反馈回来的是 我发出去的数据包。
/*广播方式发送*/
udpSocket->writeDatagram(data, data.length(), QHostAddress::Broadcast, 8002);
请求帮助,谢谢!