note 43155 added to ref.sockets

From: Date: Fri, 11 Jun 2004 09:35:14 +0000
Subject: note 43155 added to ref.sockets
Groups: php.notes 
Request: Send a blank email to php-notes+get-70922@lists.php.net to get a copy of this message
A little example that shows how to implement a simple multi-client iterative server (without forking). Launch the server and connect multiple telnet clients on it. It's a basic chat program. #!/usr/bin/php -q <?php error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); $address = '127.0.0.1'; $port = 8888; function handle_client($allclient, $socket, $buf, $bytes) { foreach($allclient as $client) { socket_write($client, "$socket wrote: $buf"); } } if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) { echo "socket_create() failed: reason: " . socket_strerror($master) . "\n"; } socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1); if (($ret = socket_bind($master, $address, $port)) < 0) { echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n"; } if (($ret = socket_listen($master, 5)) < 0) { echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n"; } $read_sockets = array($master); while (true) { $changed_sockets = $read_sockets; $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL); foreach($changed_sockets as $socket) { if ($socket == $master) { if (($client = socket_accept($master)) < 0) { echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n"; continue; } else { array_push($read_sockets, $client); } } else { $bytes = socket_recv($socket, $buffer, 2048, 0); if ($bytes == 0) { $index = array_search($socket, $read_sockets); unset($read_sockets[$index]); socket_close($socket); } else { $allclients = $read_sockets; array_shift($allclients); // remove master handle_client($allclients, $socket, $buffer, $bytes); } } } } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.sockets.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43155 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43155&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43155&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#70922) next »