qt - 如何通过 QT 中的特定网络接口(interface)写入数据报?

标签 qt sockets network-programming udp

我在 Linux 上使用 QT 4.8。

我想编写 UDP 数据报并从特定的网络接口(interface)发送。

我有2个接口(interface):

  • WLAN:IP 192.168.1.77 和 MAC 地址
  • Eth:IP 192.168.1.80 和另一个 MAC 地址

  • 当两者都启用时,如何选择其中一个网络接口(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/

    相关文章:

    Java 多客户端服务器套接字。管道破裂检测

    c++ - 客户端/服务器 C++ 之间的通信问题

    java - 在 Socket 尝试连接之前设置超时

    .net - 在 VB .NET 中查找并启动 VPN 连接

    c++ - RST 数据包是否会被发送到应用程序而不考虑其序列号?

    c++ - 如何从屏幕上实际删除 QGraphicsItem?

    c++ - 减小 Qt GUI 应用程序的大小

    c++ - Qt 嵌套模型 View

    qt - QML ListView : how to copy selected item to clipboard?

    java - 为什么 NetworkInterface 的方法会抛出 SocketException