Tuesday, 15 September 2015

java - Coding a MultiThreaded Socket Proxy: IOException SocketClosed where it shouldn't -


I have just started a proxy coding for the TCP socket connection and although I see the socket still open I'm an IOException it is the LoC which is causing it. Anyone has an idea why this can happen?

  while (! From.isClosed () & to.isClosed () & amp; amp; num.fBytes = in.read (byteBuffer)) = -1 )  

I have already debug the code; From & amp;

Proxy Public class void key (string [] args) {try (server socket proxy server = new serversecache (5432)) {Java}

  Int i = 0; While (true) {socket client = proxy server. Println (i ++); Socket server = new socket ("localhost", 5000); ProxyHandler Clientos Server = New ProxyHandler (Client, Server); ProxyHandler Servertoolient = New ProxyHandler (Server, Client); ClientToServer.setName ("Customer & gt; Server" + i); ServerToClient.setName ("server & gt; customer" + i); ClientToServer.start (); ServerToClient.start (); System.out.println ("Start Proxy"); }} Hold (IOException e) {// TODO auto-generated cal block e.printStackTrace (); }}}  

ProxyHandler.java

  extension of public class ProxyHandler thread {private socket; To the private socket; Public ProxyHandler (socket, socket to) {this.from = from; this; For =; } @ Override Public Wide Run (try (DataInputStream = new in DataInputStream (from.getInputStream ()); DataOutputStream Out = New DataOutputStream (to.getOutputStream ()) {Byte [] byteBuffer = new byte [1024]; Int NumOfBytes = 0; while (! From.isClosed () and! To.isClosed () & amp; amp; and (numOfBytes = in.read (byteBuffer)) = -1) {out.write (ByteBuffer, 0, numOfBytes); out.flush (); System.out.println (getName () + "(" + numOfBytes + ")"); System.out.println (new string (bytebuffer, "UTF-8 "));} System.out.println (" left: "+ getName ());} hold (IOException io) {System.out.println (" IOException: "+ io.getMessage () +" "+ + + Names ();; Io.printStackTrace ();}}}  

isClosed () Return only True If you, you have explicitly closed your socket, it can not be used to detect unexpected disconnects.


No comments:

Post a Comment