1

I'm coding a php socket server script which is running on ubuntu.

It works well, clients can connect/disconnect to this server. But the server is shutting down silently when a client connect to this server that didn't have any client connected for a long time.

I setted up timeout to 0 (unlimited) but the server is stoping without any errors or messages... like this.

root@exfl:/home/projectap/sec/server_core# php socket_server.php
[exfl.kr] Starting AbsolutePitch socket server...
[2014.06.21 16:50:58] Server is listening. PID: 6442
[2014.06.21 17:28:48] Client [192.168.0.1:8795] connected to the server.
root@exfl:/home/projectap/sec/server_core#

I coded for this server like this.

<?php
set_time_limit(0);
error_reporting(E_ALL);
ini_set("memory_limit","2048M");
ini_set("max_execution_time", "0");

if(posix_getpid() == true) { 
    echo "[exfl.kr] Starting AbsolutePitch socket server...\n"; 
} else {
    echo getTimeToStr()."Server is already running.\n"; 
}

$cSock = array();
$socketsInformation = array();
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$bind = socket_bind($socket, "0.0.0.0", "24180");

$listen = socket_listen($socket, 5);
echo getTimeToStr()."Server is listening. PID: ".posix_getpid()."\n"; 

while(true) {

    $sockArr = array_merge(array($socket), $cSock);
    if(socket_select($sockArr, $tWrite = null, $tExcept = null, null) > 0) {
        foreach($sockArr as $sock){

            if($sock == $socket){

                $tSock = socket_accept($socket);
                socket_getpeername($tSock, $sockIp, $sockPort);
                array_push($cSock, $tSock);
                array_push($socketsInformation, array("SOCKETINDEX"=>(count($cSock)-1), "IP"=>$sockIp, "USERID"=>"", "STATUS"=>"", "AUTH"=>false));
                echo getTimeToStr()."Client [".$sockIp.":".$sockPort."] connected to the server.\n";
            } else {
                ...
            }
        }
    }
}

echo getTimeToStr()."Server closed.\n";
...
?>

When the server stops suddenly, it isn't outputing a "Server closed" message..

Please help.

Sorry for my bad English.

Ex.
  • 11
  • 3

1 Answers1

0

You may want to try socket_set_options() with SO_KEEPALIVE. To know more about your options use the socket_get_options page:

https://www.php.net/manual/en/function.socket-get-option.php

To know more about the usage of socket_set_options() use:

https://www.php.net/manual/en/function.socket-set-option.php

Konkret
  • 991
  • 9
  • 14