PDA

View Full Version : Binding QTcpSocket to a specific network interface



vkincaid
14th May 2010, 21:28
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.


QString intf = "eth0.1";
int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
setsocketoption(s, SOL_SOCKET, SO_BINDTODEVICE, intf, intf.length() + 1);

QTcpSocket* qSocket = new QTcpSocket(parent);
qSocket->setSocketDescriptor(s, QAbstractSocket::BoundState);

QUrl url("192.168.0.10");
QHttp* http = new QHttp;
http->setSocket(qSocket);
http->setHost(url.host(), QHttp::ConnectionModeHttp, 8080u);

QBuffer* buffer = new QBuffer;
QHttpRequestHeader header("GET", url.path());
header.setValue("Host", url.host());
// set other values for header here
http->request(header, 0, buffer);