Results 1 to 2 of 2
- 10-27-2009, 08:05 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
Socket Exceptions not being generated, readLine not returning null
I'm having a really weird problem that I can't for the life of me figure out. This is happening in a multi-client game I'm writing, but I've been able to reproduce it in significantly less code for posting here.
The way i understand BufferedReader is that readLine is supposed to block until it either reads a line OR the underlying stream has an error in which case it returns null. This is how it has been working for simple tests, but with the following code I can reproduce a case in which it fails to work properly, leaving a client connected that should actually be disconnected.
Here's a log of what happens when the problem occurs. The remarks inside angle brackets are when I physically closed the connections, not something the program outputs.Java Code:package sockettest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; class Client extends Thread{ BufferedReader stdin; PrintWriter stdout; Socket s; int id; Client(Socket s, int id) throws IOException { this.s = s; this.id=id; stdin = new BufferedReader(new InputStreamReader(s.getInputStream())); stdout = new PrintWriter(s.getOutputStream()); System.out.println("Client " + id + " connected."); this.start(); } @Override public void run() { try { while(s.isConnected()) { String str = stdin.readLine(); System.out.println("Client " + id + ": " + str); stdout.println(str.toUpperCase()); stdout.flush(); } } catch (IOException ex) { ex.printStackTrace(); } finally { try { s.close(); System.out.println("Client " + id + " disconnected."); } catch ( Exception e) { e.printStackTrace(); } } } } public class Main { public static void main(String[] args) { try { ServerSocket s = new ServerSocket(4444); int count = 0; while(true) new Client(s.accept(),count++); } catch (IOException ex) { ex.printStackTrace(); } } }
As you can see when Client 0 closes telnet the server doesn't even notice it. It isn't until Client 1 closes his connection that the server notices that both Client 1 and Client 0 are closed.Java Code:Client 0 connected. Client 0: A Client 1 connected. Client 1: B <Client 0 closed telnet> Client 1: C Client 1: D Client 1: E <Client 1 closed telnet> Client 1: null Client 1 disconnected. java.net.SocketException: Connection reset Client 0 disconnected. at java.net.SocketInputStream.read(SocketInputStream.java:168) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) at java.io.InputStreamReader.read(InputStreamReader.java:167) at java.io.BufferedReader.fill(BufferedReader.java:136) at java.io.BufferedReader.readLine(BufferedReader.java:299) at java.io.BufferedReader.readLine(BufferedReader.java:362) at sockettest.Client.run(Main.java:27) Exception in thread "Thread-1" java.lang.NullPointerException at sockettest.Client.run(Main.java:29) <Client 0 Closed Telnet>
I'm at a loss for what else I can test/check/do. If you want more information, or have a suggestion please feel free to ask/let me know. If this is a well known problem please point out to me where it is (I've been searching for a while and although I've found some related stuff, they don't solve this problem).
Thank you for your time.
Jody SteeleLast edited by jmlsteele; 10-28-2009 at 02:41 AM.
- 10-28-2009, 02:39 AM #2
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
*sigh*
Much to my expectations there ISN'T anything wrong with the code, or Java, it was with the manner I was testing it.
I was using PuTTY to connect to the server, and then instead of opening a brand new connection I simply used PuTTY's built in "Duplicate Session" feature.
Apparently there is a known bug with Duplicate Session where the Parent won't close its sockets until all of their children are gone.
Link: Google "PuTTY semi-bug dup-sessions-dont-close" (since I can't post URLs yet)
Similar Threads
-
auto generated keys
By abhi_iips in forum New To JavaReplies: 3Last Post: 03-13-2009, 07:14 AM -
How to delete pre-generated code?
By Terentius in forum NetBeansReplies: 0Last Post: 08-19-2008, 04:02 PM -
getting dynamically generated valus
By abhiN in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 03-29-2008, 10:58 AM -
Generated servlet error
By tommy in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 08-05-2007, 10:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks