让socket服务器在CentOS上运行,以便在php中监听客户端请求

I have a client and server program i do not have an idea of how to start the server automatically on my CENTOS server and the server should keep on running irrespective of my browser is closed, and it keeps on listening to client request. Please Help

Server.php

<?php
// set some variables
$host = "XX.XX.XXX.XX";
$port = 25763;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket
");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket
");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener
");

// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection
");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input
");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "
";
echo $spawn;
socket_write($spawn, $output, strlen ($output)) or die("Could not write output
");
// close sockets
socket_close($spawn);
socket_close($socket);
?>

Client.php

<?php
// where is the socket server?
$host    = "XX.XX.XXX.XX";
$port    = 25763;
$message = "Hello Server This is the first message to the server";
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket
");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server
");  
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server
");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response
");
echo "Reply From Server  :".$result;
// close socket
socket_close($socket);
    // print result to browser
?>

Generally, you should start .php file with exec(), for example:

exec('/usr/bin/php /var/www/directory/server/event_server.php');

Next thing, put your socket_accept() (althoug, I used different scheme, with socket_select()) in infinite loop, and dont forget about delay - this is very important, as you don't want to hang your CPU:

$broadcast_start = microtime(true);

while(TRUE) {
    socket_accept($socket);

    //Do stuff...

    if( round(microtime(true) - $broadcast_start, 1) < 0.5 ) {
        usleep(500000);
    }
}

But actually, your subbjects is really complex. You should first search the web for solutions, which are plenty.