套接字程序能够连接到仍处于 TIME_WAIT 状态的端口

发布于 2024-09-24 21:08:56 字数 2250 浏览 1 评论 0原文

  1. 我写了一个非常简单的套接字服务器。
  2. 它在 63254 后监听。
  3. 首先我做了一个 socket_create、socket_bind、socket_listen,所以这里有一个连接正在监听。
  4. 然后在一个循环中我做套接字接受。所以这里再听一下。
  5. 读取函数读取直到我输入退出。
  6. 之后,socket_accept 的资源 id 将关闭。
  7. 然后主连接关闭。

当我在关闭所有连接后在 TCPview 中检查此进程时,我仍然可以看到系统进程在 63254 后显示 TIME_WAIT,

如果我再次运行它正在连接的套接字服务器程序,并且当一个完整进程结束时,所有连接都会关闭并且程序终止现在我可以看到同一端口的另一个 TIME_WAIT。但我仍然可以第三次连接到同一个端口。

在stackover问题的答案中,据说处于等待状态的端口无法建立连接。

我打开 Firefox 浏览器,它打开了 4 个连接。 当我关闭时,它全部关闭,系统进程显示4次等待2分钟。 所有时间等待停留2分钟然后消失。

所以我的结论是,每次连接关闭都会发生等待,并且无法避免。

我阅读了堆栈溢出流程中的许多帖子,但仍然不确定。

我在命令行中运行以下代码。

我的服务器代码

<?
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush(); 

$str = '';
$buff = '';

$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$s)die('Unable to create socket');

if(!socket_bind($s,'127.0.0.1',63254))
    die("\nTrying to Bind: ".socket_strerror(socket_last_error()));

if(!socket_listen($s,1))
    die(socket_strerror(socket_last_error()));

    while(1)
    {
        $acc = socket_accept($s);
        if(!$acc)die(socket_strerror(socket_last_error()));
//      echo "\n".gettype($acc);
        if(!$acc)die(socket_strerror(socket_last_error()));

        while(1)
        {
            $str = socket_read($acc,512);
            $buff.= $str;
            echo $str;
//          echo '::'.gettype($str);

            if($str===false)die(socket_strerror(socket_last_error()));
            if($str=="exit\r\n")break;          
        }

//      if(!socket_shutdown($acc,2))echo socket_strerror(socket_last_error());  
        socket_close($acc);     
        if(preg_match('/exit/',$buff))break;
    }
//echo "\nConnection closed by server\n";   
//if(!socket_shutdown($s,2))echo socket_strerror(socket_last_error());
socket_close($s);
?>

客户端代码

<?
    set_time_limit(0);
    $f = fsockopen('127.0.0.1',63254,$a,$b,10);
    if(!$f)die('cannot connect');
    echo "\nConnected: \n";
    do{
        $buff = fgets(STDIN);   
        fwrite($f,$buff);
    }while($buff!="exit\r\n");
    fclose($f);
?>

需要建议来改进更好的客户端服务器(如果这还不够)。这段代码只是一个孩子的游戏。只是想了解沟通的运作方式。

  1. I have written a very simple socket server.
  2. It listens in post 63254.
  3. First i did a socket_create, socket_bind, socket_listen so here a connection is listening.
  4. Then in a loop i do the socket accpet. so here another listen.
  5. the read function reads untill i input exit.
  6. after that the resource id by socket_accept closes.
  7. and then the main connection closes.

when i checked this process in TCPview after closing all connections i can still see the system process showing TIME_WAIT for post 63254

if i again run the socket server program it is connecting and when one full process is over all the connection is closed and the program terminated and now i can see another TIME_WAIT for the same port. but still i could connect to the same port the third time.

in stackover question answer it is said that connection cannot be done for port which is in wait state.

I opened firefox browser it opened 4 connections.
when i closed it all closed and the system process showed 4 time waits for 2 minutes.
all time wait stays for 2 minutes and disappears.

so what i conclude is for every connection close the time wait is occurs and cannot be avoided.

i read many posts in stack overflow flow but still wasn't sure of it.

i run the following code in command line.

My server Code

<?
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush(); 

$str = '';
$buff = '';

$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$s)die('Unable to create socket');

if(!socket_bind($s,'127.0.0.1',63254))
    die("\nTrying to Bind: ".socket_strerror(socket_last_error()));

if(!socket_listen($s,1))
    die(socket_strerror(socket_last_error()));

    while(1)
    {
        $acc = socket_accept($s);
        if(!$acc)die(socket_strerror(socket_last_error()));
//      echo "\n".gettype($acc);
        if(!$acc)die(socket_strerror(socket_last_error()));

        while(1)
        {
            $str = socket_read($acc,512);
            $buff.= $str;
            echo $str;
//          echo '::'.gettype($str);

            if($str===false)die(socket_strerror(socket_last_error()));
            if($str=="exit\r\n")break;          
        }

//      if(!socket_shutdown($acc,2))echo socket_strerror(socket_last_error());  
        socket_close($acc);     
        if(preg_match('/exit/',$buff))break;
    }
//echo "\nConnection closed by server\n";   
//if(!socket_shutdown($s,2))echo socket_strerror(socket_last_error());
socket_close($s);
?>

The client code

<?
    set_time_limit(0);
    $f = fsockopen('127.0.0.1',63254,$a,$b,10);
    if(!$f)die('cannot connect');
    echo "\nConnected: \n";
    do{
        $buff = fgets(STDIN);   
        fwrite($f,$buff);
    }while($buff!="exit\r\n");
    fclose($f);
?>

need suggestions to improve a better client server if this is not sufficient. this code is just a child's play. just trying to understand the way communication works.

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

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

发布评论

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

评论(1

-小熊_ 2024-10-01 21:08:56

在stackover问题的答案中是
说无法连接
对于处于等待状态的端口。

我不知道您指的是什么答案,但您无法绑定到处于 TIME_WAIT 状态的端口。如果您是服务器,您可以使用 setReuseAddress() 来克服这个问题。如果您是客户端,则必须等待,或者使用不同的出站端口,或者最好根本不指定出站端口,让系统找到一个。您是服务器,因此这不适用于您。

我打开firefox浏览器它打开了4
连接。当我把它全部关闭时
关闭,系统进程显示4
时间等待2分钟。所有时间
等待 2 分钟,然后
消失。

但这些是客户端端口。出站端口。在您的服务器上,它们是入站端口,并且同一端口号上还有一个侦听端口。只要有监听端口,入站连接就可以成功。

所以我的结论是对于每个
连接关闭等待时间是
发生且无法避免。

当您是最先发送关闭消息的一方时,就会发生 TIME_WAIT。如果您是收到关闭消息并响应关闭的一端,则您的端口根本不会进入 TIME_WAIT。

In stackover question answer it is
said that connection cannot be done
for port which is in wait state.

I don't know what answer you're referring to, but you cannot bind to a port which is in TIME_WAIT state. If you are a server you can use setReuseAddress() to overcome this. If you're a client you have to wait, or use a different outbound port, or best of all don't specify an outbound port at all, let the system find one. You are a server so this doesn't apply to you.

I opened firefox browser it opened 4
connections. when i closed it all
closed and the system process showed 4
time waits for 2 minutes. all time
wait stays for 2 minutes and
disappears.

But those are client ports. Outbound ports. At your server they were inbound ports, and there was also a listening port on the same port number. As long as there is a listening port, an inbound connnection can succeed.

so what i conclude is for every
connection close the time wait is
occurs and cannot be avoided.

TIME_WAIT occurs when you are the end that sends the close first. If you are the end that received the close, and closed in response, your port doesn't go into TIME_WAIT at all.

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