How to use QTcpSocket stays in ConnectingState after reconnection.
-
Hai ,
I am doing tcp client application and in my application QTcpSocket stays in ConnectingState after reconnection.
How use "setsockopt " "SO_REUSEADDR" in qt -
@swansorter
Usesetsockopt()
on the native socket descriptor returned by qintptr QAbstractSocket::socketDescriptor() const.For this case of
SO_REUSEADDR
it is a server-side flag (not client), and you should instead follow the advice from @Christian-Ehrlicher below. -
@JonB Sir used that one
data_socket.setSocketOption(QTcpSocket::ReuseAddressHint,1);
it is showing
: error: no matching function for call to ‘QTcpSocket::setSocketOption(QAbstractSocket::BindFlag, int)’
data_socket.setSocketOption(QTcpSocket::ReuseAddressHint,1);
^ -
QTcpSocket::ReuseAddressHint is as per documentation a QAbstractSocket::BindFlag so it can't be use by the function setSocketOption() since this needs a SocketOption flag.
If you need this flag you have to pass it to bind() as written in the documentation to this flag. -
@Christian-Ehrlicher I am developing client side application.can i use bind in client application
-
@swansorter said in How to use QTcpSocket stays in ConnectingState after reconnection.:
can i use bind in client application
Why should you need it there?
-
@Christian-Ehrlicher As i mentioned earlier when i try to reconnect to the same port its showing connectingstate
-
@swansorter
SO_REUSEADDR
should only be set on the server side of a socket. It allows multiple clients to bind to the same server address. Your client side connections should not need this, instead they should be allowed to use a "random" client-side-generated address. -
Don't see what this has to do with SO_REUSEADDR... please read the docs about this option - it's not a client option at all.
-
@JonB @Christian-Ehrlicher then why i am getting connectingstate after reconnection,is ther any solution for this ?
-
@swansorter
If you have not setQTcpSocket::ReuseAddressHint
at the server side, prior tobind()
, the server will not accept a second connection from any client (on the same client IP address), while a client is connected to the server. (There may be a period of time after "reported disconnection" when the client is still connected so the server cannot reuse the address.)So.... have you set this at the server side, in the correct place?
Otherwise you may show your client connection code to verify you are not specifying a client-side port.