调用 ServerSocket.close() 是否足以关闭端口?

发布于 2024-11-25 05:54:40 字数 1384 浏览 2 评论 0原文

我有一些与此类似的 java 代码:

private void startServer() throws IOException {
        URLClassLoader classloader = null;

        System.out.println("Opening server socket for listening on " + PORT_NUMBER);
        try {
            server = new ServerSocket(PORT_NUMBER);
            server.setSoTimeout(10000);
            connected = true;
            System.out.println("Server is now 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 for client connection...");
                // Block waiting for an incoming connection.
                socket = server.accept();
                if (socket == null) continue;

...等等。当我稍后调用 server.close() 时(如果我先调用 socket.close(),我不会得到任何不同的行为),我没有收到任何错误,但 netstat 显示该端口仍在侦听。调用 ServerSocket.close() 是否足以释放该系统上的端口?

我正在为 Java 1.4.2 微版本运行时编程。还值得注意的是,我有这个方法在另一个线程中运行,并且我试图从其父线程关闭套接字。

编辑 这是来自 netstat 的行,但我可以向您保证它仍在被侦听,因为如果我再次启动 Xlet,我会收到该端口号的异常。

tcp        0      0  *.2349                 *.*                    LISTEN

I have some java code that looks similar to this:

private void startServer() throws IOException {
        URLClassLoader classloader = null;

        System.out.println("Opening server socket for listening on " + PORT_NUMBER);
        try {
            server = new ServerSocket(PORT_NUMBER);
            server.setSoTimeout(10000);
            connected = true;
            System.out.println("Server is now 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 for client connection...");
                // Block waiting for an incoming connection.
                socket = server.accept();
                if (socket == null) continue;

...and so on and so forth. When I call server.close() later on (I don't get any different behavior if I call socket.close() first), I don't get any errors, but netstat shows that the port is still being listened on. Should calling ServerSocket.close() be sufficient enough to free up the port on this system?

I am programming for a Java 1.4.2 microedition runtime. It is also worthy to note that I have this method being run in another thread, and I am trying to close the socket from its parent thread.

EDIT Here is the line from netstat, though I can assure you it is still being listened on, since if I start the Xlet again I get an exception with that port number.

tcp        0      0  *.2349                 *.*                    LISTEN

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿萝 2024-12-02 05:54:40

有几件事需要考虑。其中之一是通过以下来自 ServerSocket 的 JavaDoc 的引用来描述的

公共无效setReuseAddress(布尔值)
抛出 SocketException

启用/禁用
SO_REUSEADDR 套接字选项。当 TCP 连接关闭时
连接可能会在一段时间内保持超时状态
连接已关闭(通常称为 TIME_WAIT 状态或
2MSL 等待状态)。对于使用众所周知的套接字地址的应用程序
或端口可能无法将套接字绑定到所需的端口
SocketAddress 如果有连接处于超时状态涉及
套接字地址或端口。

因此,在您 close() 服务器套接字之后,操作系统仍然可以显示正在发生某些事情,这是可以接受的。但是,如果您要频繁打开/关闭同一端口上的服务器套接字,则可能会遇到问题。

There are several things to consider. One of them is described by the following quotation from JavaDoc of ServerSocket

public void setReuseAddress(boolean on)
throws SocketException

Enable/disable the
SO_REUSEADDR socket option. When a TCP connection is closed the
connection may remain in a timeout state for a period of time after
the connection is closed (typically known as the TIME_WAIT state or
2MSL wait state). For applications using a well known socket address
or port it may not be possible to bind a socket to the required
SocketAddress if there is a connection in the timeout state involving
the socket address or port.

So it is kind of OK that the OS can still show that there is something going on after you close() the server socket. But if you going to open/close a server socket on the same port frequently you might hit a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文