From b2d6dfc5809889ca58dbab80ab8406603a80fa59 Mon Sep 17 00:00:00 2001 From: Egon Schmid Date: Tue, 12 Sep 2000 19:58:00 +0000 Subject: [PATCH] Added socket_get_status() and some formating. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@32587 c90b9560-bf6c-de11-be94-00142212c4b1 --- functions/sockets.xml | 205 +++++++++++++++++++++++------------------- 1 file changed, 111 insertions(+), 94 deletions(-) diff --git a/functions/sockets.xml b/functions/sockets.xml index 3b2fedd21d..b67cadb4b7 100644 --- a/functions/sockets.xml +++ b/functions/sockets.xml @@ -1,33 +1,30 @@ Socket functions Sockets + The socket extension implements a low-level interface to the socket communication functions, providing the possibility to act as a socket server as well as a client. - The socket functions described here are part of an extension to PHP which must be enabled at compile time by giving the option to configure. - For a more generic client-side socket interface, see fsockopen and pfsockopen. - When using these functions, it is important to remember that while many of them have identical names to their C counterparts, they often have different declarations. Please be sure to read the descriptions to avoid confusion. - That said, those unfamiliar with socket programming can still find a lot of useful material in the appropriate Unix man pages, and @@ -35,7 +32,6 @@ programming in C on the web, much of which can be applied, with slight modifications, to socket programming in PHP. - Socket example: Simple TCP/IP server @@ -48,61 +44,60 @@ setup). Anything you type will then be output on the server side, and echoed back to you. To disconnect, enter 'quit'. - + <?php -error_reporting(E_ALL); +error_reporting (E_ALL); /* Allow the script to hang around waiting for connections. */ -set_time_limit(0); +set_time_limit (0); $address = '192.168.1.53'; $port = 10000; -if (($sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - echo "socket() failed: reason: " . strerror($sock) . "\n"; +if (($sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) { + echo "socket() failed: reason: " . strerror ($sock) . "\n"; } -if (($ret = bind($sock, $address, $port)) < 0) { - echo "bind() failed: reason: " . strerror($ret) . "\n"; +if (($ret = bind ($sock, $address, $port)) < 0) { + echo "bind() failed: reason: " . strerror ($ret) . "\n"; } -if (($ret = listen($sock, 5)) < 0) { - echo "listen() failed: reason: " . strerror($ret) . "\n"; +if (($ret = listen ($sock, 5)) < 0) { + echo "listen() failed: reason: " . strerror ($ret) . "\n"; } do { if (($msgsock = accept_connect($sock)) < 0) { - echo "accept_connect() failed: reason: " . strerror($msgsock) . "\n"; + echo "accept_connect() failed: reason: " . strerror ($msgsock) . "\n"; break; } do { $buf = ''; - $ret = read($msgsock, $buf, 2048); + $ret = read ($msgsock, $buf, 2048); if ($ret < 0) { - echo "read() failed: reason: " . strerror($ret) . "\n"; + echo "read() failed: reason: " . strerror ($ret) . "\n"; break 2; } if ($ret == 0) { break 2; } - $buf = trim($buf); + $buf = trim ($buf); if ($buf == 'quit') { - close($msgsock); + close ($msgsock); break 2; } $talkback = "PHP: You said '$buf'.\n"; - write($msgsock, $talkback, strlen($talkback)); + write ($msgsock, $talkback, strlen ($talkback)); echo "$buf\n"; } while (true); - close($msgsock); + close ($msgsock); } while (true); -close($sock); +close ($sock); ?> - Socket example: Simple TCP/IP client @@ -113,26 +108,26 @@ close($sock); <?php -error_reporting(E_ALL); +error_reporting (E_ALL); echo "<h2>TCP/IP Connection</h2>\n"; /* Get the port for the WWW service. */ -$service_port = getservbyname('www', 'tcp'); +$service_port = getservbyname ('www', 'tcp'); /* Get the IP address for the target host. */ -$address = gethostbyname('www.php.net'); +$address = gethostbyname ('www.php.net'); /* Create a TCP/IP socket. */ -$socket = socket(AF_INET, SOCK_STREAM, 0); +$socket = socket (AF_INET, SOCK_STREAM, 0); if ($socket < 0) { - echo "socket() failed: reason: " . strerror($socket) . "\n"; + echo "socket() failed: reason: " . strerror ($socket) . "\n"; } else { - "socket() successful: " . strerror($socket) . "\n"; + "socket() successful: " . strerror ($socket) . "\n"; } echo "Attempting to connect to '$address' on port '$service_port'..."; -$result = connect($socket, $address, $service_port); +$result = connect ($socket, $address, $service_port); if ($result < 0) { echo "connect() failed.\nReason: ($result) " . strerror($result) . "\n"; } else { @@ -143,28 +138,27 @@ $in = "HEAD / HTTP/1.0\r\n\r\n"; $out = ''; echo "Sending HTTP HEAD request..."; -write($socket, $in, strlen($in)); +write ($socket, $in, strlen ($in)); echo "OK.\n"; echo "Reading response:\n\n"; -while (read($socket, $out, 2048)) { +while (read ($socket, $out, 2048)) { echo $out; } echo "Closing socket..."; -close($socket); +close ($socket); echo "OK.\n\n"; ?> - accept_connect - Accepts a connection on a socket. + Accepts a connection on a socket Description @@ -174,7 +168,6 @@ echo "OK.\n\n"; int socket - After the socket socket has been created using socket, bound to a name with @@ -191,7 +184,6 @@ echo "OK.\n\n"; set_nonblock, an error code will be returned. - The socket descriptor returned by accept_connect may not be used to accept new @@ -199,30 +191,28 @@ echo "OK.\n\n"; socket, however, remains open and may be reused. - Returns a new socket descriptor on success, or a negative error code on failure. This code may be passed to strerror to get a textual explanation of the error. - See also bind, connect, listen, - socket, and + socket, + socket_get_status, and strerror. - bind - Binds a name to a socket. + Binds a name to a socket Description @@ -236,14 +226,12 @@ echo "OK.\n\n"; - bind binds the name given in address to the socket described by socket, which must be a valid socket descriptor created with socket. - The address parameter is either an IP address in dotted-quad notation @@ -252,36 +240,33 @@ echo "OK.\n\n"; Unix-domain socket, if the socket family is AF_UNIX. - The port parameter is only used when connecting to an AF_INET socket, and designates the port on the remote host to which a connection should be made. - Returns zero on success, or a negative error code on failure. This code may be passed to strerror to get a textual explanation of the error. - See also accept_connect, connect, listen, - socket, and + socket, + socket_get_status, and strerror. - close - Closes a file descriptor. + Closes a file descriptor Description @@ -291,12 +276,10 @@ echo "OK.\n\n"; int socket - close closes the file (or socket) descriptor given by socket. - Note that close should not be used on PHP file descriptors created with fopen, @@ -305,24 +288,23 @@ echo "OK.\n\n"; with socket or accept_connect. - Returns true on success, or false if an error occurs (i.e., socket is invalid). - See also bind, listen, - socket, and strerror. + socket, + socket_get_status, and + strerror. - connect - Initiates a connection on a socket. + Initiates a connection on a socket Description @@ -336,13 +318,11 @@ echo "OK.\n\n"; - Initiates a connection using the socket descriptor socket, which must be a valid socket descriptor created with socket. - The address parameter is either an IP address in dotted-quad notation @@ -351,35 +331,32 @@ echo "OK.\n\n"; Unix-domain socket, if the socket family is AF_UNIX. - The port parameter is only used when connecting to an AF_INET socket, and designates the port on the remote host to which a connection should be made. - Returns zero on success, or a negative error code on failure. This code may be passed to strerror to get a textual explanation of the error. - See also bind, listen, - socket, and + socket, + socket_get_status, and strerror. - listen - Listens for a connection on a socket. + Listens for a connection on a socket Description @@ -390,7 +367,6 @@ echo "OK.\n\n"; int backlog - After the socket socket has been created using socket and bound to a name with @@ -399,35 +375,32 @@ echo "OK.\n\n"; backlog incoming connections will be queued for processing. - listen is applicable only to sockets with type SOCK_STREAM or SOCK_SEQPACKET. - Returns zero on success, or a negative error code on failure. This code may be passed to strerror to get a textual explanation of the error. - See also accept_connect, bind, connect, - socket, and + socket, + socket_get_status, and strerror. - socket - Create a socket (endpoint for communication). + Create a socket (endpoint for communication) Description @@ -439,18 +412,15 @@ echo "OK.\n\n"; int protocol - Creates a communication endpoint (a socket), and returns a descriptor to the socket. - The domain parameter sets the domain. Currently, AF_INET and AF_UNIX are understood. - The type parameter selects the socket type. This is one of SOCK_STREAM, @@ -459,24 +429,77 @@ echo "OK.\n\n"; SOCK_RAW, SOCK_RDM, or SOCK_PACKET. - protocol sets the protocol. - Returns a valid socket descriptor on success, or a negative error code on failure. This code may be passed to strerror to get a textual explanation of the error. - For more information on the usage of socket, as well as on the meanings of the various parameters, see the - Unix man page socket (2). + Unix man page socket (2). + + See also + accept_connect, + bind, + connect, + listen, + strerror, and + socket_get_status. + + + + + + socket_get_status + + Returns information about existing socket resource + + + + Description + + + array socket_get_status + resource + socket_get_status + + + + + Returns information about existing socket resource. Currently + returns 4 entries in the result array: + + + + + timed_out (bool) - The socket timed out + waiting for data + + + + + blocked (bool) - The socket was blocked + + + + + eof (bool) - Indicates EOF event + + + + + unread_bytes (bool) - Number of bytes + left in the socket buffer + + + See also accept_connect, @@ -485,14 +508,13 @@ echo "OK.\n\n"; listen, and strerror. - strerror - Return a string describing a socket error. + Return a string describing a socket error Description @@ -502,7 +524,6 @@ echo "OK.\n\n"; int errno - strerror takes as its errno parameter the return value of one of @@ -513,22 +534,20 @@ echo "OK.\n\n"; just pass it to strerror, and it tells you what happened. - <function>strerror</function> example - + <?php -if (($socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - echo "socket() failed: reason: " . strerror($socket) . "\n"; +if (($socket = socket (AF_INET, SOCK_STREAM, 0)) < 0) { + echo "socket() failed: reason: " . strerror ($socket) . "\n"; } -if (($ret = bind($socket, '127.0.0.1', 80)) < 0) { - echo "bind() failed: reason: " . strerror($ret) . "\n"; +if (($ret = bind ($socket, '127.0.0.1', 80)) < 0) { + echo "bind() failed: reason: " . strerror ($ret) . "\n"; } ?> - The expected output from the above example (assuming the script is not run with root privileges): @@ -536,19 +555,17 @@ if (($ret = bind($socket, '127.0.0.1', 80)) < 0) { bind() failed: reason: Permission denied - - See also accept_connect, bind, connect, - listen, and - socket. + listen, + socket, and + socket_get_status. -