• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Disconnect client socket in java

 
Greenhorn
Posts: 9
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 4636
581
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 4636
581
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Good night. Drive safely. Here's a tiny ad for the road:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic