java 使用服务器套接字后如何关闭端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6147481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:32:53  来源:igfitidea点击:

How to close port after using server sockets

javasocketsserversocket

提问by dan

In my application I create a ServerSocketinstance with some port. After I am finished, I close the socket, but when I try to make a new ServerSocketon the same port, it throws:

在我的应用程序中,我创建了一个ServerSocket带有一些端口的实例。完成后,我关闭了套接字,但是当我尝试ServerSocket在同一个端口上创建一个新端口时,它抛出:

"java.net.BindException: Address already in use"

If I create the ServerSocketwith a different port, then it works.

如果我ServerSocket使用不同的端口创建它,那么它就可以工作。

ServerSocket.isClosedalso returns true

ServerSocket.isClosed也返回真

What is the problem?

问题是什么?

public void run() {
    try {
        BufferedInputStream bufferedinputstream = new BufferedInputStream(
                                                        new FileInputStream(fileReq));
        BufferedOutputStream outStream = new BufferedOutputStream(
                                                        cs.getOutputStream());
        byte buffer[] = new byte[1024];

        int read;
        System.out.println(cs);
        while ((read = bufferedinputstream.read(buffer)) != -1)
        {
            outStream.write(buffer, 0, read);
            outStream.flush();
        }
        System.out.println("File transfered");
        outStream.close();
        bufferedinputstream.close();

        try {
            this.finalize();
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
} catch (Exception e) {
    System.out.println("Exce....");
    System.out.println(e.getMessage());
}
finally
{
    if ( cs != null)
        try {
            int usedPort=cs.getLocalPort();
            System.out.println("Closing "+cs);
            cs.close();
            System.out.println(cs+" Closed");
            System.out.println("asd"+cs.isClosed());
            portManager.getInstance().mp.put(usedPort,true);
        } catch (IOException e) {
            System.err.println("in sendToClient can't close sockt");
            System.err.println(e.getMessage());
        }
}

回答by Code Painters

Have you tried calling setReuseAddress(true)on the server socket? It's normal for the TCP state machine to enter TIME_WAITstate. For detailed explanation look here.

您是否尝试过调用setReuseAddress(true)服务器套接字?TCP状态机进入TIME_WAIT状态是正常的。详细解释请看这里

回答by Jér?me Verstrynge

It is because there is a cooldown period implemented on some O.S. If you wait a little (a couple of minutes for example), then you should be able to access the port again.

这是因为在某些操作系统上实施了冷却时间 如果您稍等(例如几分钟),那么您应该能够再次访问该端口。

That being said, it is not a good idea to open and close serversockets on a port. It is better to open it and keep it open as long as necessary. Then close it when you are done. Don't open/close/open/close/open/close...

话虽如此,在端口上打开和关闭服务器套接字并不是一个好主意。最好打开它并在必要时保持打开状态。然后完成后关闭它。不要打开/关闭/打开/关闭/打开/关闭...

回答by AjayR

Just close the socket when done. It automatically shuts down the port it is listening on. Never keep open the socket (port) when you are done.

完成后关闭套接字即可。它会自动关闭它正在侦听的端口。完成后切勿保持打开套接字(端口)。

回答by Vasman

This means that the server is already open you need a check for user input or client input that says close the server like

这意味着服务器已经打开,您需要检查用户输入或客户端输入,表示关闭服务器,例如

while( bufferedinputstream != "quit" )

try{

...

}

Once you have the server open its just open until something closes it.

一旦您打开服务器,它就会打开,直到某些东西将其关闭。