• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tough one: remote socket closed

 
Ben Roy
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This server I'm working on spawns a new thread for each user that connects. It passes the socket, which the thread uses to make a new BufferedReader. Then it has a while loop:
while(keepGoing)
{
in.readLine();
doStuff();
Thread.sleep();
}
This is obviously an approximation. Anyway, it recognizes a disconnect command, in which case the thread cleans up and kills itself. But if someone just closes the telnet session they have open, the thread doesn't die (why should it?) and keep trying to read null, which also does all kinds of other nasty stuff. So how do I know if that particular connection was closed?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can test the readLine() for a return of -1. Since nothing in the ASCII range is negative, getting this value back means sumpn ain't right with the connection. Then just close() on that case.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ben Roy
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So readLine() returns -1 when it fails to read anything? Not null?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!