Reputation: 143
I am trying to establish a TCP socket connection between my Android phone and PC for file transfer. A TCP Server program will in PC and TCP client program run in Android. When the server program is running everything works fine. But when the server program is not running and when the client tries to establish the connection, the client hangs(in the sense it waits for long time till it establish theconnection ). How to overcome this problem? I want to terminate the socket as soon the client finds that no such server is running. Is it possible to do that?
Upvotes: 1
Views: 2213
Reputation: 310980
You can control the connect timeout of a Socket
by constructing a new Socket()
and using the Socket.connect(SocketAddress, timeout)
method.
Note that you can use this to decrease the default timeout, which is a bit over a minute, but not to increase it.
Upvotes: 3
Reputation: 2972
The problem seems to be in the server configuration (PC in your case). It might have prohibited the outgoing ICMP traffic, which is responsible of notifying the clients about closed ports.
Try to telnet your service port when it's not running. Like
telnet <my.pc.address> <my-port>
On "normal" configurations it should immediately fail (exactly as your client app shoud) with
telnet: Unable to connect to remote host: Connection refused
On tricky configurations with firewalls, ICMP prohibited, etc it will "hang" for a long time, "thinking" that the service is running and waiting for it's answer.
Upvotes: 2
Reputation: 184
I believe you need to play with the timeout settings of the Socket API. See here for some more detailed discussion:
Upvotes: 3