在 QT 中尝试使用 UDP 从特定 IP 地址接收数据
In QT Trying to receive data from specific IP address using UDP
我正在尝试在 QT 中使用 UDP 协议从特定 IP 地址(如 166.0.0.245)接收数据。我的网络中有多个 udp 连接。
但问题是我从任何 IP 地址而不是特定 IP 地址接收。
下面我附上我的代码,我在其中设置我的接收者的 IP 地址,即 166.0.0.34,我的发件人 IP 地址是 166.0.0.245 -> 我在哪里专门设置这个 IP 地址,以便我只能接收该 ip 地址并丢弃我网络中的其余 ip 地址。
Plz 有人可以建议我在哪里专门设置我的 ip 地址在接收端只接受一个具有特定 ip 地址和端口的发件人。
提前致谢
NetBroadcasterDlg::NetBroadcasterDlg(QWidget *parent) : //Main Function
QWidget(parent),
ui(new Ui::NetBroadcasterDlg)
{
// ethernet initialisation and binding wini
udpSocket = new QUdpSocket(this);
udpSocket_send = new QUdpSocket(this);
udpSocket->bind(QHostAddress("166.0.0.34"), 1100, QUdpSocket::ShareAddress);
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(processPendingDatagrams()));
...
}
void NetBroadcasterDlg::processPendingDatagrams()
{
// Read data from ethernet
while (udpSocket->hasPendingDatagrams()) {
datagram.resize(int(udpSocket->pendingDatagramSize()));
udpSocket->readDatagram(datagram.data(), datagram.size());
// Sending data to the Target
udpSocket_send->writeDatagram(datagram.data(), datagram.size(),
QHostAddress("166.168.1.20"), 2500); //target address and port
...
}
实际上,一个打开的 UDP 套接字旨在接收来自任何 IP 地址的任何内容,该地址能够在您的网络中打开该 UDP 套接字。
您可以阻止其他 IP 地址连接到此特定接口或
QHostAddress fromAddress;
udpSocket->readDatagram(datagram.data(), datagram.size(),&fromAddress);
if(fromAddress.isEqual(QHostAddress("166.0.0.245")){
// do whatever with your filtered data
}
我正在尝试在 QT 中使用 UDP 协议从特定 IP 地址(如 166.0.0.245)接收数据。我的网络中有多个 udp 连接。
但问题是我从任何 IP 地址而不是特定 IP 地址接收。
下面我附上我的代码,我在其中设置我的接收者的 IP 地址,即 166.0.0.34,我的发件人 IP 地址是 166.0.0.245 -> 我在哪里专门设置这个 IP 地址,以便我只能接收该 ip 地址并丢弃我网络中的其余 ip 地址。 Plz 有人可以建议我在哪里专门设置我的 ip 地址在接收端只接受一个具有特定 ip 地址和端口的发件人。
提前致谢
NetBroadcasterDlg::NetBroadcasterDlg(QWidget *parent) : //Main Function
QWidget(parent),
ui(new Ui::NetBroadcasterDlg)
{
// ethernet initialisation and binding wini
udpSocket = new QUdpSocket(this);
udpSocket_send = new QUdpSocket(this);
udpSocket->bind(QHostAddress("166.0.0.34"), 1100, QUdpSocket::ShareAddress);
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(processPendingDatagrams()));
...
}
void NetBroadcasterDlg::processPendingDatagrams()
{
// Read data from ethernet
while (udpSocket->hasPendingDatagrams()) {
datagram.resize(int(udpSocket->pendingDatagramSize()));
udpSocket->readDatagram(datagram.data(), datagram.size());
// Sending data to the Target
udpSocket_send->writeDatagram(datagram.data(), datagram.size(),
QHostAddress("166.168.1.20"), 2500); //target address and port
...
}
实际上,一个打开的 UDP 套接字旨在接收来自任何 IP 地址的任何内容,该地址能够在您的网络中打开该 UDP 套接字。 您可以阻止其他 IP 地址连接到此特定接口或
QHostAddress fromAddress;
udpSocket->readDatagram(datagram.data(), datagram.size(),&fromAddress);
if(fromAddress.isEqual(QHostAddress("166.0.0.245")){
// do whatever with your filtered data
}