Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - Closing socket when reloading class
    text
    copied!<p>So I'm working on a server for an IRC, and I added a config screen where you can edit the port it is using, but I have to reintialize the <code>Listen</code> class for it to take effect, so I have this in my Config class:</p> <pre><code>Listen.closePorts(); new Listen(); </code></pre> <p>And here is my Listen class:</p> <pre><code>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 and listening for messages on port " + port + ".\n"); while(true) { socket = serverSocket.accept(); InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String msg = br.readLine(); GUIMain.jta.append("Received message from client: " + msg + "\n"); } } catch (Exception e) { GUIMain.jta.append("Port " + port + " already in use!\n"); } finally { try { socket.close(); } catch(Exception e) { } } } public static void closePorts() { try { socket.close(); GUIMain.jta.append("Server closed\n"); } catch (IOException e) { } } } </code></pre> <p>When I start the application it runs this class, opening the socket with the port specified, but when I try to close the port here:</p> <pre><code>public static void closePorts() { try { socket.close(); GUIMain.jta.append("Server closed\n"); } catch (IOException e) { } } </code></pre> <p>It hangs on <code>socket.close();</code></p> <p>And if I comment <code>Listen.closePorts();</code> out and try to reload the class and leaving the socket open it either hangs or runs the <code>catch()</code> if the port is the same as the port that is in use.</p> <p>So long story short, How can I close the socket before reintalizing the class and use the new port?</p> <p>Here's the full exception it trows:</p> <pre><code>java.lang.NullPointerException at server.network.Listen.closePorts(Listen.java:44) at server.gui.GUISettings.actionPerformed(GUISettings.java:86) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload