Note that there are some explanatory texts on larger screens.

plurals
  1. POsocket.getInputSteam.read() does not throw when I close the socket from the client
    text
    copied!<p>I am on windows 7 x64. I am writing a server which opens a thread for every incoming connection - the thread reads from the connection's input stream. The read() should block and throw an exception if the socket is closed(). It does not - just returns -1. If I do not close the connection from the client - just let the client terminate - I get a connection reset as excpected - but if I close() the connection from the client (or just the client's output stream for that matter) read() in the server thread does not throw - just returns <code>-1</code>. The <a href="http://docs.oracle.com/javase/6/docs/api/java/net/Socket.html#close%28%29" rel="nofollow">docs</a> are pretty clear on this :</p> <blockquote> <p>public void close() throws IOException</p> <pre><code>Closes this socket. Any thread currently blocked in an I/O operation upon this socket will throw a SocketException. </code></pre> </blockquote> <p>Help</p> <p>Working code :</p> <p>Server :</p> <pre><code>import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.logging.Level; import java.util.logging.Logger; public class CloseTest { private int port; public CloseTest(int port) { this.port = port; } void base_station_activate() { ServerSocket baseStationListeningSocket=null; try { baseStationListeningSocket = new ServerSocket(this.port, 1, InetAddress.getByName("127.0.0.1")); } catch (IOException ex) { } main_server: while (true) { try { Socket clientSocket = baseStationListeningSocket.accept(); BaseStationClientHandler ch = new BaseStationClientHandler(clientSocket); Thread myThread = new Thread(ch); myThread.start(); } catch (IOException ex) { System.exit(1); } // main_server finally { // baseStationListeningSocket.close() } } } public static void main(String args[]){ CloseTest bs = new CloseTest(8082); bs.base_station_activate(); } public class BaseStationClientHandler implements Runnable { private final Socket clientSocket; private BaseStationClientHandler(Socket clientSocket) { this.clientSocket = clientSocket; } public void run() { String debug_message = null; try { InputStream in = clientSocket.getInputStream(); // read message and respond String s = ""; char x; int r; server: while (true) { try { while ((r = in.read()) != (int) '%') { if (r == -1) { debug_message = "Stream/socket .closed() - exception not thrown (WHYYYYY ????) by client"; System.out.println(debug_message); break server; } x = (char) r; s += x; } System.out.println(s); } catch (SocketException socketException) { System.out.println(socketException.getLocalizedMessage()); // if connection reset (but not if Stream/socket .closed()) read throws !!!!! debug_message = "socket_reset"; break server; } s = ""; } //server } catch (IOException ex) { System.out.println("IOexception in client handler - check if thrown by read"); } finally { try { clientSocket.close(); } catch (IOException ex) { } } } } } </code></pre> <p>Client :</p> <pre><code>import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.util.Vector; public class CloseTestClient { public CloseTestClient(int port, String ipAddress){ Vector&lt;Socket&gt; connections = new Vector&lt;Socket&gt;(); try { for(int i=0;i&lt;20;i++){ Socket connection = new Socket(InetAddress.getByName(ipAddress), port); connections.add(connection); OutputStream out = connection.getOutputStream(); out.write( ("CONNECT#"+(i+1)+"#1%").getBytes()); System.out.println("[CloseTestClient SENT]:"+"CONNECT#"+(i+1)+"#1%"); Thread.sleep(1000); // to be sure the server threads are blocked in the read() // connection.close(); // if I comment this out I see the connection reset message from the server when this main terminates // commented it out finally and moved the closing at the end to be sure the server threads are blocked in read() } } catch (Exception ex) { ex.printStackTrace(); } finally{ // if I comment *for* out I see the "connection_reset" message from the server when this main terminates for (Socket c : connections){ try{ c.close(); }catch(Exception ex){ } } } } public static void main(String args[]){ System.out.println("CloseTestClient run !"); new CloseTestClient(8082,"127.0.0.1"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload