Java - Closing socket when reloading class -
so i'm working on server irc, , added config screen can edit port using, have reintialize listen
class take effect, have in config class:
listen.closeports(); new listen();
and here listen class:
package server.network; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.serversocket; import java.net.socket; import server.server; import server.gui.guimain; public class listen { private static socket socket = null; private int port; public listen() { try { port = server.listenport; @suppresswarnings("resource") serversocket serversocket = new serversocket(port); guimain.jta.append("\nserver started , listening messages on port " + port + ".\n"); while(true) { socket = serversocket.accept(); inputstream = socket.getinputstream(); inputstreamreader isr = new inputstreamreader(is); bufferedreader br = new bufferedreader(isr); string msg = br.readline(); guimain.jta.append("received message client: " + msg + "\n"); } } catch (exception e) { guimain.jta.append("port " + port + " in use!\n"); } { try { socket.close(); } catch(exception e) { } } } public static void closeports() { try { socket.close(); guimain.jta.append("server closed\n"); } catch (ioexception e) { } } }
when start application runs class, opening socket port specified, when try close port here:
public static void closeports() { try { socket.close(); guimain.jta.append("server closed\n"); } catch (ioexception e) { } }
it hangs on socket.close();
and if comment listen.closeports();
out , try reload class , leaving socket open either hangs or runs catch()
if port same port in use.
so long story short, how can close socket before reintalizing class , use new port?
here's full exception trows:
java.lang.nullpointerexception @ server.network.listen.closeports(listen.java:44) @ server.gui.guisettings.actionperformed(guisettings.java:86) @ javax.swing.abstractbutton.fireactionperformed(unknown source) @ javax.swing.abstractbutton$handler.actionperformed(unknown source) @ javax.swing.defaultbuttonmodel.fireactionperformed(unknown source) @ javax.swing.defaultbuttonmodel.setpressed(unknown source) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(unknown source) @ java.awt.component.processmouseevent(unknown source) @ javax.swing.jcomponent.processmouseevent(unknown source) @ java.awt.component.processevent(unknown source) @ java.awt.container.processevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.lightweightdispatcher.retargetmouseevent(unknown source) @ java.awt.lightweightdispatcher.processmouseevent(unknown source) @ java.awt.lightweightdispatcher.dispatchevent(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.window.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$200(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
the socket
variable not assigned until after client makes request. if call closeports()
before happens, program still waiting @ socket = serversocket.accept();
, socket.close()
throw nullpointerexception because socket
still null.
see http://docs.oracle.com/javase/7/docs/api/java/net/serversocket.html#accept() description of serversocket.accept().
Comments
Post a Comment