Heru S
Heru S

Reputation: 1323

PHP socket script causes many and long CLOSE_WAIT socket connection

I have a PHP script (which I found online) that runs infinitely listening to a port and if there is a connection made to this port it will establish a TCP connection. However, when I ran this script and there are many connections (around 500), the number of CLOSE_WAIT connections increase. The remote device that is connected in this state is not able to connect again because the CLOSE_WAIT did not terminate.

// port info
$host = "0.0.0.0";
$port = 10260;
$pos = 1;

// don't timeout!
set_time_limit(0);
record("START");

$sock = socket_create(AF_INET, SOCK_STREAM, 0);
$timeout = array('sec'=>3000,'usec'=>0);
$try = socket_set_option($sock,SOL_SOCKET,SO_RCVTIMEO,$timeout);

// Bind the socket to the address/port
if(!socket_bind($sock, $host, $port))
{
    echo socket_last_error() ;
    die('Could not bind to address');
}
record("SOCKET BIND OK");

// start listening for connections
$result = socket_listen($sock, 1024) or die("Could not set up socket listener\n");
record("SOCKET LISTEN OK");
$clients = array($sock);


// infinite while loop
while(1)
{

    // Setup clients listen socket for reading
    $read = $clients;

    $e = NULL;

    if (socket_select($read, $write = NULL, $except = NULL, 0,0) < 1) 
    {
       continue;
    }

    /* if a new read ready is being made add it to the client array */

    if (in_array($sock, $read)) {
        record("NEW CONNECTION");
        $clients[$pos] = $newsock = socket_accept($sock);
        $curpos = $pos;
        $pos++;
        socket_getpeername($newsock, $ip,$port);
        record("Incoming IP: {$ip} PORT: {$port}");
        // remove the listening socket from the clients-with-data array
        $key = array_search($sock, $read);
        unset($read[$key]);

    } // end if in_array

    // loop through all the clients that have data to read from
    foreach ($read as $read_key => $read_sock) {
        // read until newline or 1024 bytes
        // socket_read while show errors when the client is disconnected, so silence the error messages
        $key = $read_key;
        $fulldata = $data = @socket_read($read_sock, 1024);

        // check if the client is disconnected
        if ($data === false) {
            // remove client for $clients array
            $key = array_search($read_sock, $clients);
            socket_close($read_sock);
            unset($clients[$key]);

            record("NO DATA");

            // continue to the next client to read from, if any
            continue;
        }


        // .. do something with $data ...

    }


}

socket_close($sock);    
record("END");
die("DONE");

I tried to use socket_close() in the code but to no avail. CLOSE_WAIT seems to take longer to move to the next state.

Upvotes: 1

Views: 2864

Answers (1)

a4c8b
a4c8b

Reputation: 925

I found two problems in your code:

First, the 4th parameter(tv_sec) of socket_select should be NULL instead of 0. As described in the Manual, 0 causes high CPU load in a useless, endless loop, while NULL will block until anything happen:

tv_sec may be zero , causing socket_select() to return immediately. This is useful for polling. If tv_sec is NULL (no timeout), socket_select() can block indefinitely.

The second, and I think this is the Reason for the CLOSE_WAIT is your test if $data is false. This only works on a gracefull closed connection, but as one comment here says, you also shold test on en empty string. So change the one line to:

if (socket_select($read, $write = NULL, $except = NULL, NULL, 0) < 1) 

and the other to:

// check if the client is disconnected
if ($data === false || $data === '') {

and you should be fine

Upvotes: 3

Related Questions