所以我正在为 IRC 开发服务器,我添加了一个配置屏幕,您可以在其中编辑它正在使用的端口,但我必须重新初始化Listen
该类才能使其生效,所以我在我的 Config 类中有这个:
Listen.closePorts();
new Listen();
这是我的听课:
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) { }
}
}
当我启动应用程序时,它运行此类,打开指定端口的套接字,但是当我尝试在此处关闭端口时:
public static void closePorts() {
try {
socket.close();
GUIMain.jta.append("Server closed\n");
} catch (IOException e) { }
}
它挂在socket.close();
如果我注释掉Listen.closePorts();
并尝试重新加载类并保持套接字打开,catch()
如果端口与正在使用的端口相同,它要么挂起要么运行。
长话短说,如何在重新初始化课程并使用新端口之前关闭套接字?
这是它抛出的完整异常:
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)