Sockets: new socket_select() example

From: Date: Fri, 08 Mar 2002 20:07:23 +0000
Subject: Sockets: new socket_select() example
References: 1 2 3 4 5  Groups: php.dev 
Request: Send a blank email to php-dev+get-80945@lists.php.net to get a copy of this message
Due to popular request, here is an example of the new socket_select(): NOTE: ***This code does not do any error handling*** <?php error_reporting(E_ALL); function http_connect($addr) { $sock=socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp")); socket_connect($sock, $addr, 80); return $sock; } function http_send_get($sock, $page) { socket_send($sock, "GET /$page HTTP/1.0\n\n",strlen($page)+20,0); } function process_readable($sock, $key) { print "Reading Socket: $sock\n"; } function process_writable($sock, $key) { print "Writing to Socket: $sock\n"; } /* Connect to 4 websites */ $sock1=http_connect("www.yahoo.com"); $sock2=http_connect("www.cnn.com"); $sock3=http_connect("www.msn.com"); $sock4=http_connect("www.php.net"); /* Send a request only 2 and 4 */ http_send_get($sock2,""); http_send_get($sock4,""); sleep(1); $rfds=array($sock1, $sock2, $sock3, $sock4); $wfds=array($sock1, $sock2, $sock3, $sock4); $ret=socket_select($rfds, $wfds, NULL, 1); print "$ret Total sockets ready for action\n"; print count($rfds) . " Readable:\n"; array_walk($rfds, 'process_readable'); print count($wfds) . " Writable:\n"; array_walk($wfds, 'process_writable'); ?>

« previous php.dev (#80945) next »