Queryspace - How sockets are used in PHP ?
 
     
 Home > Computers and Internet
Previous Next


anoop

Open Question

Oct 17, 2006, 3:58 am
How sockets are used in PHP ? 
 

   
   

Answers  (1)


k7
Dec 11, 2006,3:07 am
Socket Functions
<br>Introduction
<br>The socket extension implements a low-level interface to the socket communication functions based on the popular BSD sockets, providing the possibility to act as a socket server as well as a client.
<br>
<br>1. Stream_socket_client
<br>(PHP 5)
<br>
<br>stream_socket_client -- Open Internet or Unix domain socket connection
<br>Description
<br>resource stream_socket_client ( string remote_socket [, int &errno [, string &errstr [, float timeout [, int flags [, resource context]]]]] )
<br>
<br>
<br>Initiates a stream or datagram connection to the destination specified by remote_socket. The type of socket created is determined by the transport specified using standard URL formatting: transport://target. For Internet Domain sockets (AF_INET) such as TCP and UDP, the target portion of the remote_socket parameter should consist of a hostname or IP address followed by a colon and a port number. For Unix domain sockets, the target portion should point to the socket file on the filesystem. The optional timeout can be used to set a timeout in seconds for the connect system call. flags is a bitmask field which may be set to any combination of connection flags. Currently the selection of connection flags is limited to STREAM_CLIENT_CONNECT (default), STREAM_CLIENT_ASYNC_CONNECT and STREAM_CLIENT_PERSISTENT.
<br>
<br>Note: If you need to set a timeout for reading/writing data over the socket, use stream_set_timeout(), as the timeout parameter to stream_socket_client() only applies while connecting the socket.
<br>
<br>stream_socket_client() returns a stream resource which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()).
<br>
<br>If the call fails, it will return FALSE and if the optional errno and errstr arguments are present they will be set to indicate the actual system level error that occurred in the system-level connect() call. If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket. Note that the errno and errstr arguments will always be passed by reference.
<br>
<br>Depending on the environment, the Unix domain or the optional connect timeout may not be available. A list of available transports can be retrieved using stream_get_transports(). See Appendix O for a list of built in transports.
<br>
<br>The stream will by default be opened in blocking mode. You can switch it to non-blocking mode by using stream_set_blocking(). Example 1. stream_socket_client() Example
<br>
<br>
<br>
<br>The example below shows how to retrieve the day and time from the UDP service "daytime" (port 13) in your own machine. Example 2. Using UDP connection
<br>
<br>
<br>
<br>
<br>
<br>
<br>Warning
<br>UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.
<br>
<br>
<br>
<br>Note: When specifying a numerical IPv6 address (e.g. fe80::1) you must enclose the IP in square brackets. For example, tcp://[fe80::1]:80.
<br>
<br>
<br>2.stream_socket_server
<br>(PHP 5)
<br>
<br>stream_socket_server -- Create an Internet or Unix domain server socket
<br>Description
<br>resource stream_socket_server ( string local_socket [, int &errno [, string &errstr [, int flags [, resource context]]]] )
<br>
<br>
<br>Creates a stream or datagram socket on the specified local_socket. The type of socket created is determined by the transport specified using standard URL formatting: transport://target. For Internet Domain sockets (AF_INET) such as TCP and UDP, the target portion of the remote_socket parameter should consist of a hostname or IP address followed by a colon and a port number. For Unix domain sockets, the target portion should point to the socket file on the filesystem. flags is a bitmask field which may be set to any combination of socket creation flags. The default value of flags is STREAM_SERVER_BIND | STREAM_SERVER_LISTEN.
<br>
<br>Note: For UDP sockets, you must use STREAM_SERVER_BIND as the flags parameter.
<br>
<br>This function only creates a socket, to begin accepting connections use stream_socket_accept().
<br>
<br>If the call fails, it will return FALSE and if the optional errno and errstr arguments are present they will be set to indicate the actual system level error that occurred in the system-level socket(), bind(), and listen() calls. If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the bind() call. This is most likely due to a problem initializing the socket. Note that the errno and errstr arguments will always be passed by reference.
<br>
<br>Depending on the environment, Unix domain sockets may not be available. A list of available transports can be retrieved using stream_get_transports(). See Appendix O for a list of bulitin transports.
<br>
<br>Example 1. Using TCP server sockets
<br>
<br>
<br>
<br>
<br>
<br>The example below shows how to act as a time server which can respond to time queries as shown in an example on stream_socket_client().
<br>
<br>Note: Most systems require root access to create a server socket on a port below 1024.
<br>
<br>Example 2. Using UDP server sockets
<br>
<br>
<br>
<br>
<br>
<br>Note: When specifying a numerical IPv6 address (e.g. fe80::1) you must enclose the IP in square brackets. For example, tcp://[fe80::1]:80.
<br>
<br>See also stream_socket_client(), stream_set_blocking(), stream_set_timeout(), fgets(), fgetss(), fwrite(), fclose(), feof(), and the Curl extension.
<br>
<br>
<br>
<br>
<br> Some Notes : Neil Munro
<br>stream_socket_server
<br>06-Jul-2006 04:26
<br>If you want a high speed socket server, use the low-level sockets instead (socket_create/bind/listen). The stream_socket_server version appears to have internal fixed 8k buffers that will overflow if you don't keep up by reading.
<br>
<br>This is a serious problem if you an application that reads the socket for messages and then, say, saves the result in a database. The delay while it is busy processing means you can't read the data in time unless you get involved in muti-threading.
<br>
<br>With the the low-level functions, the OS quietly buffers TCP/IP packets so there is no problem (tested on Windows XP Professional).
<br>e at osterman dot com
<br>26-May-2006 10:36
<br>Example "Hello World!" SSL HTTP Server. Note, if you don't use a signed ssl certificate, your browser will give you a warning.
<br>
<br>
<br>
<br>Notes from - andrey at php dot net
<br>08-Aug-2004 12:02
<br>Just a small example how to use this function and also stream_select() to make a server that accepts more than one connections (can have many clients connected):
<br>In master we hold all opened connections. Just before calling stream select we copy the array to $read and then pass it ot stream_select(). In case that we may read from at least one socket, $read will contain socket descriptors. $master is needed not to lose references to the opened connections we have.
<br>stream_server.php :
<br>
<br>stream_client.php:
<br>
<br>
<br>
<br>For a more generic client-side socket interface, see stream_socket_client(), stream_socket_server(), fsockopen(), and pfsockopen().
<br>
<br>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.
<br>
<br>Those unfamiliar with socket programming can find a lot of useful material in the appropriate Unix man pages, and there is a great deal of tutorial information on socket programming in C on the web, much of which can be applied, with slight modifications, to socket programming in PHP. The Unix Socket FAQ might be a good start.
<br>
<br>Requirements
<br>No external libraries are needed to build this extension.
<br>
<br>Installation
<br>The socket functions described here are part of an extension to PHP which must be enabled at compile time by giving the --enable-sockets option to configure.
<br>
<br>Note: IPv6 Support was added with PHP 5.0.0.
<br>
<br>Runtime Configuration
<br>This extension has no configuration directives defined in php.ini.
<br>
<br>Resource Types
<br>This extension has no resource types defined.
<br>
<br>Predefined Constants
<br>The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.
<br>
<br>