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


i started coding proxy tcp socket connections , although i'm checking if sockets still open i'm getting ioexception. loc causing it. has idea why can happen?

while (!from.isclosed() && !to.isclosed() && (numofbytes = in.read(bytebuffer)) != -1)  

i've debugged code; & not closed when checked.

for context:

proxy.java

public class proxy {      public static void main(string[] args)     {         try (serversocket proxyserver = new serversocket(5432))         {             int = 0;             while (true)             {                 socket client = proxyserver.accept();                 system.out.println(i++);                 socket server = new socket("localhost", 5000);                 proxyhandler clienttoserver = new proxyhandler(client, server);                 proxyhandler servertoclient = new proxyhandler(server, client);                 clienttoserver.setname("client->server"+i);                 servertoclient.setname("server->client"+i);                 clienttoserver.start();                 servertoclient.start();                 system.out.println("proxy started");             }         }         catch (ioexception e)         {             // todo auto-generated catch block             e.printstacktrace();         }     } } 

proxyhandler.java

public class proxyhandler extends thread {      private socket from;     private socket to;      public proxyhandler(socket from, socket to)     {         this.from = from;         this.to = to;     }      @override     public void run()     {         try (datainputstream in = new datainputstream(from.getinputstream());                 dataoutputstream out = new dataoutputstream(to.getoutputstream()))         {              byte[] bytebuffer = new byte[1024];             int numofbytes = 0;             while (!from.isclosed() && !to.isclosed() && (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());         }         catch (ioexception io)         {             system.out.println("ioexception: " + io.getmessage() + " " + getname());             io.printstacktrace();          }     } } 

isclosed() returns true if you, yourself, have explicitly closed socket. can not used detect unexpected disconnects.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -