note 109888 added to sockets.examples

From: Date: Thu, 30 Aug 2012 19:25:54 +0000
Subject: note 109888 added to sockets.examples
Groups: php.notes 
Request: Send a blank email to php-notes+get-190284@lists.php.net to get a copy of this message
You can easily extend the first example to handle any number of connections instead of jsut one #!/usr/bin/env php <?php error_reporting(E_ALL); /* Permitir al script esperar para conexiones. */ set_time_limit(0); /* Activar el volcado de salida implícito, así veremos lo que estamo obteniendo * mientras llega. */ ob_implicit_flush(); $address = '127.0.0.1'; $port = 10000; if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "\n"; } if (socket_bind($sock, $address, $port) === false) { echo "socket_bind() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n"; } if (socket_listen($sock, 5) === false) { echo "socket_listen() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n"; } //clients array $clients = array(); do { $read = array(); $read[] = $sock; $read = array_merge($read,$clients); // Set up a blocking call to socket_select if(socket_select($read,$write = NULL, $except = NULL, $tv_sec = 5) < 1) { // SocketServer::debug("Problem blocking socket_select?"); continue; } // Handle new Connections if (in_array($sock, $read)) { if (($msgsock = socket_accept($sock)) === false) { echo "socket_accept() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n"; break; } $clients[] = $msgsock; $key = array_keys($clients, $msgsock); /* Enviar instrucciones. */ $msg = "\nBienvenido al Servidor De Prueba de PHP. \n" . "Usted es el cliente numero: {$key[0]}\n" . "Para salir, escriba 'quit'. Para cerrar el servidor escriba 'shutdown'.\n"; socket_write($msgsock, $msg, strlen($msg)); } // Handle Input foreach ($clients as $key => $client) { // for each client if (in_array($client, $read)) { if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) { echo "socket_read() falló: razón: " . socket_strerror(socket_last_error($client)) . "\n"; break 2; } if (!$buf = trim($buf)) { continue; } if ($buf == 'quit') { unset($clients[$key]); socket_close($client); break; } if ($buf == 'shutdown') { socket_close($client); break 2; } $talkback = "Cliente {$key}: Usted dijo '$buf'.\n"; socket_write($client, $talkback, strlen($talkback)); echo "$buf\n"; } } } while (true); socket_close($sock); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 190.183.222.194 ---- Manual Page -- http://www.php.net/manual/en/sockets.examples.php Edit -- https://master.php.net/note/edit/109888 Del: integrated -- https://master.php.net/note/delete/109888/integrated Del: useless -- https://master.php.net/note/delete/109888/useless Del: bad code -- https://master.php.net/note/delete/109888/bad+code Del: spam -- https://master.php.net/note/delete/109888/spam Del: non-english -- https://master.php.net/note/delete/109888/non-english Del: in docs -- https://master.php.net/note/delete/109888/in+docs Del: other reasons-- https://master.php.net/note/delete/109888 Reject -- https://master.php.net/note/reject/109888 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190284) next »