ok i need to write a client that can be able to detect the connection between client and server…once server is close and bring back up my client need to be able to connect it back to the server..but i am really not sure how to do it…any help if possible?
public TEST(String serverIP, int serverPort){
Log("Connection to the Server....");
try{
socket = new Socket(serverIP, serverPort);
Log("Connected to the server : "+socket);
start();
} catch(UnknownHostException uhe){
System.out.println("Unknown Host: "+ uhe.getMessage());
} catch (IOException ioe){
System.out.println("IO Exception: "+ioe.getMessage());
}
String readline = "";
streamOutput.println("TRY test");
while(true){
try{
readline = streamInput.readLine();
System.out.println(readline);
} catch (IOException ioe){
System.out.println("Error in sending: "+ioe.getMessage());
return;
}
}
}
above is my client code that use to connect to a server once it is compiled…
The only way to know a connection is closed is to try sending data over the wire. Connections don’t automatically test open/closed status. If you want to check for an closed connection and reconnect, you’ll have to send a heartbeat signal from one to the other. Usually, it’s just a special byte or tiny message that tells the other side “I’m still here”
But then, depending on your application, you may not need that. Do both sides of your application start messages? Or is it an RMI-style app? Where the server just listens for requests?
Maybe something like this?