0

I have an android app with always active socket connections. If connection lost, the app immideately reconnect to the server. Socket running in seperate thread.

I can correctly close socket in different situations. For example, when screen off or back button pressed.

I tried to override onPause, onResume, onDestroy with this:

I can correctly close socket in different situations. For example, when screen off or back button pressed.

I tried to override onPause, onResume, onDestroy. I have tried all three of these approaches,

Closing the socket explicitly:

socket.close();

Just nulling it out:

socket = null;

And I have also tried the shutdownInput method:

socket.shutdownInput();

but server continue thinking that socket is alive.

P.S.: by the way, when I recompile and run again android app the connection drops well.

selbie
  • 100,020
  • 15
  • 103
  • 173
Ok-Alex
  • 558
  • 2
  • 13
  • 29
  • 2
    How does the code above not crash or throw an exception? I mean, you are calling shutdownInput method on a null object. – selbie Feb 04 '12 at 01:56
  • selbie, of course those 3 lines of code are not working together. I just mentioned my attepts. 3 lines = 3 different attempts – Ok-Alex Feb 04 '12 at 01:59
  • Oh, I see. That is completely unobvious. A single code block like the one you have above is usually interpreted on SO to mean the actual code path you have tried. Let me edit your question for you. – selbie Feb 04 '12 at 02:02
  • I'm actually not a java expert - so I'll hold back from posting an answer. But socket.close() is almost certainly the right method to call. Perhaps the bug is on the server? Or that Android when android suspends your app, the pending close doesn't get sent? I doubt it, but if you can run a sniffer to see if teh FIN is getting sent, you can confirm whether this is a client or server bug. – selbie Feb 04 '12 at 02:11

2 Answers2

0

You should make the server to close the socket too. If you don't do that the server will belive the client server is still open until a write / read attempt is tried by the server.

I suggest to check the client-server protocol: you should read some "command" from the server that could be interpreted as a close request; then the server could close the socket too. Something like this on server side should help you

1 - server creates listening socket

2 - server accept()

3 - The following:

while( readClientCommand() !=CLOSE_COMMAND)

   // process command

4 - socket.close();

Littm
  • 4,923
  • 4
  • 30
  • 38
0

What you're describing appears to be a common issue when dealing with sockets; namely, even though you may have correctly closed the socket on the client side (i.e. in your Android app), your server is not aware of this, and that is probably why it still thinks the socket is alive.

Community
  • 1
  • 1
Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54