调用ServerSocket.close()是否足以关闭端口? [英] Is calling ServerSocket.close() sufficient enough to close a port?

查看:768
本文介绍了调用ServerSocket.close()是否足以关闭端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类似于这样的java代码:

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;

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

...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?

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

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 这是来自netstat的行,虽然我可以向你保证它仍在被监听,因为如果我再次启动Xlet,我会得到一个带有该端口号的异常。

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


推荐答案

有几件事需要考虑。其中一个由来自ServerSocket的JavaDoc的以下引用描述

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

public void setReuseAddress(boolean on) throws SocketException

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

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.

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

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.

这篇关于调用ServerSocket.close()是否足以关闭端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆