QTcpSocket can not bind to specified net card
-
I am using QT 5.9 in a embeded board that has to net card. When I use QTcpSocket 's "bind" function to bind the net card by IP addr ,the QTcpSocket dose not send data using the net card specified,instead ,it send data rely on the route,I dont know why. The board is running linux3.10,is it possible the linux reason?Please help me,thanks
-
data is always sent based on the routing table. bind() binds for listenning, not for sending.
-
If you want your socket bind to a specific interface you should look for
SO_BINDTODEVICE
- Qt does not provide a high-level api for this (for good reasons when you search enough) -
If you want your socket bind to a specific interface you should look for
SO_BINDTODEVICE
- Qt does not provide a high-level api for this (for good reasons when you search enough)@Christian-Ehrlicher thank for your reply. From your reply, I realized I have a wrong way.But Qt seems to not support this option,SO,I create socket alone and the use "setSocketDescriptor" function set the socket to QTcpSocket.At start it's worked,but sometimes Qt report a problem ,just like :
QNativeSocketEngine::bytesAvailable() was called in QAbstractSocket::UnconnectedState
QNativeSocketEngine::read() was called not in QAbstractSocket::ConnectedState or QAbstractSocket::BoundState
but I don't know what I have done make this happen -
data is always sent based on the routing table. bind() binds for listenning, not for sending.
@Kent-Dorfman I create socket alone and the use "setSocketDescriptor" function set the socket to QTcpSocket.At start it's worked,but sometimes Qt report a problem ,just like :
QNativeSocketEngine::bytesAvailable() was called in QAbstractSocket::UnconnectedState
QNativeSocketEngine::read() was called not in QAbstractSocket::ConnectedState or QAbstractSocket::BoundState
but I don't know what I have done make this happen -
Why not simply use QTcpSocket::socketDescriptor() and call setsockopt there?
Your problem is that you don't call setSocketDescriptor() correctly - you either have to connect by low-level ai or pass the correct second argument to setSocketDescriptor()
-
Why not simply use QTcpSocket::socketDescriptor() and call setsockopt there?
Your problem is that you don't call setSocketDescriptor() correctly - you either have to connect by low-level ai or pass the correct second argument to setSocketDescriptor()
@Christian-Ehrlicher I have sovled the problem by your suggestion,thanks a lot