How to reproduce "java.net.SocketException.Connection reset"? -
i trying reproduce "java.net.socketexception.connection reset" exception.
wanted know if there program available me simulate it. tried following server , client programs see if simulate not able exception. using java8.
server code-
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.serversocket; import java.net.socket; import java.net.socketexception; import java.net.sockettimeoutexception; public class simpleserverapp { public static void main(string[] args) throws interruptedexception { new thread(new simpleserver()).start(); } static class simpleserver implements runnable { @override public void run() { serversocket serversocket = null; try { serversocket = new serversocket(3333); serversocket.setsotimeout(0); //serversocket. while (true) { try { socket clientsocket = serversocket.accept(); bufferedreader inputreader = new bufferedreader(new inputstreamreader(clientsocket.getinputstream())); system.out.println("client said :"+ inputreader.readline()); } catch (sockettimeoutexception e) { e.printstacktrace(); } } }catch (exception e) { e.printstacktrace(); system.out.println(" exception " + e.getstacktrace()); }/*catch (ioexception e1) { e1.printstacktrace(); }*/ /*finally { try { if (serversocket != null) { serversocket.close(); } } catch (ioexception e) { e.printstacktrace(); } }*/ } } }
client code -
import java.io.ioexception; import java.io.printwriter; import java.net.socket; import java.net.socketexception; import java.net.unknownhostexception; public class simpleclientapp { public static void main(string[] args) { new thread(new simpleclient()).start(); } static class simpleclient implements runnable { @override public void run() { socket socket = null; try { socket = new socket("localhost", 3333); printwriter outwriter = new printwriter(socket.getoutputstream(), true); system.out.println("wait"); thread.sleep(20000); //system.exit(0); //throw new exception("random exception"); //socket.close(); outwriter.println("hello mr. server!"); }catch (socketexception e) { e.printstacktrace(); }catch (interruptedexception e) { e.printstacktrace(); } catch (unknownhostexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } /*finally { try { if (socket != null) socket.close(); } catch (ioexception e) { e.printstacktrace(); } } */ } } }
scenario 1.
- start server program locally.
- start client program locally.
- close client program abruptly (ctrl c) - output on server program "client said :null"
scenario 2.
- start server program locally.
- start client program locally.
- client connected server, while client program waiting close server program abruptly. still no exception.
can tell me way produce connection reset exception, working sample code.
this works me:
class server { public static void main(string[] args) throws exception { serversocket ss = new serversocket(9999); socket s = ss.accept(); inputstream = s.getinputstream(); i.read(); } }
client connects , disconnects without closing socket
class client { public static void main(string[] args) throws exception { socket s = new socket("localhost", 9999); } }
this results in exception on server
exception in thread "main" java.net.socketexception: connection reset @ java.net.socketinputstream.read(unknown source) @ java.net.socketinputstream.read(unknown source) @ java.net.socketinputstream.read(unknown source) @ net.server.main(server.java:13)
Comments
Post a Comment