2

I have tcp socket client on android. Also I have tcp socket server on nodejs, which sends to connected client test message every 2 seconds. When I launch application, it receives test messages from server. But when I switch off wi-fi connection, my client socket closes and throws SocketException (etimedout), and if I switch on wi-fi I cannot receive server messages. But server messages are queued in server already and waits to send to client.

As I know, if tcp client losts connection, it should try to repair connection and receive server messages. But in my case, client cannot receive messages, because socket closed.

With windows and linux socket client on my pc, I'm doing same things, but when I switch off wi-fi, client socket not closed, and when I switch wi-fi on, I received messages, which has sent, when wi-fi disabled.

Question: Why in android, socket closes in no time? Can I set any timeout for socket client to close?

I tried :

 setKeepAlive(true); setSoTimeout(10000);

Tried to use SocketFactory.getDefault().createSocket();

No effect.

Vüsal
  • 2,580
  • 1
  • 12
  • 31
  • It may be that when you turn off wifi, Android is actually removing that network interface. It might be interesting to compare the difference between turning off wifi on the Android device, vs. unplugging the access point it was connected to. – Chris Stratton Nov 04 '14 at 17:04
  • I tried to unplug the access point. Same result. – Vüsal Nov 14 '14 at 21:16

2 Answers2

0

What is your SoLinger set to?

Try using

setSoLinger(boolean on, int timeout)

to see if it helps your situation.

Chackle
  • 2,249
  • 18
  • 34
  • Thanks for answer. Unfortunatly this doesn't help. And the next I've found from SocketOptions.java about SO_LINGER: "Number of seconds to wait when closing a socket if there * is still some buffered data to be sent." But it's not what I need. I have no message to server, I only read from InputStream. – Vüsal Nov 04 '14 at 12:28
0

But when I switch off wi-fi connection, my client socket closes and throws SocketException

Your socket doesn't close. There are problems with socket inputstream when WiFi switches off. It returns -1, but socket is alive.