2013-06-21 36 views
4

我在Linux上使用QT 4.8。如何通過QT中的特定網絡接口寫入數據報?

我想編寫UDP數據報並從特定的網絡接口發送它。

我有2個接口:

  1. WLAN:IP 192.168.1.77和MAC地址
  2. 的Eth:IP 192.168.1.80和另一個MAC地址

我如何可以選擇其中之一當兩者都啓用時,網絡接口和寫入數據報?

回答

4

簡短的回答是,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 ... 
} 
相關問題