Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Disconnect client socket in java  RSS feed

 
Anh Duc
Greenhorn
Posts: 9
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,
I am developing a chat project to serve iOS, Android, WindowsPhone application,
I got bit problem:
How can server socket detect disconnected client???,
Explain:
1. when i used java console, i click X button of windows then server socket detected disconnected client and i remove it in user online list
2. when i use client on cell phone application and i shutdown Wifi and 3G to disconnect server socket but server socket did not detect disconnected client.

How to solve this problem??

P/S: i can not use clientSocket.setSoTimeout() because client always connect to server socket, it only disconnect when network problem.
 
Henry Wong
author
Sheriff
Posts: 22832
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, welcome to the ranch.

Second, you really should consider the forum you are posting in more carefully -- as network questions don't really belong in the "meaningless drivel" forum. Unless of course, you really do consider your question as meaningless drivel.

Anyway, I will move this question to the network forum for you.

Henry
 
Ron McLeod
Saloon Keeper
Posts: 1425
206
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you wait long enough, you will eventually get an indication that the connection was closed due to TCP keepalive. With Linux, by default, this would be after a bit over 2 hours.

You might consider implementing a type of heartbeat mechanism at the application layer where the client and/or server periodically send a message to the other side, querying if they are still alive. Failure to receive a timely response would indicate that the other side was gone and the application to close the socket. On the client-side, you may possibly trigger an attempt to re-connect. Periodic messages may also help when the connection between the client and server traverses through a firewall. Firewalls often drop inactive connections and having a heartbeat messages may help maintain a connection which otherwise may be torn-down after after some time.

One down-side however with mobiles is that if they were sleeping, that sending the heartbeat message require that the mobile to woken-up and possibly the WiFi or cellular radio turned-on. This might have a measurable effect on the battery life of the device.
 
Anh Duc
Greenhorn
Posts: 9
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
First, welcome to the ranch.

Second, you really should consider the forum you are posting in more carefully -- as network questions don't really belong in the "meaningless drivel" forum. Unless of course, you really do consider your question as meaningless drivel.

Anyway, I will move this question to the network forum for you.

Henry


Thanks sir,
 
Anh Duc
Greenhorn
Posts: 9
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:If you wait long enough, you will eventually get an indication that the connection was closed due to TCP keepalive. With Linux, by default, this would be after a bit over 2 hours.

You might consider implementing a type of heartbeat mechanism at the application layer where the client and/or server periodically send a message to the other side, querying if they are still alive. Failure to receive a timely response would indicate that the other side was gone and the application to close the socket. On the client-side, you may possibly trigger an attempt to re-connect. Periodic messages may also help when the connection between the client and server traverses through a firewall. Firewalls often drop inactive connections and having a heartbeat messages may help maintain a connection which otherwise may be torn-down after after some time.

One down-side however with mobiles is that if they were sleeping, that sending the heartbeat message require that the mobile to woken-up and possibly the WiFi or cellular radio turned-on. This might have a measurable effect on the battery life of the device.


Thanks for your response,
It mean, Will server send ping message to client every seconds???
If server send ping message in short time, what if it influences on system's perpormance when server send message every seconds ???
 
Ron McLeod
Saloon Keeper
Posts: 1425
206
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would send the heartbeat messages at whatever rate is required - that could be every few seconds or several minutes. The rate a which the server pings the clients probably has little effect on the server-side (other than increased network traffic), but heartbeating too frequently would probably shorten the battery life of the mobile handset.
 
Anh Duc
Greenhorn
Posts: 9
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:You would send the heartbeat messages at whatever rate is required - that could be every few seconds or several minutes. The rate a which the server pings the clients probably has little effect on the server-side (other than increased network traffic), but heartbeating too frequently would probably shorten the battery life of the mobile handset.

Thanks,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!