1

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.

porton
  • 5,214
  • 11
  • 47
  • 95
  • possibly relevant: http://stackoverflow.com/questions/2441479/php-using-declare-what-is-a-tick – Marc B Oct 19 '16 at 21:13

1 Answers1

1

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.
Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172