multithreading - Java: Socket closing after try-catch blocks -
i attempting client/server type chat box (using gui's). won't details of multi-threading used in program since believe not part of problem (i hope not) , amount of code post. anyways, both client , server create socket, , other stream classes within try block, , reason sockets close after catch blocks. ps not call socket.close() method anywhere end if early
server, ran constructor of 1 of class. breaks down into, main has server stuff on different thread, (like previous post) fix gui can load , run server stuff without 1 waiting on other. anyways, without detail, here code
public chatappprotocol(socket sock) { super("chatappserver"); // has clas var of socket this.sock = sock; try ( printwriter output = new printwriter(this.sock.getoutputstream(), true); bufferedreader input = new bufferedreader(new inputstreamreader(this.sock.getinputstream())) ; ) { // first stream of string username loging in client string name = input.readline(); // returns false, not closed system.out.println("closed?: " + this.sock.isclosed()); } catch (ioexception e) { e.printstacktrace(); } // problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // closed after catch blocks before methods ends // p.s. plan on using socket in method can't since closes system.out.println("closed?: " +this.sock.isclosed()); }
now client
@fxml private void login() { this.name = this.username.gettext().trim(); this.portnum = integer.parseint(this.port.gettext()); this.name = this.username.gettext().trim(); this.ipaddr = this.ip.gettext().trim(); try (t socket socket = new socket(this.ipaddr, this.portnum); printwriter output = new printwriter(socket.getoutputstream(), true); ) { this.sock = socket; output.println(this.name); // returns false, not closed system.out.println("closed?: " +this.sock.isclosed()); } catch (unknownhostexception e) { system.err.println("problem @ ip: " + this.ipaddr); system.exit(1); } // problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // returns true here, closes before methods end , cant reuse system.out.println("is closed!!!!!! " + this.sock.isclosed()); } }
so, reason why both different class, different files, different project sockets close after try-catch blocks? can't find answer online, , been on while , stuck. found out problem after seeing on server side console
java.net.socketexception: socket closed @ java.net.socket.getoutputstream(socket.java:943) @ chatappserver.chatappprotocol.run(chatappprotocol.java:62)
because you're creating socket
brackets of try
block, automatically closed upon exiting block. instead, try creating inside block , shouldn't closed:
try { this.sock = new socket(this.ipaddr, this.portnum); printwriter output = new printwriter(socket.getoutputstream(), true); output.println(this.name); } catch (unknownhostexception e) { system.err.println("problem @ ip: " + this.ipaddr); system.exit(1); } // this.sock should still open @ point.
have read of java tutorial on try-with-resources more information on why you're getting current behaviour.
Comments
Post a Comment