Java - 重新加载类时关闭套接字

时间:2013-03-12 19:50:53

标签: java sockets port reload

所以我在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()。 / p>

长话短说,如何在重新确定类并使用新端口之前关闭套接字?

以下是它的完整例外:

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)

3 个答案:

答案 0 :(得分:1)

不提供直接解决方案,但不会抛出任何异常,因为您的代码全部吞噬了它们。空抓块永远不是好习惯。至少,使用e.printStackTrace()

打印捕获的异常的堆栈跟踪

答案 1 :(得分:0)

在客户端发出请求之前,不会分配socket变量。如果在此之前调用closePorts(),您的程序仍将在socket = serverSocket.accept();等待,socket.close()会抛出NullPointerException,因为socket仍为空。

有关ServerSocket.accept()的说明,请参阅http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html#accept()

答案 2 :(得分:0)

此处没有类重新加载,您无需关闭现有客户端的已接受套接字。只需关闭ServerSocket并在新端口上重新创建它。