Hi all,

How would I bind a QTcpSocket to a specific network interface (i.e. eth0.1, eth0.2)?

I tried the following pseudo code but it still gets data from the default interface (eth0).
Could you please let me know what I did wrong here?

Thanks a bunch.

Qt Code:
  1. QString intf = "eth0.1";
  2. int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  3. setsocketoption(s, SOL_SOCKET, SO_BINDTODEVICE, intf, intf.length() + 1);
  4.  
  5. QTcpSocket* qSocket = new QTcpSocket(parent);
  6. qSocket->setSocketDescriptor(s, QAbstractSocket::BoundState);
  7.  
  8. QUrl url("192.168.0.10");
  9. QHttp* http = new QHttp;
  10. http->setSocket(qSocket);
  11. http->setHost(url.host(), QHttp::ConnectionModeHttp, 8080u);
  12.  
  13. QBuffer* buffer = new QBuffer;
  14. QHttpRequestHeader header("GET", url.path());
  15. header.setValue("Host", url.host());
  16. // set other values for header here
  17. http->request(header, 0, buffer);
To copy to clipboard, switch view to plain text mode