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

发布于 2024-11-09 17:21:58 字数 1780 浏览 1 评论 0原文

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

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

如果我使用不同的端口创建 ServerSocket ,然后就可以了。

ServerSocket.isClosed 也返回 true

有什么问题?

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());
        }
}

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

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

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

ServerSocket.isClosed also returns true

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());
        }
}

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

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

发布评论

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

评论(4

等风来 2024-11-16 17:21:58

您是否尝试过在服务器套接字上调用 setReuseAddress(true) ? TCP状态机进入TIME_WAIT状态是正常的。有关详细说明,请参阅此处

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

以为你会在 2024-11-16 17:21:58

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

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

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

魂ガ小子 2024-11-16 17:21:58

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

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.

固执像三岁 2024-11-16 17:21:58

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

while( bufferedinputstream != "quit" )

try{

...

}

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

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.

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