# Socket server in PHP hangs

**URL:** <https://forum.kirupa.com/t/socket-server-in-php-hangs/267581>\
**Category:** Uncategorized\
**Created:** [August 3, 2008, 9:38am UTC](https://forum.kirupa.com/t/socket-server-in-php-hangs/267581 "2008-08-03T09:38:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![shubh](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@shubh](https://forum.kirupa.com/u/shubh)\
**Post date:** [August 3, 2008, 9:38am UTC](https://forum.kirupa.com/t/socket-server-in-php-hangs/267581/1 "2008-08-03T09:38:51Z")

</div>

Hi,

I have a chat server written in PHP which support multiple users. It works fine. But, when I leave the server idle, that is no user sends any message for some time say 5 mins. Then server appears to be hanged. Not fully hanged though. It’s able to accept new user but it doesn’t broadcast messages. I am guessing somekind of timeout is happening. Although I have set no time out to happen in socket\_select() . sockt\_read() function is called to read character by character. Below is the main part of the code…

```php

$s_client == array ();
$abort = false;
     while (!$abort) 
       $set = array_merge((array)$s_listen, $s_clients);
       //$set[0] = $s_listen;
       //foreach(
       //if (socket_select($set, $write = NULL, $expect = NULL, 1, 0) > 0) {
       if (socket_select($set, $write = NULL, $expect = NULL, $tv_sec=NULL) > 0) {
 
         foreach ($set as $sock) {
           /* listening socket has a connection, deal with it */
           if ($sock == $s_listen) {
             if (!($newc = socket_accept($s_listen)))
               myerror();
             else {
               /* add socket to client list and announce connection */
               echo "Adding a new socket...
";
               $s_clients[$newc]= $newc;
               socket_getpeername($newc, $addr);
               $cdata[$newc]['addr'] = $addr;
               $cdata[$newc]['buf'] = '';
               $cdata[$newc]['state'] = STATE_NICKNAME;
               echo "[connection]: $addr
";
               send_single($newc,'############Welcome to Indiwin Chat############');
               send_single($newc, 'Enter a nickname:');
             }
           }
           else {
             if (($read = socket_read($sock, 1024)) === false || $read == '') {
               if ($read != '')
                 myerror();
               else /* no error, but connection was closed, so tell everyone */
                 on_quit($sock);
               /* remove client from arrays */
               unset($s_clients[$sock]);
               unset($cdata[$sock]);
             }
             else {
               /* only want data with a newline */
               if (strchr($read, "
") === false)
//...rest part fo code not shown

```

I am using PUTTY, or dos telnet client to interact with server. I assume that these client insert ’  
\r’ after we hit enter.

Any idea why does server hangs when left idle for approx 5 minutes?

Thank you  
Shubhankar
