1

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?

vij
  • 143
  • 1
  • 2
  • 18
  • possible duplicate of [Set timeout for socket](http://stackoverflow.com/questions/4969760/set-timeout-for-socket) – user207421 Jun 17 '12 at 11:30

3 Answers3

3

I believe you need to play with the timeout settings of the Socket API. See here for some more detailed discussion:

Set timeout for socket

Community
  • 1
  • 1
pvlissidis
  • 184
  • 3
3

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
2

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.

LiMar
  • 2,822
  • 3
  • 22
  • 28
  • what ever you said is true. I ve even tested that. It terminates very quickly. But when i try it using java code it hangs. – vij Jun 17 '12 at 10:49