java - Socket does not throw exception when trying to write to closed connection -


i new in java socket programming have faced problem.
when trying write connection unexpectedly closed in client side don`t exception. have server side.

    public static void main(string[] args) throws ioexception {      serversocket socket = new serversocket(8036);     string st;     while(true) {         system.out.println("hello");         socket socket1 = socket.accept();         bufferedreader reader = new bufferedreader(new inputstreamreader (socket1.getinputstream()));         bufferedwriter writer = new bufferedwriter(new outputstreamwriter(socket1.getoutputstream()));         system.out.println(reader.readline());         writer.write("h");         writer.flush();     } } 

and client

    public static void main(string[] args) throws  exception{      inet4address s = (inet4address)inet4address.getbyname("localhost");     system.out.println("begin");      socket socket = new socket(s,8036);      bufferedwriter w = new bufferedwriter (new outputstreamwriter(socket.getoutputstream()));      bufferedreader reader = new bufferedreader(new inputstreamreader(socket.getinputstream()));      string h ;      int prev = 0; } 

as can see client exits after connection, dont close socket, try model client have exited unexpectedly. dont exception in server side while trying write socket. why?

  1. tcp cannot know on receiving fin peer has closed connection reading. knows peer has closed connection writing. have shutdown connection output , still reading. first write such connection cannot error.

  2. when write tcp connection, data buffered locally in socket send buffer, , control returned application. writing network proceeds asynchronously. first write connection has been closed peer unlikely detect that. subsequent write will.

nb question writing connection has been closed peer, not writing closed socket. give ebadf immediately.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -