This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes How to check if connection is still good Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How to check if connection is still good" Watch "How to check if connection is still good" New topic
Author

How to check if connection is still good

Cliff Smith
Greenhorn

Joined: Mar 02, 2011
Posts: 3

I have written an app that monitors multiple external devices (serial to ethernet converters spread over a wide geographic area). Everything is working except I can't tell if a connection drops (remote box down, power out, mice chewed through cable, somebody kicked the plug out, etc.)
I am using telnet protocol from the jakarta.commons.net.telnet (?) package.
Each remote device is monitored in a seperate thread and the whole thing runs as a service on a linux machine.
I can add code to periodically check something, but what?
Any ideas?

Thank you,
Cliff Smith


No situation is ever improved by panic.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

How about asking for the current folder using pwd? The results won't be long and it's sure to give you back something.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Cliff Smith
Greenhorn

Joined: Mar 02, 2011
Posts: 3

Rob,
Thanks for your reply.
On my end I am running what amounts to a passive listening service once the connection has been established. The serial ports that are being monitored remotely are not communication ports to any type of system in the normal sense (linux, windows, rtos). About the only remote commands they respond to are reboot and reset to factory defaults.
Once the connection is established, there is a 'keep-alive' issued by the remote device over the ip port every 45 seconds, but I can't figure out a way to check from within the thread if the connection is still 'live'.
If I understand it correctly, 'telnet.isConnected', for example, only tells me if a connection was established initially but not if it is still active at a later time.
Any ideas?

Cliff Smith
Cliff Smith
Greenhorn

Joined: Mar 02, 2011
Posts: 3

I think I figured out a work-around. The remote units seem to always respond to any input with an <ACK/>. It appears that if it is an invalid command they still return the <ACK/> but otherwise ignore the message. So what I do is send what would be an invalid command, like just a space (0x20). If no response, then the connection has been lost and the appropriate measures taken. If I get a response the connection is still good.
Unfortunately, this is specific to these devices and not something within Java itself, but it works for my needs.
 
jQuery in Action, 2nd edition
 
subject: How to check if connection is still good
 
Similar Threads
java.net.SocketException: Connection closed by remote host
ServerSocket deny connection
Client and EJB components on different servers
Checking the results of a ResultSet
Java remote debugging