2

I'm developing a Qt application and experience rather weird network issue. Let me show how it looks from end-user perspective.

First I start up my server and verify that it's listening on a target port:

[user@host server]$ sudo netstat -anp | grep 30004
    tcp        0      0 0.0.0.0:30004               0.0.0.0:*                   LISTEN      11113/./server 

Then I connect to the server with telnet:

[user@host server]$ telnet localhost 30004
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

Netstat displays that connection is now established. Nothing fancy so far:

[user@host server]$ sudo netstat -anp | grep 30004
tcp        0      0 0.0.0.0:30004               0.0.0.0:*                   LISTEN      11113/./server 
tcp        0      0 127.0.0.1:30004             127.0.0.1:34608             ESTABLISHED 11113/./server 
tcp        0      0 127.0.0.1:34608             127.0.0.1:30004             ESTABLISHED 12657/telnet        

Then the server drops the connection based on application-specific timeout. It is set to 10 seconds at the moment:

[user@host server]$ sudo netstat -anp | grep 30004
tcp        0      0 0.0.0.0:30004               0.0.0.0:*                   LISTEN      11113/./server 
tcp        0      0 127.0.0.1:30004             127.0.0.1:34608             TIME_WAIT   -                   

I then shut down the server and verify that the listenning socket is destroyed:

[user@host server]$ sudo netstat -anp | grep 30004
tcp        0      0 127.0.0.1:30004             127.0.0.1:34608             TIME_WAIT   -                   

Finally I start up the server again, but the listening port doesn't show up anymore:

[user@host server]$ sudo netstat -anp | grep 30004
tcp        0      0 127.0.0.1:30004             127.0.0.1:34608             TIME_WAIT   -                   

As a result client cannot connect to the server:

[user@host server]$ telnet localhost 30004
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused

What am I doing wrong here? I'm inclined to think that this is a configuration issue, not a bug in the application. This scenario seems to work on my laptop's Ubuntu. The aforementioned output was produced on linux box as well.

UPDATE: One more thing that is different in these two environemnt is qt version. On my notebook I have 4.8.6, on linux box it's 4.6.2. Not sure if it matters.

krakovjak
  • 51
  • 1
  • 7
  • What error do your code report when you try to bind/listen/accept your server socket ? Is there any difference if you set the SO_REUSEADDR socket option on your server socket (same as using [QAbstractSocket::ShareAddress](http://qt-project.org/doc/qt-5/QAbstractSocket.html) as the BindMode if you're using Qt sockets )? Is your server listening on anything (run netstat -apn |grep server) – nos Sep 25 '14 at 13:17
  • @nos, I'm using QTcpServer and all I can see is that call to the listen method returns false. How can I enable this option for QTcpServer? Afaik QTcpServer sets this option automagically. Nope, nothing: [user@host server]$ sudo netstat -anp | grep server Active Internet connections (servers and established) Active UNIX domain sockets (servers and established) – krakovjak Sep 25 '14 at 13:25
  • It seems to be the default on *nix. So if listen() returns false, call QAbstractSocket::SocketError and see what it says. You should probably post your code anyway. – nos Sep 25 '14 at 13:35
  • Thanks, it reports 8, i.e. QAbstractSocket::AddressInUseError... – krakovjak Sep 25 '14 at 13:47
  • How it's possible that QTcpServer::listen yields AddressInUseError although it sets SO_REUSEADDR option? – krakovjak Sep 26 '14 at 07:17

1 Answers1

0

Apparently there was an issue with versions of qt libraries. We upgraded it to latest 4.x.x and now the problem seems to be resolved.

krakovjak
  • 51
  • 1
  • 7