porton
porton

Reputation: 5805

Bug in PHP sockets? It does not obey timeout

I start the following server:

<?php

$socket = socket_create(AF_INET, SOCK_STREAM,  SOL_TCP);

socket_bind($socket, 'localhost', 9000) or die("Failed: socket_bind()");
socket_listen($socket, 20)              or die("Failed: socket_listen()");

$client = socket_accept($socket);

socket_set_option($client, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>1, 'usec'=>0));
socket_set_option($client, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>1, 'usec'=>0));

while(1) {
  $read = array($client);
  $write = $except = null;
  socket_select($read,$write,$except,1);
}

When I try connect to this server with telnet localhost 9000 it does not exit after a second, despite I have set 1 sec timeout.

PHP 7.0.11 on Linux. There is a similar problem with PHP 5.6.21 on FreeBSD

Why does it not work?

Note: In this example I try to decrease the timeout. In my real problem I need to increase it to a few hours or maybe few days.

Upvotes: 1

Views: 202

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123260

There is no reason it should exit. As far as I understand your code:

  • You are setting the timeout for read and write operations, but you don't do any read or writes in the code shown.
  • You are calling select with a timeout of 1 and once select is done you call it again etc in an endless loop.

Upvotes: 1

Related Questions