Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

socket_bind(3) [php man page]

SOCKET_BIND(3)								 1							    SOCKET_BIND(3)

socket_bind - Binds a name to a socket

SYNOPSIS
bool socket_bind (resource $socket, string $address, [int $port]) DESCRIPTION
Binds the name given in $address to the socket described by $socket. This has to be done before a connection is be established using socket_connect(3) or socket_listen(3). PARAMETERS
o $socket - A valid socket resource created with socket_create(3). o $address - If the socket is of the AF_INET family, the $address is an IP in dotted-quad notation (e.g. 127.0.0.1). If the socket is of the AF_UNIX family, the $address is the path of a Unix-domain socket (e.g. /tmp/my.sock). o $port (Optional) - The $port parameter is only used when binding an AF_INET socket, and designates the port on which to listen for connections. RETURN VALUES
Returns TRUE on success or FALSE on failure. The error code can be retrieved with socket_last_error(3). This code may be passed to socket_strerror(3) to get a textual explanation of the error. EXAMPLES
Example #1 Using socket_bind(3) to set the source address <?php // Create a new socket $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // An example list of IP addresses owned by the computer $sourceips['kevin'] = '127.0.0.1'; $sourceips['madcoder'] = '127.0.0.2'; // Bind the source address socket_bind($sock, $sourceips['madcoder']); // Connect to destination address socket_connect($sock, '127.0.0.1', 80); // Write $request = 'GET / HTTP/1.1' . " " . 'Host: example.com' . " "; socket_write($sock, $request); // Close socket_close($sock); ?> NOTES
Note This function must be used on the socket before socket_connect(3). Note Windows 9x/ME compatibility note: socket_last_error(3) may return an invalid error code if trying to bind the socket to a wrong address that does not belong to your machine. SEE ALSO
socket_connect(3), socket_listen(3), socket_create(3), socket_last_error(3), socket_strerror(3). PHP Documentation Group SOCKET_BIND(3)

Check Out this Related Man Page

SOCKET_CREATE_PAIR(3)							 1						     SOCKET_CREATE_PAIR(3)

socket_create_pair - Creates a pair of indistinguishable sockets and stores them in an array

SYNOPSIS
bool socket_create_pair (int $domain, int $type, int $protocol, array &$fd) DESCRIPTION
socket_create_pair(3) creates two connected and indistinguishable sockets, and stores them in $fd. This function is commonly used in IPC (InterProcess Communication). PARAMETERS
o $domain - The $domain parameter specifies the protocol family to be used by the socket. See socket_create(3) for the full list. o $type - The $type parameter selects the type of communication to be used by the socket. See socket_create(3) for the full list. o $protocol - The $protocol parameter sets the specific protocol within the specified $domain to be used when communicating on the returned socket. The proper value can be retrieved by name by using getprotobyname(3). If the desired protocol is TCP, or UDP the corre- sponding constants SOL_TCP, and SOL_UDP can also be used. See socket_create(3) for the full list of supported protocols. o $fd - Reference to an array in which the two socket resources will be inserted. RETURN VALUES
Returns TRUE on success or FALSE on failure. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.3.0 | | | | | | | This function is now re-enabled on Windows plat- | | | forms. | | | | | 4.3.0 | | | | | | | This function was due to a bug made unavailable | | | on Windows platforms. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 socket_create_pair(3) example <?php $sockets = array(); /* On Windows we need to use AF_INET */ $domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX); /* Setup socket pair */ if (socket_create_pair($domain, SOCK_STREAM, 0, $sockets) === false) { echo "socket_create_pair failed. Reason: ".socket_strerror(socket_last_error()); } /* Send and Recieve Data */ if (socket_write($sockets[0], "ABCdef123 ", strlen("ABCdef123 ")) === false) { echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($sockets[0])); } if (($data = socket_read($sockets[1], strlen("ABCdef123 "), PHP_BINARY_READ)) === false) { echo "socket_read() failed. Reason: ".socket_strerror(socket_last_error($sockets[1])); } var_dump($data); /* Close sockets */ socket_close($sockets[0]); socket_close($sockets[1]); ?> Example #2 socket_create_pair(3) IPC example <?php $ary = array(); $strone = 'Message From Parent.'; $strtwo = 'Message From Child.'; if (socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $ary) === false) { echo "socket_create_pair() failed. Reason: ".socket_strerror(socket_last_error()); } $pid = pcntl_fork(); if ($pid == -1) { echo 'Could not fork Process.'; } elseif ($pid) { /*parent*/ socket_close($ary[0]); if (socket_write($ary[1], $strone, strlen($strone)) === false) { echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($ary[1])); } if (socket_read($ary[1], strlen($strtwo), PHP_BINARY_READ) == $strtwo) { echo "Recieved $strtwo "; } socket_close($ary[1]); } else { /*child*/ socket_close($ary[1]); if (socket_write($ary[0], $strtwo, strlen($strtwo)) === false) { echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($ary[0])); } if (socket_read($ary[0], strlen($strone), PHP_BINARY_READ) == $strone) { echo "Recieved $strone "; } socket_close($ary[0]); } ?> SEE ALSO
socket_create(3), socket_create_listen(3), socket_bind(3), socket_listen(3), socket_last_error(3), socket_strerror(3). PHP Documentation Group SOCKET_CREATE_PAIR(3)
Man Page