aspose file tools*
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
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: 19697
    
  20

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to check if connection is still good