我在 Linux 上使用 QT 4.8。
我想编写 UDP 数据报并从特定的网络接口(interface)发送。
我有2个接口(interface):
当两者都启用时,如何选择其中一个网络接口(interface)并从那里写入数据报?
最佳答案
简短的回答是,bind to *one of the addresses of the eth interface .
Qt 有一个非常干净的 library for this .但是当我需要弄脏的时候,我会使用 ACE C++ library 之类的东西。 .
无论如何,这里有一些东西可以帮助您入门,但是您应该在 QtCreator 或 google 中查看更具体的示例。 :
QUdpSocket socket;
// I am using the eth interface that's associated
// with IP 192.168.1.77
//
// Note that I'm using a "random" (ephemeral) port by passing 0
if(socket.bind(QHostAddress("192.168.1.77"), 0))
{
// Send it out to some IP (192.168.1.1) and port (45354).
qint64 bytesSent = socket.writeDatagram(QByteArray("Hello World!"),
QHostAddress("192.168.1.1"),
45354);
// ... etc ...
}
关于qt - 如何通过 QT 中的特定网络接口(interface)写入数据报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17235639/