The following is a simple message processing server implemented through PHP's socket extension module: bind to a local port, listen for client connections, receive data and forward it to all clients except the sender
socket_server.php
foreach($read as $read_socket){
$data=@socket_read($read_socket,1024,PHP_NORMAL_READ);
if($data===false){
//If not fetched To the data, it means that the client has disconnected
$ key = array_search ($ read_sockets, $ clients);
unset ($ clients [$ key]);
echo "client disconnectd.n"; continue;
}
$data=trim($data);
if(!empty($data)){
foreach($clients as $write_socket){
// Exclude the server and itself, and then send the data to all other clients
if($write_socket==$socket||$write_socket==$read_socket){
continue;
}
socket_write ($write_socket, "$datan" ); :
zhxia@zhxia-pc:~/sh/php$ ./socket_server.php
listen on port 9981...
Connected via telnet:
http://www.bkjia.com/PHPjc/327639.html
www.bkjia.com
true
TechArticle