note 73509 added to function.socket-read

From: Date: Tue, 27 Feb 2007 00:22:56 +0000
Subject: note 73509 added to function.socket-read
Groups: php.notes 
Request: Send a blank email to php-notes+get-123913@lists.php.net to get a copy of this message
Hello, Here is a working solution for using socket_read in nonblocking mode for PHP_NORMAL_MODE read type, as it has some problems in standard PHP. The function socket_normal_read will read from an unlimited number of sockets in PHP_NORMAL_MODE read type. If you plan to use this function for an application that uses a lot of sockets that are always opening and closing, you might want to improve it to delete the respective records from $sockets and $queues static variables when you close a socket that you will not use anymore. <?php define ("LINE_END", "\n"); function socket_normal_read ($socket, $length) { static $sockets = array (); static $queues = array (); static $sock_num = 0; for ($i = 0; isset ($sockets[$i]) && $socket != $sockets[$i]; $i++); if (!isset ($sockets[$i])) { $sockets [$sock_num] = $socket; $queues [$sock_num++] = ""; } $recv = socket_read ($socket, $length, PHP_BINARY_READ); if ($recv === "") { if (strpos ($queues[$i], LINE_END) === false) return false; } else if ($recv !== false) { $queues[$i] .= $recv; } $pos = strpos ($queues[$i], LINE_END); if ($pos === false) return ""; $ret = substr ($queues[$i], 0, $pos); $queues[$i] = substr ($queues[$i], $pos+2); return $ret; } $sock1 = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); $sock2 = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); $sock3 = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); $sock4 = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); socket_connect ($sock1, "127.0.0.1", 4551); socket_connect ($sock2, "127.0.0.1", 4552); socket_connect ($sock3, "127.0.0.1", 4553); socket_connect ($sock4, "127.0.0.1", 4554); socket_set_nonblock ($sock1); socket_set_nonblock ($sock2); socket_set_nonblock ($sock3); socket_set_nonblock ($sock4); while (1) { usleep (1000000); $x = socket_normal_read ($sock1, 4096); if ($x !== false && strlen ($x)) echo "1: $x\n"; $x = socket_normal_read ($sock2, 4096); if ($x !== false && strlen ($x)) echo "2: $x\n"; $x = socket_normal_read ($sock3, 4096); if ($x !== false && strlen ($x)) echo "3: $x\n"; $x = socket_normal_read ($sock4, 4096); if ($x !== false && strlen ($x)) echo "4: $x\n"; } ?> To test this simple application, just use netcat (nc -vv -l 127.0.0.1 455x) to open 4 listening sockets. After you run this script, write some lines in each of those netcat sessions. I hope this will help people that tried to use test-based socket connections in PHP using the sockets library. I wait for feedback at the address specified. Thank you. ---- Server IP: 69.147.83.197 Probable Submitter: 89.41.253.173 ---- Manual Page -- http://www.php.net/manual/en/function.socket-read.php Edit -- https://master.php.net/note/edit/73509 Del: integrated -- https://master.php.net/note/delete/73509/integrated Del: useless -- https://master.php.net/note/delete/73509/useless Del: bad code -- https://master.php.net/note/delete/73509/bad+code Del: spam -- https://master.php.net/note/delete/73509/spam Del: non-english -- https://master.php.net/note/delete/73509/non-english Del: in docs -- https://master.php.net/note/delete/73509/in+docs Del: other reasons-- https://master.php.net/note/delete/73509 Reject -- https://master.php.net/note/reject/73509 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123913) next »