• Post Reply Bookmark Topic Watch Topic
  • New Topic

HELP!!! Socket Connection Timeout Problem

 
Andres Delrotti
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this piece of code executed inside a Thread

public void run() {




Socket socket = new Socket("hostname", Integer.parseInt("4444"));
InputStreamReader input = new InputStreamReader(socket.getInputStreamReader());
BufferedReader in = new BufferedReader(input);
System.out.println("start.....");
while (true){
if (in.ready()){
int x = in.read();
// do processes
}
}




}



This piece of code works fine but I have this problem. After sometime...the host connection suddenly times out or becomes down. The thread still continues to run..BUT..when the connection comes back, the BufferedReader can no longer receive input from the host and because of this..I need to restart the thread again to receive inputs from the host. How could I catch a socket connection timeout in this process? so i won't have to restart the thread anymore. I have tried to implement some available codes on the net but they didn't seem to work. Can anyone help me on this? I'd appreciate any form of help. Thanks.
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have loops within loops. Why don't you do something like this (very rough sketch):
 
Ian Tsai
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about this??

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!