• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to detect connection status

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I would like to write java class that will connet to certain host and port, and stay behind, when connection error occurs, it will detect and try to reconnect silently. how do i make this kind of class for both performance and reliablity of connection.


import java.net.*;
import java.io.*;

public class SolidConnector implements Runnable {
private Socket socket;
private String host;
private int port;
private int reconnectTime = 3000;

...

public SolidConnector(String host,int port){
socket = new Socket();
this.host = host;
this.port = port;
}

public boolean connect(){
try {

socket= new Socket(host,port);
System.out.println ("connected to" +socket.getInetAddress().getHostName());
return true;
}catch (Exception ex) {
//fail
}
return false;
}

public void run(){
int counter = 0;
while(!connect()){
try {
System.out.println ("connecting..."+ ++counter);
Thread.sleep(reconnectTime);

}
catch (Exception ex) {

}
}

while(true){

if(stopped) break;
try {
//nothing do, give cpu to other threads
Thread.sleep(2000);
//i want to detect connection is ok or not
//if connection got problem it throws Connction reset exception
socket.getInputStream().read();

}catch (IOException ex) {
System.out.println ("Connection Lost!");
reconnect();
}catch (InterruptedException ie){

}
}
}

public void reconnect() {
socket = null;
int counter = 0;
while(!connect()){
try {
System.out.println ("reconnecting..."+ ++counter);
Thread.sleep(reconnectTime);
}
catch (InterruptedException ex) {
//interrupted
}
}
}

public void stop(){
this.stopped = true;
}

public static void main (String[] args) {

SolidConnector sc = new SolidConnector("localhost",8000);
Thread t = new Thread(sc);
t.start();

}

}


The key point i like to know is how to detect when connection lost? I dont want to detect by socket.getInputStream().read() method. I would like to detect silently.

Please give suggestions for how to overcome this problem for performance and reliabily issues.

Thanks in advance,
kaze
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't detect that a TCP connection has dropped without trying to use it; there's no "heartbeat" or other signal that travels back and forth to inform the endpoints of the connections status.

Don't try to poll the connection and reconnect if it drops; just be prepared to reconnect and retry if an actual attempt to use the connection fails.
 
Joe Blant
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ernest Friedman-Hill

Thanks for your reply.
 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Why should we not poll the connection or reconnect if the line drops. Actually I was just looking of this sort of question on this forum... Can you please explain.

Thanks
Shashank
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A polling routine that periodically pings and reconnects even if the main routine doesn't want to send a message right now is extra complexity and overhead on both ends. It wouldn't guarantee a good connection, either, because the connection could fail in the time between the last poll and the next message.

Try to send the message, if the connection is bad, reconnect and try again. That does exactly what's needed and no more at exactly the time it's needed.

In fact, check how long it takes to connect and see if you can afford that much delay every time you send a message. Connecting may well be the slowest thing you do, but not necessarily unacceptable. Use real times to prove it's worth keeping the connection open at all.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic