java - Is calling ServerSocket.close() sufficient enough to close a port? -


i have java code looks similar this:

private void startserver() throws ioexception {         urlclassloader classloader = null;          system.out.println("opening server socket listening on " + port_number);         try {             server = new serversocket(port_number);             server.setsotimeout(10000);             connected = true;             system.out.println("server listening on port " + port_number);         } catch (ioexception e) {             system.err.println("could not start server on port " + port_number);             e.printstacktrace();             connected = false;         }          while (connected) {              // incoming request handler socket.             socket socket = null;             try {                 system.out.println("waiting client connection...");                 // block waiting incoming connection.                 socket = server.accept();                 if (socket == null) continue; 

...and on , forth. when call server.close() later on (i don't different behavior if call socket.close() first), don't errors, netstat shows port still being listened on. should calling serversocket.close() sufficient enough free port on system?

i programming java 1.4.2 microedition runtime. worthy note have method being run in thread, , trying close socket parent thread.

edit here line netstat, though can assure still being listened on, since if start xlet again exception port number.

tcp        0      0  *.2349                 *.*                    listen 

there several things consider. 1 of them described following quotation javadoc of serversocket

public void setreuseaddress(boolean on) throws socketexception

enable/disable so_reuseaddr socket option. when tcp connection closed connection may remain in timeout state period of time after connection closed (typically known time_wait state or 2msl wait state). applications using known socket address or port may not possible bind socket required socketaddress if there connection in timeout state involving socket address or port.

so kind of ok os can still show there going on after close() server socket. if going open/close server socket on same port might hit problem.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -