Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

Reading from Streams during connection timeout  RSS feed

 
Andres Delrotti
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • 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: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Welcome to the JavaRanch, AJ. Please do not post the same question in more than one forum. Everyone here reads more than one forum so you aren't increasing your audience. It can also cause the community to duplicate effort as we try to answer every question.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!