【问题标题】:make the server wait the [FIN + ACK] from the client before closing the socket让服务器在关闭套接字之前等待来自客户端的 [FIN + ACK]
【发布时间】:2015-06-23 05:25:10
【问题描述】:

我有以下 php 脚本(tcp 服务器)

<?php

$sock = socket_create(AF_INET, SOCK_STREAM, 0);

// Bind the socket to an address/port
socket_bind($sock, "0.0.0.0", 14010) or die('Could not bind to address');
$i=0;
for(;;) {
    // Start listening for connections
    socket_listen($sock);

    /* Accept incoming requests and handle them as child processes */
    $client = socket_accept($sock);

    // Read the input from the client &#8211; 1024 bytes
    $in = socket_read($client, 41984);

    socket_write($client, $in);

    //socket_close($client);
}

// Close the master sockets

socket_close($sock);
?>

如何让服务器在关闭套接字之前等待来自客户端的 [FIN + ACK]?

【问题讨论】:

  • 您只想listen 一次(在循环外)然后accept 多次。

标签: php sockets networking tcp


【解决方案1】:

您可以不调用close,而是在套接字上执行recv(不确定php 构造)。 recv 系统调用在对等关闭时返回 0。如果是这种情况,您可以在服务器端使用close

【讨论】:

    猜你喜欢
    • 2015-02-26
    • 2022-01-05
    • 2021-03-20
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 2018-09-02
    • 2019-06-09
    相关资源
    最近更新 更多