PHP socket inactivity does not work

时间:2016-10-20 13:05:08

标签: php sockets tcp freebsd

PHP on FreeBSD, a fragment of a TCP server code:

  protected function connected ($user) {
  //     stream_set_timeout($user->socket, 7200); // does not work: http://php.net/manual/en/function.stream-set-timeout.php
    socket_set_option($user->socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>7200, 'usec'=>0));
    socket_set_option($user->socket, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>7200, 'usec'=>0));
    socket_set_option($user->socket, SOL_SOCKET, SO_KEEPALIVE, 1);
    socket_set_option($user->socket, SOL_SOCKET, 256/*TCP_KEEPIDLE*/, 7200*1000);
    socket_set_option($user->socket, SOL_SOCKET, 256/*TCP_KEEPIDLE*/, 7200*1000);
  }

But this code does not help: the connection is broken after about 5 min of inactivity.

You see that I want it break after 7200 sec of inactivity.

Please help!

It seems that I understand why SO_RCVTIMEO and SO_SNDTIMEO do not work: they are about read/write timeouts not inactivity timeouts. But why the rest doesn't work?

Don't suggest to "ping" it manually, this solution was not accepted by my boss.

0 个答案:

没有答案