简体   繁体   English

关闭客户端和服务器后的孤立TCP连接

[英]Orphaned TCP connections after closing client and server

I implemented a client server application in java and it works fine . 我用Java实现了一个客户端服务器应用程序,它运行良好。 Now if i do a netstat -a in the shell to see the active connections i see that there are alot of orphaned connections of the following form 现在,如果我在外壳中执行netstat -a来查看活动的连接,我会看到有许多以下形式的孤立连接

TCP 127.0.0.1:54725 Sin-Host:0 LISTENING

For example if i create a server on port 58185 like below 例如,如果我在端口58185上创建服务器,如下所示

TCP 127.0.0.1:58185 Sin-Host:0 LISTENING

When a client connects to it i get two connections like as follows 当客户端连接到它时,我得到两个连接,如下所示

TCP 127.0.0.1:58185 Sin-Host:0 LISTENING
TCP 127.0.0.1:58185 Sin-Host:58187 ESTABLISHED
TCP 127.0.0.1:58187 Sin-Host:58185 ESTABLISHED

Now when i close both my client and server the first connection remains ( namely in LISTENING state not TIMED_WAIT ) 现在,当我同时关闭客户端和服务器时,第一个连接仍然保留(即处于LISTENING状态而不是TIMED_WAIT

I think there is no reason for the first connection to remain when i have closed both the client and server . 我认为当我同时关闭客户端和服务器时,没有必要保留第一个连接。 Is this a bug ? 这是一个错误吗? It doesnt really affect my program but i just want to know so that i know its impact . 它并没有真正影响我的程序,但是我只想知道,以便知道它的影响。

**EDIT : How long do these orphans last**

I rebooted my system and all the orphans are gone . 我重新启动系统,所有孤儿都消失了。 I repeated the test and see that orphans remain even after application shut down but go away after system reboot . 我重复了测试,发现即使应用程序关闭后,孤儿仍然存在,但在系统重启后消失了。

Maybe i guess this is some system settings that prevents it from going in the first place .? 也许我猜这是一些系统设置,阻止它首先进行。

As long as you have a line like 只要你有一条线像

serverSocket.accept();

in you code the connection with LISTENING will remain. 在您的代码中,与LISTENING的连接将保留。

The accept() leads to the LISTENING state of the port. accept()导致端口的LISTENING状态。

Both of the ESTABLSHED lines are for the active connection. 两条ESTABLSHED线均用于活动连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM