= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // OFFLINE | socket_addrinfo_bind() - PHP_7 >= PHP_7_2_0, PHP_8 // OFFLINE | socket_addrinfo_connect() - PHP_7 >= PHP_7_2_0, PHP_8 // OFFLINE | socket_addrinfo_explain() - PHP_7 >= PHP_7_2_0, PHP_8 // OFFLINE | socket_addrinfo_lookup() - PHP_7 >= PHP_7_2_0, PHP_8 // socket_bind() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_clear_error() - PHP_4 >= PHP_4_2_0, PHP_5, PHP_7, PHP_8 // socket_close() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // OFFLINE | socket_cmsg_space() - PHP_5 >= PHP_5_5_0, PHP_7, PHP_8 // socket_connect() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_create_listen() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_create_pair() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_create() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // OFFLINE | socket_export_stream() - PHP_7 >= PHP_7_0_7, PHP_8 // socket_get_option() - PHP_4 >= PHP_4_3_0, PHP_5, PHP_7, PHP_8 // socket_getopt() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_getpeername() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_getsockname() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // OFFLINE | socket_import_stream() - PHP_5 >= PHP_5_4_0, PHP_7, PHP_8 // socket_last_error() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_listen() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_read() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_recv() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_recvfrom() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // OFFLINE | socket_recvmsg() - PHP_5 >= PHP_5_5_0, PHP_7, PHP_8 // socket_select() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_send() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // OFFLINE | socket_sendmsg() - PHP_5 >= PHP_5_5_0, PHP_7, PHP_8 // socket_sendto() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_set_block() - PHP_4 >= PHP_4_2_0, PHP_5, PHP_7, PHP_8 // socket_set_nonblock() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_set_option() - PHP_4 >= PHP_4_3_0, PHP_5, PHP_7, PHP_8 // socket_setopt() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_shutdown() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_strerror() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // socket_write() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // OFFLINE | socket_wsaprotocol_info_export() - PHP_7 >= PHP_7_3_0, PHP_8 // OFFLINE | socket_wsaprotocol_info_import() - PHP_7 >= PHP_7_3_0, PHP_8 // OFFLINE | socket_wsaprotocol_info_release() - PHP_7 >= PHP_7_3_0, PHP_8 // ============================== USING CLASSES (0) // ============================== USING DATA_TYPES (9) // OFFLINE | Socket - PHP_8 // false // OFFLINE | AddressInfo - PHP_8 // array // string // bool // int // void // resource // ============================== END // REQUIREMENTS // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS // ============================== ABOUT // PHP Manual / Function Reference / Other Services / Sockets // URL: https://www.php.net/manual/en/book.sockets.php // ============================== DESCRIPTION // SOCKETS // // SOCKETS - BEGIN // Sockets // // INTRODUCTION // INSTALLING_CONFIGURING // PREDEFINED_CONSTANTS // EXAMPLES // SOCKET_ERRORS // SOCKET_FUNCTIONS // THE_SOCKET_CLASS // THE_ADDRESSINFO_CLASS // // INTRODUCTION - BEGIN // Introduction // // 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. // For a more generic client-side socket interface, see stream_socket_client(), stream_socket_server(), 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. // 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. // // LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/intro.sockets.php // INTRODUCTION - END // // INSTALLING_CONFIGURING - BEGIN // Installing/Configuring // // REQUIREMENTS // INSTALLATION // RUNTIME_CONFIGURATION // RESOURCE_TYPES // // REQUIREMENTS - BEGIN // Requirements // // No external libraries are needed to build this extension. // // LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/sockets.requirements.php // REQUIREMENTS - END // // INSTALLATION - BEGIN // Installation // // 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. // // LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/sockets.installation.php // INSTALLATION - END // // RUNTIME_CONFIGURATION - BEGIN // Runtime Configuration // // This extension has no configuration directives defined in php.ini. // // LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/sockets.configuration.php // RUNTIME_CONFIGURATION - END // // RESOURCE_TYPES - BEGIN // Resource Types // // Prior to PHP 8.0.0, socket_accept(), socket_import_stream(), socket_addrinfo_connect(), socket_addrinfo_bind(), socket_create_listen(), socket_wsaprotocol_info_import() and socket_create() returned Socket resources. // Prior to PHP 8.0.0, socket_addrinfo_lookup() returned an array of AddressInfo resources. // // LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/sockets.resources.php // RESOURCE_TYPES - END // // LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/sockets.setup.php // INSTALLING_CONFIGURING - END // // PREDEFINED_CONSTANTS - BEGIN // Predefined Constants // // 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. // AF_UNIX (int) - // AF_INET (int) - // AF_INET6 (int) - Only available if compiled with IPv6 support. // SOCK_STREAM (int) - // SOCK_DGRAM (int) - // SOCK_RAW (int) - // SOCK_SEQPACKET (int) - // SOCK_RDM (int) - // MSG_OOB (int) - // MSG_WAITALL (int) - // MSG_PEEK (int) - // MSG_DONTROUTE (int) - // MSG_EOR (int) - Not available on Windows platforms. // MSG_EOF (int) - Not available on Windows platforms. // MSG_ZEROCOPY (int) - Available as of PHP 8.2.0 // SO_DEBUG (int) - // SO_REUSEADDR (int) - // SO_REUSEPORT (int) - This constant is only available on platforms that support the SO_REUSEPORT socket option: this includes Linux, macOS and *BSD, but does not include Windows. // SO_KEEPALIVE (int) - // SO_DONTROUTE (int) - // SO_LINGER (int) - // SO_BROADCAST (int) - // SO_OOBINLINE (int) - // SO_SNDBUF (int) - // SO_RCVBUF (int) - // SO_SNDLOWAT (int) - // SO_RCVLOWAT (int) - // SO_SNDTIMEO (int) - // SO_RCVTIMEO (int) - // SO_TYPE (int) - // SO_ERROR (int) - // SO_ZEROCOPY (int) - Available as of PHP 8.2.0 // TCP_NODELAY (int) - Used to disable Nagle TCP algorithm. // TCP_KEEPCNT (int) - Available as of PHP 8.2.0 // TCP_KEEPIDLE (int) - Available as of PHP 8.2.0 // TCP_KEEPINTVL (int) - Available as of PHP 8.2.0 // TCP_KEEPALIVE (int) - Available as of PHP 8.2.0 // TCP_NOTSENT_LOWAT (int) - Available as of PHP 8.2.0 // SO_MARK (int) - Available as of PHP 8.1.0 // SO_USER_COOKIE (int) - Available as of PHP 8.1.0 // SO_RTABLE (int) - Available as of PHP 8.2.0 // SO_ACCEPTFILTER (int) - Available as of PHP 8.1.0 // SO_DONTTRUNC (int) - Available as of PHP 8.1.0 // SO_WANTMORE (int) - Available as of PHP 8.1.0 // SO_INCOMING_CPU (int) - Available as of PHP 8.2.0 // SO_MEMINFO (int) - Available as of PHP 8.2.0 // SO_BPF_EXTENSIONS (int) - Available as of PHP 8.2.0 // SO_SETFIB (int) - Available as of PHP 8.2.0 // SOL_FILTER (int) - Available as of PHP 8.2.0 // TCP_DEFER_ACCEPT (int) - Available as of PHP 8.1.0 // TCP_CONGESTION (int) - Available as of PHP 8.2.0 // SOL_SOCKET (int) - // PHP_NORMAL_READ (int) - // PHP_BINARY_READ (int) - // SOL_TCP (int) - // SOL_UDP (int) - // // The following constants are defined under Windows and UNIX-like platforms. Each constant is only defined if their equal is available on the platform. // SOCKET_EINTR (int) - Interrupted system call. // SOCKET_EBADF (int) - Bad file descriptor number. // SOCKET_EACCES (int) - Permission denied. // SOCKET_EFAULT (int) - Bad address. // SOCKET_EINVAL (int) - Invalid argument. // SOCKET_EMFILE (int) - Too many open files. // SOCKET_ENAMETOOLONG (int) - File name too long. // SOCKET_ENOTEMPTY (int) - Directory not empty. // SOCKET_ELOOP (int) - Too many symbolic links encountered. // SOCKET_EWOULDBLOCK (int) - Operation would block. // SOCKET_EREMOTE (int) - Object is remote. // SOCKET_EUSERS (int) - Too many users. // SOCKET_ENOTSOCK (int) - Socket operation on non-socket. // SOCKET_EDESTADDRREQ (int) - Destination address required. // SOCKET_EMSGSIZE (int) - Message too long. // SOCKET_EPROTOTYPE (int) - Protocol wrong type for socket. // SOCKET_EPROTONOSUPPORT (int) - Protocol not supported. // SOCKET_ESOCKTNOSUPPORT (int) - Socket type not supported. // SOCKET_EOPNOTSUPP (int) - Operation not supported on transport endpoint. // SOCKET_EPFNOSUPPORT (int) - Protocol family not supported. // SOCKET_EAFNOSUPPORT (int) - Address family not supported by protocol. // SOCKET_EADDRNOTAVAIL (int) - Cannot assign requested address. // SOCKET_ENETDOWN (int) - Network is down. // SOCKET_ENETUNREACH (int) - Network is unreachable. // SOCKET_ENETRESET (int) - Network dropped connection because of reset. // SOCKET_ECONNABORTED (int) - Software caused connection abort. // SOCKET_ECONNRESET (int) - Connection reset by peer. // SOCKET_ENOBUFS (int) - No buffer space available. // SOCKET_EISCONN (int) - Transport endpoint is already connected. // SOCKET_ENOTCONN (int) - Transport endpoint is not connected. // SOCKET_ESHUTDOWN (int) - Cannot send after transport endpoint shutdown. // SOCKET_ETIMEDOUT (int) - Connection timed out. // SOCKET_ECONNREFUSED (int) - Connection refused. // SOCKET_EHOSTDOWN (int) - Host is down. // SOCKET_EHOSTUNREACH (int) - No route to host. // SOCKET_EALREADY (int) - Operation already in progress. // SOCKET_EINPROGRESS (int) - Operation now in progress. // // The following constants are only defined under Windows. // SOCKET_ENOPROTOOPT (int) - // SOCKET_EADDRINUSE (int) - // SOCKET_ETOOMYREFS (int) - // SOCKET_EPROCLIM (int) - // SOCKET_EDUOT (int) - // SOCKET_ESTALE (int) - // SOCKET_EDISCON (int) - // SOCKET_SYSNOTREADY (int) - // SOCKET_VERNOTSUPPORTED (int) - // SOCKET_NOTINITIALISED (int) - // SOCKET_HOST_NOT_FOUND (int) - // SOCKET_TRY_AGAIN (int) - // SOCKET_NO_RECOVERY (int) - // SOCKET_NO_DATA (int) - // SOCKET_NO_ADDRESS (int) - // // The following constants are only available on UNIX-like platforms. Each constant is only defined if their equal is available on the platform. // SOCKET_EPERM (int) - Operation not permitted. // SOCKET_ENOENT (int) - No such file or directory. // SOCKET_EIO (int) - I/O error. // SOCKET_ENXIO (int) - No such device or address. // SOCKET_E2BIG (int) - Arg list too long. // SOCKET_EAGAIN (int) - Try again. // SOCKET_ENOMEM (int) - Out of memory. // SOCKET_ENOTBLK (int) - Block device required. // SOCKET_EBUSY (int) - Device or resource busy. // SOCKET_EEXIST (int) - File exists. // SOCKET_EXDEV (int) - Cross-device link. // SOCKET_ENODEV (int) - No such device. // SOCKET_ENOTDIR (int) - Not a directory. // SOCKET_EISDIR (int) - Is a directory. // SOCKET_ENFILE (int) - File table overflow. // SOCKET_ENOTTY (int) - Not a typewriter. // SOCKET_ENOSPC (int) - No space left on device. // SOCKET_ESPIPE (int) - Illegal seek. // SOCKET_EROFS (int) - Read-only file system. // SOCKET_EMLINK (int) - Too many links. // SOCKET_EPIPE (int) - Broken pipe. // SOCKET_ENOLCK (int) - No record locks available. // SOCKET_ENOSYS (int) - Function not implemented. // SOCKET_ENOMSG (int) - No message of desired type. // SOCKET_EIDRM (int) - Identifier removed. // SOCKET_ECHRNG (int) - Channel number out of range. // SOCKET_EL2NSYNC (int) - Level 2 not synchronized. // SOCKET_EL3HLT (int) - Level 3 halted. // SOCKET_EL3RST (int) - Level 3 reset. // SOCKET_ELNRNG (int) - Link number out of range. // SOCKET_EUNATCH (int) - Protocol driver not attached. // SOCKET_ENOCSI (int) - No CSI structure available. // SOCKET_EL2HLT (int) - Level 2 halted. // SOCKET_EBADE (int) - Invalid exchange. // SOCKET_EBADR (int) - Invalid request descriptor. // SOCKET_EXFULL (int) - Exchange full. // SOCKET_ENOANO (int) - No anode. // SOCKET_EBADRQC (int) - Invalid request code. // SOCKET_EBADSLT (int) - Invalid slot. // SOCKET_ENOSTR (int) - Device not a stream. // SOCKET_ENODATA (int) - No data available. // SOCKET_ETIME (int) - Timer expired. // SOCKET_ENOSR (int) - Out of streams resources. // SOCKET_ENONET (int) - Machine is not on the network. // SOCKET_ENOLINK (int) - Link has been severed. // SOCKET_EADV (int) - Advertise error. // SOCKET_ESRMNT (int) - Srmount error. // SOCKET_ECOMM (int) - Communication error on send. // SOCKET_EPROTO (int) - Protocol error. // SOCKET_EMULTIHOP (int) - Multihop attempted. // SOCKET_EBADMSG (int) - Not a data message. // SOCKET_ENOTUNIQ (int) - Name not unique on network. // SOCKET_EBADFD (int) - File descriptor in bad state. // SOCKET_EREMCHG (int) - Remote address changed. // SOCKET_ERESTART (int) - Interrupted system call should be restarted. // SOCKET_ESTRPIPE (int) - Streams pipe error. // SOCKET_EPROTOOPT (int) - Protocol not available. // SOCKET_ADDRINUSE (int) - Address already in use. // SOCKET_ETOOMANYREFS (int) - Too many references: cannot splice. // SOCKET_EISNAM (int) - Is a named type file. // SOCKET_EREMOTEIO (int) - Remote I/O error. // SOCKET_EDQUOT (int) - Quota exceeded. // SOCKET_ENOMEDIUM (int) - No medium found. // SOCKET_EMEDIUMTYPE (int) - Wrong medium type. // SCM_RIGHTS (int) - Send or receive a set of open file descriptors from another process. // SCM_CREDENTIALS (int) - // SCM_CREDS (int) - Available as of PHP 8.2.0 // SCM_CREDS2 (int) - Available as of PHP 8.2.0 // LOCAL_CREDS (int) - Available as of PHP 8.2.0 // LOCAL_CREDS_PERSISTENT (int) - Available as of PHP 8.2.0 // SKF_AD_OFF (int) - Available as of PHP 8.2.0 // SKF_AD_PROTOCOL (int) - Available as of PHP 8.2.0 // SKF_AD_PKTTYPE (int) - Available as of PHP 8.2.0 // SKF_AD_IFINDEX (int) - Available as of PHP 8.2.0 // SKF_AD_NLATTR (int) - Available as of PHP 8.2.0 // SKF_AD_NLATTR_NEST (int) - Available as of PHP 8.2.0 // SKF_AD_MARK (int) - Available as of PHP 8.2.0 // SKF_AD_QUEUE (int) - Available as of PHP 8.2.0 // SKF_AD_HATYPE (int) - Available as of PHP 8.2.0 // SKF_AD_RXHASH (int) - Available as of PHP 8.2.0 // SKF_AD_CPU (int) - Available as of PHP 8.2.0 // SKF_AD_ALU_XOR_X (int) - Available as of PHP 8.2.0 // SKF_AD_VLAN_TAG (int) - Available as of PHP 8.2.0 // SKF_AD_VLAN_TAG_PRESENT (int) - Available as of PHP 8.2.0 // SKF_AD_PAY_OFFSET (int) - Available as of PHP 8.2.0 // SKF_AD_RANDOM (int) - Available as of PHP 8.2.0 // SKF_AD_VLAN_TPID (int) - Available as of PHP 8.2.0 // SKF_AD_MAX (int) - Available as of PHP 8.2.0 // // LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/sockets.constants.php // PREDEFINED_CONSTANTS - END // // EXAMPLES - BEGIN // Examples // // [example] // Example #1 Socket example: Simple TCP/IP server // This example shows a simple talkback server. Change the address and port variables to suit your setup and execute. You may then connect to the server with a command similar to: telnet 192.168.1.53 10000 (where the address and port match your setup). Anything you type will then be output on the server side, and echoed back to you. To disconnect, enter 'quit'. // [code] // #!/usr/local/bin/php -q // [php] // error_reporting(E_ALL); // // /* Allow the script to hang around waiting for connections. */ // set_time_limit(0); // // /* Turn on implicit output flushing so we see what we're getting // * as it comes in. */ // ob_implicit_flush(); // // $address = '192.168.1.53'; // $port = 10000; // // if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; // } // // if (socket_bind($sock, $address, $port) === false) { // echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; // } // // if (socket_listen($sock, 5) === false) { // echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; // } // // do { // if (($msgsock = socket_accept($sock)) === false) { // echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; // break; // } // /* Send instructions. */ // $msg = "\nWelcome to the PHP Test Server. \n" . // "To quit, type 'quit'. To shut down the server type 'shutdown'.\n"; // socket_write($msgsock, $msg, strlen($msg)); // // do { // if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) { // echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n"; // break 2; // } // if (!$buf = trim($buf)) { // continue; // } // if ($buf == 'quit') { // break; // } // if ($buf == 'shutdown') { // socket_close($msgsock); // break 2; // } // $talkback = "PHP: You said '$buf'.\n"; // socket_write($msgsock, $talkback, strlen($talkback)); // echo "$buf\n"; // } while (true); // socket_close($msgsock); // } while (true); // // socket_close($sock); // [/php] // [/code] // [/example] // [example] // Example #2 Socket example: Simple TCP/IP client // This example shows a simple, one-shot HTTP client. It simply connects to a page, submits a HEAD request, echoes the reply, and exits. // [php] // error_reporting(E_ALL); // // echo "

TCP/IP Connection

\n"; // // /* Get the port for the WWW service. */ // $service_port = getservbyname('www', 'tcp'); // // /* Get the IP address for the target host. */ // $address = gethostbyname('www.example.com'); // // /* Create a TCP/IP socket. */ // $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // if ($socket === false) { // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; // } else { // echo "OK.\n"; // } // // echo "Attempting to connect to '$address' on port '$service_port'..."; // $result = socket_connect($socket, $address, $service_port); // if ($result === false) { // echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n"; // } else { // echo "OK.\n"; // } // // $in = "HEAD / HTTP/1.1\r\n"; // $in .= "Host: www.example.com\r\n"; // $in .= "Connection: Close\r\n\r\n"; // $out = ''; // // echo "Sending HTTP HEAD request..."; // socket_write($socket, $in, strlen($in)); // echo "OK.\n"; // // echo "Reading response:\n\n"; // while ($out = socket_read($socket, 2048)) { // echo $out; // } // // echo "Closing socket..."; // socket_close($socket); // echo "OK.\n\n"; // [/php] // [/example] // // LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/sockets.examples.php // EXAMPLES - END // // SOCKET_ERRORS - BEGIN // Socket Errors // // The socket extension was written to provide a usable interface to the powerful BSD sockets. Care has been taken that the functions work equally well on Win32 and Unix implementations. Almost all of the sockets functions may fail under certain conditions and therefore emit an E_WARNING message describing the error. Sometimes this doesn't happen to the desire of the developer. For example the function socket_read() may suddenly emit an E_WARNING message because the connection broke unexpectedly. It's common to suppress the warning with the @-operator and catch the error code within the application with the socket_last_error() function. You may call the socket_strerror() function with this error code to retrieve a string describing the error. See their description for more information. // Note: // The E_WARNING messages generated by the socket extension are in English though the retrieved error message will appear depending on the current locale (LC_MESSAGES): // Warning - socket_bind() unable to bind address [98]: Die Adresse wird bereits verwendet // // LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/sockets.errors.php // SOCKET_ERRORS - END // // SOCKET_FUNCTIONS - BEGIN // Socket Functions // // Table of Contents // * socket_accept - Accepts a connection on a socket // * socket_addrinfo_bind - Create and bind to a socket from a given addrinfo // * socket_addrinfo_connect - Create and connect to a socket from a given addrinfo // * socket_addrinfo_explain - Get information about addrinfo // * socket_addrinfo_lookup - Get array with contents of getaddrinfo about the given hostname // * socket_bind - Binds a name to a socket // * socket_clear_error - Clears the error on the socket or the last error code // * socket_close - Closes a Socket instance // * socket_cmsg_space - Calculate message buffer size // * socket_connect - Initiates a connection on a socket // * socket_create_listen - Opens a socket on port to accept connections // * socket_create_pair - Creates a pair of indistinguishable sockets and stores them in an array // * socket_create - Create a socket (endpoint for communication) // * socket_export_stream - Export a socket into a stream that encapsulates a socket // * socket_get_option - Gets socket options for the socket // * socket_getopt - Alias of socket_get_option // * socket_getpeername - Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type // * socket_getsockname - Queries the local side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type // * socket_import_stream - Import a stream // * socket_last_error - Returns the last error on the socket // * socket_listen - Listens for a connection on a socket // * socket_read - Reads a maximum of length bytes from a socket // * socket_recv - Receives data from a connected socket // * socket_recvfrom - Receives data from a socket whether or not it is connection-oriented // * socket_recvmsg - Read a message // * socket_select - Runs the select() system call on the given arrays of sockets with a specified timeout // * socket_send - Sends data to a connected socket // * socket_sendmsg - Send a message // * socket_sendto - Sends a message to a socket, whether it is connected or not // * socket_set_block - Sets blocking mode on a socket // * socket_set_nonblock - Sets nonblocking mode for file descriptor fd // * socket_set_option - Sets socket options for the socket // * socket_setopt - Alias of socket_set_option // * socket_shutdown - Shuts down a socket for receiving, sending, or both // * socket_strerror - Return a string describing a socket error // * socket_write - Write to a socket // * socket_wsaprotocol_info_export - Exports the WSAPROTOCOL_INFO Structure // * socket_wsaprotocol_info_import - Imports a Socket from another Process // * socket_wsaprotocol_info_release - Releases an exported WSAPROTOCOL_INFO Structure // // LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/ref.sockets.php // SOCKET_FUNCTIONS - END // // THE_SOCKET_CLASS - BEGIN // The Socket class // // PHP_8 // // Introduction // A fully opaque class which replaces Socket resources as of PHP 8.0.0. // // Class synopsis // [code] // final class Socket { // } // [/code] // // LITERATURE_SOURCES // * PHP_NET (2023-11-11) // URL: https://www.php.net/manual/en/class.socket.php // THE_SOCKET_CLASS - END // // THE_ADDRESSINFO_CLASS - BEGIN // The AddressInfo class // // PHP_8 // // Introduction // A fully opaque class which replaces AddressInfo resources as of PHP 8.0.0. // // Class synopsis // [code] // final class AddressInfo { // } // [/code] // // LITERATURE_SOURCES // * PHP_NET (2023-11-11) // URL: https://www.php.net/manual/en/class.addressinfo.php // THE_ADDRESSINFO_CLASS - END // // LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/book.sockets.php // SOCKETS - END // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_ACCEPT // ============================== PUBLIC // ============================== ABOUT // Accepts a connection on a socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_accept() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_accept($socket) { $return_socket_accept = false; // ========== SOCKET_ACCEPT - BEGIN // ===== ABOUT // Accepts a connection on a socket // ===== DESCRIPTION // After the socket socket has been created using socket_create(), bound to a name with socket_bind(), and told to listen for connections with socket_listen(), this function will accept incoming connections on that socket. Once a successful connection is made, a new Socket instance is returned, which may be used for communication. If there are multiple connections queued on the socket, the first will be used. If there are no pending connections, socket_accept() will block until a connection becomes present. If socket has been made non-blocking using socket_set_blocking() or socket_set_nonblock(), false will be returned. // The Socket instance returned by socket_accept() may not be used to accept new connections. The original listening socket socket, however, remains open and may be reused. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_accept(Socket $socket): Socket|false // ===== CODE $return_socket_accept = socket_accept( $socket // Socket socket - A Socket instance created with socket_create(). ); // Return Values // Returns a new Socket instance on success, or false on error. The actual error code can be retrieved by calling socket_last_error(). This error code may be passed to socket_strerror() to get a textual explanation of the error. // // Changelog // Version - Description // 8.0.0 - On success, this function returns a Socket instance now; previously, a resource was returned. // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-accept.php // ========== SOCKET_ACCEPT - END // SYNTAX: // Socket|false socket_accept(Socket $socket) return $return_socket_accept; // Socket|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_ACCEPT // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_ADDRINFO_BIND // ============================== OFFLINE // ============================== ABOUT // Create and bind to a socket from a given addrinfo. // ============================== SUPPORT // PHP_7 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_addrinfo_bind() - PHP_7 >= PHP_7_2_0, PHP_8 // ============================== CODE /* function php_other_sockets_socket_addrinfo_bind($address) { $return_socket_addrinfo_bind = false; // ========== SOCKET_ADDRINFO_BIND - BEGIN // ===== ABOUT // Create and bind to a socket from a given addrinfo // ===== DESCRIPTION // Create a Socket instance, and bind it to the provided AddressInfo. The return value of this function may be used with socket_listen(). // ===== SUPPORTED // PHP_7 >= PHP_7_2_0, PHP_8 // ===== SYNTAX // socket_addrinfo_bind(AddressInfo $address): Socket|false // ===== CODE $return_socket_addrinfo_bind = socket_addrinfo_bind( $address // AddressInfo address - AddressInfo instance created from socket_addrinfo_lookup(). ); // Return Values // Returns a Socket instance on success or false on failure. // // Changelog // Version - Description // 8.0.0 - On success, this function returns a Socket instance now; previously, a resource was returned. // 8.0.0 - address is an AddressInfo instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-addrinfo-bind.php // ========== SOCKET_ADDRINFO_BIND - END // SYNTAX: // Socket|false socket_addrinfo_bind(AddressInfo $address) return $return_socket_addrinfo_bind; // Socket|false } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_ADDRINFO_BIND // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_ADDRINFO_CONNECT // ============================== OFFLINE // ============================== ABOUT // Create and connect to a socket from a given addrinfo. // ============================== SUPPORT // PHP_7 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_addrinfo_connect() - PHP_7 >= PHP_7_2_0, PHP_8 // ============================== CODE /* function php_other_sockets_socket_addrinfo_connect($address) { $return_socket_addrinfo_connect = false; // ========== SOCKET_ADDRINFO_CONNECT - BEGIN // ===== ABOUT // Create and connect to a socket from a given addrinfo // ===== DESCRIPTION // Create a Socket instance, and connect it to the provided AddressInfo instance. The return value of this function may be used with the rest of the socket functions. // ===== SUPPORTED // PHP_7 >= PHP_7_2_0, PHP_8 // ===== SYNTAX // socket_addrinfo_connect(AddressInfo $address): Socket|false // ===== CODE $return_socket_addrinfo_connect = socket_addrinfo_connect( $address // AddressInfo address - AddressInfo instance created from socket_addrinfo_lookup() ); // Return Values // Returns a Socket instance on success or false on failure. // // Changelog // Version - Description // 8.0.0 - On success, this function returns a Socket instance now; previously, a resource was returned. // 8.0.0 - address is an AddressInfo instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-addrinfo-connect.php // ========== SOCKET_ADDRINFO_CONNECT - END // SYNTAX: // Socket|false socket_addrinfo_connect(AddressInfo $address) return $return_socket_addrinfo_connect; // Socket|false } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_ADDRINFO_CONNECT // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_ADDRINFO_EXPLAIN // ============================== OFFLINE // ============================== ABOUT // Get information about addrinfo. // ============================== SUPPORT // PHP_7 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_addrinfo_explain() - PHP_7 >= PHP_7_2_0, PHP_8 // ============================== CODE /* function php_other_sockets_socket_addrinfo_explain($address) { $return_socket_addrinfo_explain = null; // ========== SOCKET_ADDRINFO_EXPLAIN - BEGIN // ===== ABOUT // Get information about addrinfo // ===== DESCRIPTION // socket_addrinfo_explain() exposed the underlying addrinfo structure. // ===== SUPPORTED // PHP_7 >= PHP_7_2_0, PHP_8 // ===== SYNTAX // socket_addrinfo_explain(AddressInfo $address): array // ===== CODE $return_socket_addrinfo_explain = socket_addrinfo_explain( $address // AddressInfo address - AddressInfo instance created from socket_addrinfo_lookup() ); // Return Values // Returns an array containing the fields in the addrinfo structure. // // Changelog // Version - Description // 8.0.0 - address is an AddressInfo instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-addrinfo-explain.php // ========== SOCKET_ADDRINFO_EXPLAIN - END // SYNTAX: // array socket_addrinfo_explain(AddressInfo $address) return $return_socket_addrinfo_explain; // array } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_ADDRINFO_EXPLAIN // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_ADDRINFO_LOOKUP // ============================== OFFLINE // ============================== ABOUT // Get array with contents of getaddrinfo about the given hostname. // ============================== SUPPORT // PHP_7 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_addrinfo_lookup() - PHP_7 >= PHP_7_2_0, PHP_8 // ============================== CODE /* function php_other_sockets_socket_addrinfo_lookup($host, $service = null, $hints = array()) { $return_socket_addrinfo_lookup = false; // ========== SOCKET_ADDRINFO_LOOKUP - BEGIN // ===== ABOUT // Get array with contents of getaddrinfo about the given hostname // ===== DESCRIPTION // Lookup different ways we can connect to host. The returned array contains a set of AddressInfo instances that we can bind to using socket_addrinfo_bind(). // ===== SUPPORTED // PHP_7 >= PHP_7_2_0, PHP_8 // ===== SYNTAX // socket_addrinfo_lookup(string $host, ?string $service = null, array $hints = []): array|false // ===== CODE $return_socket_addrinfo_lookup = socket_addrinfo_lookup( $host, // string host - Hostname to search. $service, // string service - The service to connect to. If service is a numeric string, it designates the port. Otherwise it designates a network service name, which is mapped to a port by the operating system. $hints // array hints - Hints provide criteria for selecting addresses returned. You may specify the hints as defined by getaddrinfo. ); // Return Values // Returns an array of AddressInfo instances that can be used with the other socket_addrinfo functions. On failure, false is returned. // // Changelog // Version - Description // 8.0.0 - On success, this function returns a array of AddressInfo instances now; previously, an array of resources was returned. // 8.0.0 - service is nullable now. // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-addrinfo-lookup.php // ========== SOCKET_ADDRINFO_LOOKUP - END // SYNTAX: // array|false socket_addrinfo_lookup(string $host, string $service = null, array $hints = array()) return $return_socket_addrinfo_lookup; // array|false } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_ADDRINFO_LOOKUP // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_BIND // ============================== PUBLIC // ============================== ABOUT // Binds a name to a socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_bind() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_bind($socket, $address, $port = 0) { $return_socket_bind = false; // ========== SOCKET_BIND - BEGIN // ===== ABOUT // Binds a name to a socket // ===== DESCRIPTION // Binds the name given in address to the socket described by socket. This has to be done before a connection is established using socket_connect() or socket_listen(). // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_bind(Socket $socket, string $address, int $port = 0): bool // ===== CODE $return_socket_bind = socket_bind( $socket, // Socket socket - A Socket instance created with socket_create(). $address, // string 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). $port // int 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(). This code may be passed to socket_strerror() to get a textual explanation of the error. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // // [examples] // Examples // [example] // Example #1 Using socket_bind() 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' . "\r\n" . // 'Host: example.com' . "\r\n\r\n"; // socket_write($sock, $request); // // // Close // socket_close($sock); // // [/php] // [/example] // [/examples] // // Notes // Note: This function must be used on the socket before socket_connect(). // Note: Windows 9x/ME compatibility note: socket_last_error() may return an invalid error code if trying to bind the socket to a wrong address that does not belong to your machine. // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-13) // URL: https://www.php.net/manual/en/function.socket-bind.php // ========== SOCKET_BIND - END // SYNTAX: // bool socket_bind(Socket $socket, string $address, int $port = 0) return $return_socket_bind; // bool } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_BIND // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_CLEAR_ERROR // ============================== PUBLIC // ============================== ABOUT // Clears the error on the socket or the last error code. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_clear_error() - PHP_4 >= PHP_4_2_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_clear_error($socket = null) { // ========== SOCKET_CLEAR_ERROR - BEGIN // ===== ABOUT // Clears the error on the socket or the last error code // ===== DESCRIPTION // This function clears the error code on the given socket or the global last socket error if no socket is specified. // This function allows explicitly resetting the error code value either of a socket or of the extension global last error code. This may be useful to detect within a part of the application if an error occurred or not. // ===== SUPPORTED // PHP_4 >= PHP_4_2_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_clear_error(?Socket $socket = null): void // ===== CODE socket_clear_error( $socket // Socket socket - A Socket instance created with socket_create(). ); // Return Values // No value is returned. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // 8.0.0 - socket is nullable now. // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-clear-error.php // ========== SOCKET_CLEAR_ERROR - END // SYNTAX: // void socket_clear_error(Socket $socket = null) // Return: void } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_CLEAR_ERROR // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_CLOSE // ============================== PUBLIC // ============================== ABOUT // Closes a Socket instance. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_close() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_close($socket) { // ========== SOCKET_CLOSE - BEGIN // ===== ABOUT // Closes a Socket instance // ===== DESCRIPTION // socket_close() closes the Socket instance given by socket. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_close(Socket $socket): void // ===== CODE socket_close( $socket // Socket socket - A Socket instance created with socket_create() or socket_accept(). ); // Return Values // No value is returned. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-close.php // ========== SOCKET_CLOSE - END // SYNTAX: // void socket_close(Socket $socket) // Return: void } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_CLOSE // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_CMSG_SPACE // ============================== OFFLINE // ============================== ABOUT // Calculate message buffer size. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_cmsg_space() - PHP_5 >= PHP_5_5_0, PHP_7, PHP_8 // ============================== CODE /* function php_other_sockets_socket_cmsg_space($level, $type, $num = 0) { $return_socket_cmsg_space = 0; // ========== SOCKET_CMSG_SPACE - BEGIN // ===== ABOUT // Calculate message buffer size // ===== DESCRIPTION // Calculates the size of the buffer that should be allocated for receiving the ancillary data. // Warning: This function is currently not documented; only its argument list is available. // ===== SUPPORTED // PHP_5 >= PHP_5_5_0, PHP_7, PHP_8 // ===== SYNTAX // socket_cmsg_space(int $level, int $type, int $num = 0): ?int // ===== CODE $return_socket_cmsg_space = socket_cmsg_space( $level, // int level $type, // int type $num // int num ); // Return Values // int // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-cmsg-space.php // ========== SOCKET_CMSG_SPACE - END // SYNTAX: // int socket_cmsg_space(int $level, int $type, int $num = 0) return $return_socket_cmsg_space; // int } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_CMSG_SPACE // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_CONNECT // ============================== PUBLIC // ============================== ABOUT // Initiates a connection on a socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_connect() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_connect($socket, $address, $port = null) { $return_socket_connect = false; // ========== SOCKET_CONNECT - BEGIN // ===== ABOUT // Initiates a connection on a socket // ===== DESCRIPTION // Initiate a connection to address using the Socket instance socket, which must be Socket instance created with socket_create(). // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_connect(Socket $socket, string $address, ?int $port = null): bool // ===== CODE $return_socket_connect = socket_connect( $socket, // Socket socket - A Socket instance created with socket_create(). $address, // string address - The address parameter is either an IPv4 address in dotted-quad notation (e.g. 127.0.0.1) if socket is AF_INET, a valid IPv6 address (e.g. ::1) if IPv6 support is enabled and socket is AF_INET6 or the pathname of a Unix domain socket, if the socket family is AF_UNIX. $port // int port - The port parameter is only used and is mandatory when connecting to an AF_INET or an AF_INET6 socket, and designates the port on the remote host to which a connection should be made. ); // Return Values // Returns true on success or false on failure. The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual explanation of the error. // Note: If the socket is non-blocking then this function returns false with an error Operation now in progress. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // 8.0.0 - port is nullable now. // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-connect.php // ========== SOCKET_CONNECT - END // SYNTAX: // bool socket_connect(Socket $socket, string $address, int $port = null) return $return_socket_connect; // bool } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_CONNECT // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_CREATE_LISTEN // ============================== PUBLIC // ============================== ABOUT // Opens a socket on port to accept connections. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_create_listen() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_create_listen($port, $backlog = 128) { $return_socket_create_listen = false; // ========== SOCKET_CREATE_LISTEN - BEGIN // ===== ABOUT // Opens a socket on port to accept connections // ===== DESCRIPTION // socket_create_listen() creates a new Socket instance of type AF_INET listening on all local interfaces on the given port waiting for new connections. // This function is meant to ease the task of creating a new socket which only listens to accept new connections. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_create_listen(int $port, int $backlog = 128): Socket|false // ===== CODE $return_socket_create_listen = socket_create_listen( $port, // int port - The port on which to listen on all interfaces. $backlog // int backlog - The backlog parameter defines the maximum length the queue of pending connections may grow to. SOMAXCONN may be passed as backlog parameter, see socket_listen() for more information. ); // Return Values // socket_create_listen() returns a new Socket instance on success or false on error. The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual explanation of the error. // // Changelog // Version - Description // 8.0.0 - On success, this function returns a Socket instance now; previously, a resource was returned. // // Notes // Note: If you want to create a socket which only listens on a certain interface you need to use socket_create(), socket_bind() and socket_listen(). // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-create-listen.php // ========== SOCKET_CREATE_LISTEN - END // SYNTAX: // Socket|false socket_create_listen(int $port, int $backlog = 128) return $return_socket_create_listen; // Socket|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_CREATE_LISTEN // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_CREATE_PAIR // ============================== PUBLIC // ============================== ABOUT // Creates a pair of indistinguishable sockets and stores them in an array. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_create_pair() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_create_pair($domain, $type, $protocol, & $pair) { $return_socket_create_pair = false; // ========== SOCKET_CREATE_PAIR - BEGIN // ===== ABOUT // Creates a pair of indistinguishable sockets and stores them in an array // ===== DESCRIPTION // socket_create_pair() creates two connected and indistinguishable sockets, and stores them in pair. This function is commonly used in IPC (InterProcess Communication). // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_create_pair( // int $domain, // int $type, // int $protocol, // array &$pair // ): bool // ===== CODE $return_socket_create_pair = socket_create_pair( $domain, // int domain - The domain parameter specifies the protocol family to be used by the socket. See socket_create() for the full list. $type, // int type - The type parameter selects the type of communication to be used by the socket. See socket_create() for the full list. $protocol, // int 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(). If the desired protocol is TCP, or UDP the corresponding constants SOL_TCP, and SOL_UDP can also be used. // See socket_create() for the full list of supported protocols. $pair // array& pair - Reference to an array in which the two Socket instances will be inserted. ); // Return Values // Returns true on success or false on failure. // // Changelog // Version - Description // 8.0.0 - pair is a reference to an array of Socket instances now; previously, it was a reference to an array of resources. // // [examples] // Examples // [example] // Example #1 socket_create_pair() 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 Receive Data // if (socket_write($sockets[0], "ABCdef123\n", strlen("ABCdef123\n")) === false) { // echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($sockets[0])); // } // if (($data = socket_read($sockets[1], strlen("ABCdef123\n"), 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]); // [/php] // [/example] // [example] // Example #2 socket_create_pair() 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 "Received $strtwo\n"; // } // 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 "Received $strone\n"; // } // socket_close($ary[0]); // } // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-13) // URL: https://www.php.net/manual/en/function.socket-create-pair.php // ========== SOCKET_CREATE_PAIR - END // SYNTAX: // bool socket_create_pair(int $domain, int $type, int $protocol, array& $pair) return $return_socket_create_pair; // bool } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_CREATE_PAIR // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_CREATE // ============================== PUBLIC // ============================== ABOUT // Create a socket (endpoint for communication). // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_create() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_create($domain, $type, $protocol) { $return_socket_create = false; // ========== SOCKET_CREATE - BEGIN // ===== ABOUT // Create a socket (endpoint for communication) // ===== DESCRIPTION // Creates and returns a Socket instance, also referred to as an endpoint of communication. A typical network connection is made up of 2 sockets, one performing the role of the client, and another performing the role of the server. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_create(int $domain, int $type, int $protocol): Socket|false // ===== CODE $return_socket_create = socket_create( $domain, // int domain - The domain parameter specifies the protocol family to be used by the socket. // // Available address/protocol families // Domain - Description // AF_INET - IPv4 Internet based protocols. TCP and UDP are common protocols of this protocol family. // AF_INET6 - IPv6 Internet based protocols. TCP and UDP are common protocols of this protocol family. // AF_UNIX - Local communication protocol family. High efficiency and low overhead make it a great form of IPC (Interprocess Communication). $type, // int type - The type parameter selects the type of communication to be used by the socket. // // Available socket types // Type - Description // SOCK_STREAM - Provides sequenced, reliable, full-duplex, connection-based byte streams. An out-of-band data transmission mechanism may be supported. The TCP protocol is based on this socket type. // SOCK_DGRAM - Supports datagrams (connectionless, unreliable messages of a fixed maximum length). The UDP protocol is based on this socket type. // SOCK_SEQPACKET - Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each read call. // SOCK_RAW - Provides raw network protocol access. This special type of socket can be used to manually construct any type of protocol. A common use for this socket type is to perform ICMP requests (like ping). // SOCK_RDM - Provides a reliable datagram layer that does not guarantee ordering. This is most likely not implemented on your operating system. $protocol // int 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(). If the desired protocol is TCP, or UDP the corresponding constants SOL_TCP, and SOL_UDP can also be used. // // Common protocols // Name - Description // icmp - The Internet Control Message Protocol is used primarily by gateways and hosts to report errors in datagram communication. The "ping" command (present in most modern operating systems) is an example application of ICMP. // udp - The User Datagram Protocol is a connectionless, unreliable, protocol with fixed record lengths. Due to these aspects, UDP requires a minimum amount of protocol overhead. // tcp - The Transmission Control Protocol is a reliable, connection based, stream oriented, full duplex protocol. TCP guarantees that all data packets will be received in the order in which they were sent. If any packet is somehow lost during communication, TCP will automatically retransmit the packet until the destination host acknowledges that packet. For reliability and performance reasons, the TCP implementation itself decides the appropriate octet boundaries of the underlying datagram communication layer. Therefore, TCP applications must allow for the possibility of partial record transmission. ); // Return Values // socket_create() returns a Socket instance on success, or false on error. The actual error code can be retrieved by calling socket_last_error(). This error code may be passed to socket_strerror() to get a textual explanation of the error. // // Errors/Exceptions // If an invalid domain or type is given, socket_create() defaults to AF_INET and SOCK_STREAM respectively and additionally emits an E_WARNING message. // // Changelog // Version - Description // 8.0.0 - On success, this function returns a Socket instance now; previously, a resource was returned. // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-create.php // ========== SOCKET_CREATE - END // SYNTAX: // Socket|false socket_create(int $domain, int $type, int $protocol) return $return_socket_create; // Socket|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_CREATE // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_EXPORT_STREAM // ============================== OFFLINE // ============================== ABOUT // Export a socket into a stream that encapsulates a socket. // ============================== SUPPORT // PHP_7 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_export_stream() - PHP_7 >= PHP_7_0_7, PHP_8 // ============================== CODE /* function php_other_sockets_socket_export_stream($socket) { $return_socket_export_stream = false; // ========== SOCKET_EXPORT_STREAM - BEGIN // ===== ABOUT // Export a socket into a stream that encapsulates a socket // ===== DESCRIPTION // Warning: This function is currently not documented; only its argument list is available. // ===== SUPPORTED // PHP_7 >= PHP_7_0_7, PHP_8 // ===== SYNTAX // socket_export_stream(Socket $socket): resource|false // ===== CODE $return_socket_export_stream = socket_export_stream( $socket // Socket socket ); // Return Values // Return resource or false on failure. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-09-30) // URL: https://www.php.net/manual/en/function.socket-export-stream.php // ========== SOCKET_EXPORT_STREAM - END // SYNTAX: // resource|false socket_export_stream(Socket $socket) return $return_socket_export_stream; // resource|false } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_EXPORT_STREAM // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_GET_OPTION // ============================== PUBLIC // ============================== ABOUT // Gets socket options for the socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_get_option() - PHP_4 >= PHP_4_3_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_get_option($socket, $level, $option) { $return_socket_get_option = false; // ========== SOCKET_GET_OPTION - BEGIN // ===== ABOUT // Gets socket options for the socket // ===== DESCRIPTION // The socket_get_option() function retrieves the value for the option specified by the option parameter for the specified socket. // ===== SUPPORTED // PHP_4 >= PHP_4_3_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_get_option(Socket $socket, int $level, int $option): array|int|false // ===== CODE $return_socket_get_option = socket_get_option( $socket, // Socket socket - A Socket instance created with socket_create() or socket_accept(). $level, // int level - The level parameter specifies the protocol level at which the option resides. For example, to retrieve options at the socket level, a level parameter of SOL_SOCKET would be used. Other levels, such as TCP, can be used by specifying the protocol number of that level. Protocol numbers can be found by using the getprotobyname() function. $option // int option - Available Socket Options // Option - Description ; Type // SO_DEBUG - Reports whether debugging information is being recorded. ; int // SO_BROADCAST - Reports whether transmission of broadcast messages is supported. ; int // SO_REUSEADDR - Reports whether local addresses can be reused. ; int // SO_REUSEPORT - Reports whether local ports can be reused. ; int // SO_KEEPALIVE - Reports whether connections are kept active with periodic transmission of messages. If the connected socket fails to respond to these messages, the connection is broken and processes writing to that socket are notified with a SIGPIPE signal. ; int // SO_LINGER - Reports whether the socket lingers on socket_close() if data is present. By default, when the socket is closed, it attempts to send all unsent data. In the case of a connection-oriented socket, socket_close() will wait for its peer to acknowledge the data. // If l_onoff is non-zero and l_linger is zero, all the unsent data will be discarded and RST (reset) is sent to the peer in the case of a connection-oriented socket. // On the other hand, if l_onoff is non-zero and l_linger is non-zero, socket_close() will block until all the data is sent or the time specified in l_linger elapses. If the socket is non-blocking, socket_close() will fail and return an error. // ; array. The array will contain two keys: l_onoff and l_linger. // SO_OOBINLINE - Reports whether the socket leaves out-of-band data inline. ; int // SO_SNDBUF - Reports the size of the send buffer. ; int // SO_RCVBUF - Reports the size of the receive buffer. ; int // SO_ERROR - Reports information about error status and clears it. ; int (cannot be set by socket_set_option()) // SO_TYPE - Reports the socket type (e.g. SOCK_STREAM). ; int (cannot be set by socket_set_option()) // SO_DONTROUTE - Reports whether outgoing messages bypass the standard routing facilities. ; int // SO_RCVLOWAT - Reports the minimum number of bytes to process for socket input operations. ; int // SO_RCVTIMEO - Reports the timeout value for input operations. ; array. The array will contain two keys: sec which is the seconds part on the timeout value and usec which is the microsecond part of the timeout value. // SO_SNDTIMEO - Reports the timeout value specifying the amount of time that an output function blocks because flow control prevents data from being sent. ; array. The array will contain two keys: sec which is the seconds part on the timeout value and usec which is the microsecond part of the timeout value. // SO_SNDLOWAT - Reports the minimum number of bytes to process for socket output operations. ; int // TCP_NODELAY - Reports whether the Nagle TCP algorithm is disabled. ; int // MCAST_JOIN_GROUP - Joins a multicast group. ; array with keys "group", specifying a string with an IPv4 or IPv6 multicast address and "interface", specifying either an interface number (type int) or a string with the interface name, like "eth0". 0 can be specified to indicate the interface should be selected using routing rules. (can only be used in socket_set_option()) // MCAST_LEAVE_GROUP - Leaves a multicast group. ; array. See MCAST_JOIN_GROUP for more information. (can only be used in socket_set_option()) // MCAST_BLOCK_SOURCE - Blocks packets arriving from a specific source to a specific multicast group, which must have been previously joined. ; array with the same keys as MCAST_JOIN_GROUP, plus one extra key, source, which maps to a string specifying an IPv4 or IPv6 address of the source to be blocked. (can only be used in socket_set_option()) // MCAST_UNBLOCK_SOURCE - Unblocks (start receiving again) packets arriving from a specific source address to a specific multicast group, which must have been previously joined. ; array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option()) // MCAST_JOIN_SOURCE_GROUP - Receive packets destined to a specific multicast group whose source address matches a specific value. ; array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option()) // MCAST_LEAVE_SOURCE_GROUP - Stop receiving packets destined to a specific multicast group whose source address matches a specific value. ; array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option()) // IP_MULTICAST_IF - The outgoing interface for IPv4 multicast packets. ; Either int specifying the interface number or a string with an interface name, like eth0. The value 0 can be used to indicate the routing table is to used in the interface selection. The function socket_get_option() returns an interface index. Note that, unlike the C API, this option does NOT take an IP address. This eliminates the interface difference between IP_MULTICAST_IF and IPV6_MULTICAST_IF. // IPV6_MULTICAST_IF - The outgoing interface for IPv6 multicast packets. ; The same as IP_MULTICAST_IF. // IP_MULTICAST_LOOP - The multicast loopback policy for IPv4 packets enables or disables loopback of outgoing multicasts, which must have been previously joined. The effect differs, however, whether it applies on unixes or Windows, the former being on the receive path whereas the latter being on the send path. ; int (either 0 or 1). For socket_set_option() any value will be accepted and will be converted to a boolean following the usual PHP rules. // IPV6_MULTICAST_LOOP - Analogous to IP_MULTICAST_LOOP, but for IPv6. ; int. See IP_MULTICAST_LOOP. // IP_MULTICAST_TTL - The time-to-live of outgoing IPv4 multicast packets. This should be a value between 0 (don't leave the interface) and 255. The default value is 1 (only the local network is reached). ; int between 0 and 255. // IPV6_MULTICAST_HOPS - Analogous to IP_MULTICAST_TTL, but for IPv6 packets. The value -1 is also accepted, meaning the route default should be used. ; int between -1 and 255. // SO_MARK - Sets an identifier on the socket for packet filtering purpose on Linux. ; int // SO_ACCEPTFILTER - Adds an accept filter on the listened socket (FreeBSD/NetBSD). An accept filter kernel module needs to be loaded beforehand on FreeBSD (e.g. accf_http). ; string name of the filter (length 15 max). // SO_USER_COOKIE - Sets an identifier on the socket for packet filtering purpose on FreeBSD. ; int // SO_RTABLE - Sets an identifier on the socket for packet filtering purpose on OpenBSD. ; int // SO_DONTTRUNC - Retain unread data. ; int // SO_WANTMORE - Give a hint when more data is ready. ; int // TCP_DEFER_ACCEPT - Don't notify a listening socket until data is ready. ; int // SO_INCOMING_CPU - Gets/Sets the cpu affinity of a socket. ; int // SO_MEMINFO - Gets all the meminfo of a socket. ; int // SO_BPF_EXTENSIONS - Gets the supported BPF extensions by the kernel to attach to a socket. ; int // SO_SETFIB - Sets the route table (FIB) of a socket. (FreeBSD only) ; int // SOL_FILTER - Filters attributed to a socket. (Solaris/Illumos only) ; int // TCP_KEEPCNT - Sets the maximum number of keepalive probes TCP should send before dropping the connection. ; int // TCP_KEEPIDLE - Sets the time the connection needs to remain idle. ; int // TCP_KEEPINTVL - Sets the time between individual keepalive probes. ; int // TCP_KEEPALIVE - Sets the time the connection needs to remain idle. (macOS only) ; int // TCP_NOTSENT_LOWAT - Sets the limit number of unsent data in write queue by the socket stream. (Linux only) ; int ); // Return Values // Returns the value of the given option, or false on failure. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // // [examples] // Examples // [example] // Example #1 socket_get_option() example // [php] // $socket = socket_create_listen(1223); // // $linger = array('l_linger' => 1, 'l_onoff' => 1); // socket_set_option($socket, SOL_SOCKET, SO_LINGER, $linger); // // var_dump(socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR)); // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-13) // URL: https://www.php.net/manual/en/function.socket-get-option.php // ========== SOCKET_GET_OPTION - END // SYNTAX: // array|int|false socket_get_option(Socket $socket, int $level, int $option) return $return_socket_get_option; // array|int|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_GET_OPTION // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_GETOPT // ============================== PUBLIC // ============================== ABOUT // Gets socket options for the socket. // // socket_getopt - Alias of socket_get_option(). // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_getopt() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_getopt($socket, $level, $option) { $return_socket_getopt = false; // ========== SOCKET_GETOPT - BEGIN // ===== ABOUT // socket_getopt - Alias of socket_get_option() // ===== DESCRIPTION // This function is an alias of: socket_get_option(). // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // ===== CODE $return_socket_getopt = socket_getopt( $socket, // Socket socket $level, // int level $option // int option ); // Return // array|int|false // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-13) // URL: https://www.php.net/manual/en/function.socket-getopt.php // ========== SOCKET_GETOPT - END // ========== SOCKET_GET_OPTION - BEGIN // ===== ABOUT // Gets socket options for the socket // ===== DESCRIPTION // The socket_get_option() function retrieves the value for the option specified by the option parameter for the specified socket. // ===== SUPPORTED // PHP_4 >= PHP_4_3_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_get_option(Socket $socket, int $level, int $option): array|int|false // ===== CODE // $return_socket_get_option = socket_get_option( // $socket, // Socket socket - A Socket instance created with socket_create() or socket_accept(). // $level, // int level - The level parameter specifies the protocol level at which the option resides. For example, to retrieve options at the socket level, a level parameter of SOL_SOCKET would be used. Other levels, such as TCP, can be used by specifying the protocol number of that level. Protocol numbers can be found by using the getprotobyname() function. // $option // int option - Available Socket Options // Option - Description ; Type // SO_DEBUG - Reports whether debugging information is being recorded. ; int // SO_BROADCAST - Reports whether transmission of broadcast messages is supported. ; int // SO_REUSEADDR - Reports whether local addresses can be reused. ; int // SO_REUSEPORT - Reports whether local ports can be reused. ; int // SO_KEEPALIVE - Reports whether connections are kept active with periodic transmission of messages. If the connected socket fails to respond to these messages, the connection is broken and processes writing to that socket are notified with a SIGPIPE signal. ; int // SO_LINGER - Reports whether the socket lingers on socket_close() if data is present. By default, when the socket is closed, it attempts to send all unsent data. In the case of a connection-oriented socket, socket_close() will wait for its peer to acknowledge the data. // If l_onoff is non-zero and l_linger is zero, all the unsent data will be discarded and RST (reset) is sent to the peer in the case of a connection-oriented socket. // On the other hand, if l_onoff is non-zero and l_linger is non-zero, socket_close() will block until all the data is sent or the time specified in l_linger elapses. If the socket is non-blocking, socket_close() will fail and return an error. // ; array. The array will contain two keys: l_onoff and l_linger. // SO_OOBINLINE - Reports whether the socket leaves out-of-band data inline. ; int // SO_SNDBUF - Reports the size of the send buffer. ; int // SO_RCVBUF - Reports the size of the receive buffer. ; int // SO_ERROR - Reports information about error status and clears it. ; int (cannot be set by socket_set_option()) // SO_TYPE - Reports the socket type (e.g. SOCK_STREAM). ; int (cannot be set by socket_set_option()) // SO_DONTROUTE - Reports whether outgoing messages bypass the standard routing facilities. ; int // SO_RCVLOWAT - Reports the minimum number of bytes to process for socket input operations. ; int // SO_RCVTIMEO - Reports the timeout value for input operations. ; array. The array will contain two keys: sec which is the seconds part on the timeout value and usec which is the microsecond part of the timeout value. // SO_SNDTIMEO - Reports the timeout value specifying the amount of time that an output function blocks because flow control prevents data from being sent. ; array. The array will contain two keys: sec which is the seconds part on the timeout value and usec which is the microsecond part of the timeout value. // SO_SNDLOWAT - Reports the minimum number of bytes to process for socket output operations. ; int // TCP_NODELAY - Reports whether the Nagle TCP algorithm is disabled. ; int // MCAST_JOIN_GROUP - Joins a multicast group. ; array with keys "group", specifying a string with an IPv4 or IPv6 multicast address and "interface", specifying either an interface number (type int) or a string with the interface name, like "eth0". 0 can be specified to indicate the interface should be selected using routing rules. (can only be used in socket_set_option()) // MCAST_LEAVE_GROUP - Leaves a multicast group. ; array. See MCAST_JOIN_GROUP for more information. (can only be used in socket_set_option()) // MCAST_BLOCK_SOURCE - Blocks packets arriving from a specific source to a specific multicast group, which must have been previously joined. ; array with the same keys as MCAST_JOIN_GROUP, plus one extra key, source, which maps to a string specifying an IPv4 or IPv6 address of the source to be blocked. (can only be used in socket_set_option()) // MCAST_UNBLOCK_SOURCE - Unblocks (start receiving again) packets arriving from a specific source address to a specific multicast group, which must have been previously joined. ; array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option()) // MCAST_JOIN_SOURCE_GROUP - Receive packets destined to a specific multicast group whose source address matches a specific value. ; array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option()) // MCAST_LEAVE_SOURCE_GROUP - Stop receiving packets destined to a specific multicast group whose source address matches a specific value. ; array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option()) // IP_MULTICAST_IF - The outgoing interface for IPv4 multicast packets. ; Either int specifying the interface number or a string with an interface name, like eth0. The value 0 can be used to indicate the routing table is to used in the interface selection. The function socket_get_option() returns an interface index. Note that, unlike the C API, this option does NOT take an IP address. This eliminates the interface difference between IP_MULTICAST_IF and IPV6_MULTICAST_IF. // IPV6_MULTICAST_IF - The outgoing interface for IPv6 multicast packets. ; The same as IP_MULTICAST_IF. // IP_MULTICAST_LOOP - The multicast loopback policy for IPv4 packets enables or disables loopback of outgoing multicasts, which must have been previously joined. The effect differs, however, whether it applies on unixes or Windows, the former being on the receive path whereas the latter being on the send path. ; int (either 0 or 1). For socket_set_option() any value will be accepted and will be converted to a boolean following the usual PHP rules. // IPV6_MULTICAST_LOOP - Analogous to IP_MULTICAST_LOOP, but for IPv6. ; int. See IP_MULTICAST_LOOP. // IP_MULTICAST_TTL - The time-to-live of outgoing IPv4 multicast packets. This should be a value between 0 (don't leave the interface) and 255. The default value is 1 (only the local network is reached). ; int between 0 and 255. // IPV6_MULTICAST_HOPS - Analogous to IP_MULTICAST_TTL, but for IPv6 packets. The value -1 is also accepted, meaning the route default should be used. ; int between -1 and 255. // SO_MARK - Sets an identifier on the socket for packet filtering purpose on Linux. ; int // SO_ACCEPTFILTER - Adds an accept filter on the listened socket (FreeBSD/NetBSD). An accept filter kernel module needs to be loaded beforehand on FreeBSD (e.g. accf_http). ; string name of the filter (length 15 max). // SO_USER_COOKIE - Sets an identifier on the socket for packet filtering purpose on FreeBSD. ; int // SO_RTABLE - Sets an identifier on the socket for packet filtering purpose on OpenBSD. ; int // SO_DONTTRUNC - Retain unread data. ; int // SO_WANTMORE - Give a hint when more data is ready. ; int // TCP_DEFER_ACCEPT - Don't notify a listening socket until data is ready. ; int // SO_INCOMING_CPU - Gets/Sets the cpu affinity of a socket. ; int // SO_MEMINFO - Gets all the meminfo of a socket. ; int // SO_BPF_EXTENSIONS - Gets the supported BPF extensions by the kernel to attach to a socket. ; int // SO_SETFIB - Sets the route table (FIB) of a socket. (FreeBSD only) ; int // SOL_FILTER - Filters attributed to a socket. (Solaris/Illumos only) ; int // TCP_KEEPCNT - Sets the maximum number of keepalive probes TCP should send before dropping the connection. ; int // TCP_KEEPIDLE - Sets the time the connection needs to remain idle. ; int // TCP_KEEPINTVL - Sets the time between individual keepalive probes. ; int // TCP_KEEPALIVE - Sets the time the connection needs to remain idle. (macOS only) ; int // TCP_NOTSENT_LOWAT - Sets the limit number of unsent data in write queue by the socket stream. (Linux only) ; int // ); // Return Values // Returns the value of the given option, or false on failure. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // // [examples] // Examples // [example] // Example #1 socket_get_option() example // [php] // $socket = socket_create_listen(1223); // // $linger = array('l_linger' => 1, 'l_onoff' => 1); // socket_set_option($socket, SOL_SOCKET, SO_LINGER, $linger); // // var_dump(socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR)); // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-13) // URL: https://www.php.net/manual/en/function.socket-get-option.php // ========== SOCKET_GET_OPTION - END // SYNTAX: // array|int|false socket_getopt(Socket $socket, int $level, int $option) return $return_socket_getopt; // array|int|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_GETOPT // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_GETPEERNAME // ============================== PUBLIC // ============================== ABOUT // Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_getpeername() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_getpeername($socket, & $address, & $port) { $return_socket_getpeername = false; // ========== SOCKET_GETPEERNAME - BEGIN // ===== ABOUT // Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type // ===== DESCRIPTION // Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_getpeername(Socket $socket, string &$address, int &$port = null): bool // ===== CODE $return_socket_getpeername = socket_getpeername( $socket, // Socket socket - A Socket instance created with socket_create() or socket_accept(). $address, // string& address - If the given socket is of type AF_INET or AF_INET6, socket_getpeername() will return the peers (remote) IP address in appropriate notation (e.g. 127.0.0.1 or fe80::1) in the address parameter and, if the optional port parameter is present, also the associated port. // If the given socket is of type AF_UNIX, socket_getpeername() will return the Unix filesystem path (e.g. /var/run/daemon.sock) in the address parameter. $port // int& port - If given, this will hold the port associated to address. ); // Return Values // Returns true on success or false on failure. socket_getpeername() may also return false if the socket type is not any of AF_INET, AF_INET6, or AF_UNIX, in which case the last socket error code is not updated. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // // Notes // Note: socket_getpeername() should not be used with AF_UNIX sockets created with socket_accept(). Only sockets created with socket_connect() or a primary server socket following a call to socket_bind() will return meaningful values. // Note: For having socket_getpeername() to return a meaningful value, the socket it is applied upon must of course be one for which the concept of "peer" makes sense. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-getpeername.php // ========== SOCKET_GETPEERNAME - END // SYNTAX: // bool socket_getpeername(Socket $socket, string& $address, int& $port) return $return_socket_getpeername; // bool } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_GETPEERNAME // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_GETSOCKNAME // ============================== PUBLIC // ============================== ABOUT // Queries the local side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_getsockname() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_getsockname($socket, & $address, & $port) { $return_socket_getsockname = false; // ========== SOCKET_GETSOCKNAME - BEGIN // ===== ABOUT // Queries the local side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type // ===== DESCRIPTION // Note: socket_getsockname() should not be used with AF_UNIX sockets created with socket_connect(). Only sockets created with socket_accept() or a primary server socket following a call to socket_bind() will return meaningful values. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_getsockname(Socket $socket, string &$address, int &$port = null): bool // ===== CODE $return_socket_getsockname = socket_getsockname( $socket, // Socket socket - A Socket instance created with socket_create() or socket_accept(). $address, // string& address - If the given socket is of type AF_INET or AF_INET6, socket_getsockname() will return the local IP address in appropriate notation (e.g. 127.0.0.1 or fe80::1) in the address parameter and, if the optional port parameter is present, also the associated port. // If the given socket is of type AF_UNIX, socket_getsockname() will return the Unix filesystem path (e.g. /var/run/daemon.sock) in the address parameter. $port // int& port - If provided, this will hold the associated port. ); // Return Values // Returns true on success or false on failure. socket_getsockname() may also return false if the socket type is not any of AF_INET, AF_INET6, or AF_UNIX, in which case the last socket error code is not updated. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-getsockname.php // ========== SOCKET_GETSOCKNAME - END // SYNTAX: // bool socket_getsockname(Socket $socket, string& $address, int& $port) return $return_socket_getsockname; // bool } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_GETSOCKNAME // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_IMPORT_STREAM // ============================== OFFLINE // ============================== ABOUT // Import a stream. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_import_stream() - PHP_5 >= PHP_5_4_0, PHP_7, PHP_8 // ============================== CODE /* function php_other_sockets_socket_import_stream($stream) { $return_socket_import_stream = false; // ========== SOCKET_IMPORT_STREAM - BEGIN // ===== ABOUT // Import a stream // ===== DESCRIPTION // Imports a stream that encapsulates a socket into a socket extension resource. // ===== SUPPORTED // PHP_5 >= PHP_5_4_0, PHP_7, PHP_8 // ===== SYNTAX // socket_import_stream(resource $stream): Socket|false // ===== CODE $return_socket_import_stream = socket_import_stream( $stream // resource stream - The stream resource to import. ); // Return Values // Returns false on failure. // // Changelog // Version - Description // 8.0.0 - On success, this function returns a Socket instance now; previously, a resource was returned. // // [examples] // Examples // [example] // Example #1 socket_import_stream() example // [php] // $stream = stream_socket_server("udp://0.0.0.0:58380", $errno, $errstr, STREAM_SERVER_BIND); // $sock = socket_import_stream($stream); // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-13) // URL: https://www.php.net/manual/en/function.socket-import-stream.php // ========== SOCKET_IMPORT_STREAM - END // SYNTAX: // Socket|false socket_import_stream(resource $stream) return $return_socket_import_stream; // Socket|false } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_IMPORT_STREAM // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_LAST_ERROR // ============================== PUBLIC // ============================== ABOUT // Returns the last error on the socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_last_error() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_last_error($socket = null) { $return_socket_last_error = 0; // ========== SOCKET_LAST_ERROR - BEGIN // ===== ABOUT // Returns the last error on the socket // ===== DESCRIPTION // If a Socket instance is passed to this function, the last error which occurred on this particular socket is returned. If socket is null, the error code of the last failed socket function is returned. The latter is particularly helpful for functions like socket_create() which don't return a socket on failure and socket_select() which can fail for reasons not directly tied to a particular socket. The error code is suitable to be fed to socket_strerror() which returns a string describing the given error code. // If no error had occurred, or the error had been cleared with socket_clear_error(), the function returns 0. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_last_error(?Socket $socket = null): int // ===== CODE $return_socket_last_error = socket_last_error( $socket // Socket socket - A Socket instance created with socket_create(). ); // Return Values // This function returns a socket error code. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // 8.0.0 - socket is nullable now. // // [examples] // Examples // [example] // Example #1 socket_last_error() example // [php] // $socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // // if ($socket === false) { // $errorcode = socket_last_error(); // $errormsg = socket_strerror($errorcode); // // die("Couldn't create socket: [$errorcode] $errormsg"); // } // [/php] // [/example] // [/examples] // // Notes // Note: socket_last_error() does not clear the error code, use socket_clear_error() for this purpose. // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-13) // URL: https://www.php.net/manual/en/function.socket-last-error.php // ========== SOCKET_LAST_ERROR - END // SYNTAX: // int socket_last_error(Socket $socket = null) return $return_socket_last_error; // int } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_LAST_ERROR // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_LISTEN // ============================== PUBLIC // ============================== ABOUT // Listens for a connection on a socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_listen() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_listen($socket, $backlog = 0) { $return_socket_listen = false; // ========== SOCKET_LISTEN - BEGIN // ===== ABOUT // Listens for a connection on a socket // ===== DESCRIPTION // After the socket socket has been created using socket_create() and bound to a name with socket_bind(), it may be told to listen for incoming connections on socket. // socket_listen() is applicable only to sockets of type SOCK_STREAM or SOCK_SEQPACKET. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_listen(Socket $socket, int $backlog = 0): bool // ===== CODE $return_socket_listen = socket_listen( $socket, // Socket socket - A Socket instance created with socket_create() or socket_addrinfo_bind() $backlog // int backlog - A maximum of backlog incoming connections will be queued for processing. If a connection request arrives with the queue full the client may receive an error with an indication of ECONNREFUSED, or, if the underlying protocol supports retransmission, the request may be ignored so that retries may succeed. // Note: The maximum number passed to the backlog parameter highly depends on the underlying platform. On Linux, it is silently truncated to SOMAXCONN. On win32, if passed SOMAXCONN, the underlying service provider responsible for the socket will set the backlog to a maximum reasonable value. There is no standard provision to find out the actual backlog value on this platform. ); // Return Values // Returns true on success or false on failure. The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual explanation of the error. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-listen.php // ========== SOCKET_LISTEN - END // SYNTAX: // bool socket_listen(Socket $socket, int $backlog = 0) return $return_socket_listen; // bool } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_LISTEN // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_READ // ============================== PUBLIC // ============================== ABOUT // Reads a maximum of length bytes from a socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_read() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== USING CONSTANTS (1) // PHP_BINARY_READ - socket_read() // ============================== CODE function php_other_sockets_socket_read($socket, $length, $mode = PHP_BINARY_READ) { $return_socket_read = false; // ========== SOCKET_READ - BEGIN // ===== ABOUT // Reads a maximum of length bytes from a socket // ===== DESCRIPTION // The function socket_read() reads from the Socket instance socket created by the socket_create() or socket_accept() functions. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_read(Socket $socket, int $length, int $mode = PHP_BINARY_READ): string|false // ===== CODE $return_socket_read = socket_read( $socket, // Socket socket - A Socket instance created with socket_create() or socket_accept(). $length, // int length - The maximum number of bytes read is specified by the length parameter. Otherwise you can use \r, \n, or \0 to end reading (depending on the mode parameter, see below). $mode // int mode - Optional mode parameter is a named constant: // * PHP_BINARY_READ (Default) - use the system recv() function. Safe for reading binary data. // * PHP_NORMAL_READ - reading stops at \n or \r. ); // Return Values // socket_read() returns the data as a string on success, or false on error (including if the remote host has closed the connection). The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual representation of the error. // Note: socket_read() returns a zero length string ("") when there is no more data to read. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-read.php // ========== SOCKET_READ - END // SYNTAX: // string|false socket_read(Socket $socket, int $length, int $mode = PHP_BINARY_READ) return $return_socket_read; // string|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_READ // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_RECV // ============================== PUBLIC // ============================== ABOUT // Receives data from a connected socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_recv() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_recv($socket, & $data, $length, $flags) { $return_socket_recv = false; // ========== SOCKET_RECV - BEGIN // ===== ABOUT // Receives data from a connected socket // ===== DESCRIPTION // The socket_recv() function receives length bytes of data in data from socket. socket_recv() can be used to gather data from connected sockets. Additionally, one or more flags can be specified to modify the behaviour of the function. // data is passed by reference, so it must be specified as a variable in the argument list. Data read from socket by socket_recv() will be returned in data. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_recv( // Socket $socket, // ?string &$data, // int $length, // int $flags // ): int|false // ===== CODE $return_socket_recv = socket_recv( $socket, // Socket socket - The socket must be a Socket instance previously created by socket_create(). $data, // string& data - The data received will be fetched to the variable specified with data. If an error occurs, if the connection is reset, or if no data is available, data will be set to null. $length, // int length - Up to length bytes will be fetched from remote host. $flags // int flags - The value of flags can be any combination of the following flags, joined with the binary OR (|) operator. // // Possible values for flags // Flag - Description // MSG_OOB - Process out-of-band data. // MSG_PEEK - Receive data from the beginning of the receive queue without removing it from the queue. // MSG_WAITALL - Block until at least length are received. However, if a signal is caught or the remote host disconnects, the function may return less data. // MSG_DONTWAIT - With this flag set, the function returns even if it would normally have blocked. ); // Return Values // socket_recv() returns the number of bytes received, or false if there was an error. The actual error code can be retrieved by calling socket_last_error(). This error code may be passed to socket_strerror() to get a textual explanation of the error. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // // [examples] // Examples // [example] // Example #1 socket_recv() example // This example is a simple rewrite of the first example from Examples to use socket_recv(). // [php] // error_reporting(E_ALL); // // echo "

TCP/IP Connection

\n"; // // // Get the port for the WWW service. // $service_port = getservbyname('www', 'tcp'); // // // Get the IP address for the target host. // $address = gethostbyname('www.example.com'); // // // Create a TCP/IP socket. // $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // if ($socket === false) { // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; // } else { // echo "OK.\n"; // } // // echo "Attempting to connect to '$address' on port '$service_port'..."; // $result = socket_connect($socket, $address, $service_port); // if ($result === false) { // echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n"; // } else { // echo "OK.\n"; // } // // $in = "HEAD / HTTP/1.1\r\n"; // $in .= "Host: www.example.com\r\n"; // $in .= "Connection: Close\r\n\r\n"; // $out = ''; // // echo "Sending HTTP HEAD request..."; // socket_write($socket, $in, strlen($in)); // echo "OK.\n"; // // echo "Reading response:\n\n"; // $buf = 'This is my buffer.'; // if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) { // echo "Read $bytes bytes from socket_recv(). Closing socket..."; // } else { // echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n"; // } // socket_close($socket); // // echo $buf . "\n"; // echo "OK.\n\n"; // [/php] // The above example will produce something like: // [result] //

TCP/IP Connection

// OK. // Attempting to connect to '208.77.188.166' on port '80'...OK. // Sending HTTP HEAD request...OK. // Reading response: // // Read 123 bytes from socket_recv(). Closing socket...HTTP/1.1 200 OK // Date: Mon, 14 Sep 2009 08:56:36 GMT // Server: Apache/2.2.3 (Red Hat) // Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT // ETag: "b80f4-1b6-80bfd280" // Accept-Ranges: bytes // Content-Length: 438 // Connection: close // Content-Type: text/html; charset=UTF-8 // // OK. // [/result] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-14) // URL: https://www.php.net/manual/en/function.socket-recv.php // ========== SOCKET_RECV - END // SYNTAX: // int|false socket_recv(Socket $socket, string& $data, int $length, int $flags) return $return_socket_recv; // int|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_RECV // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_RECVFROM // ============================== PUBLIC // ============================== ABOUT // Receives data from a socket whether or not it is connection-oriented. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_recvfrom() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_recvfrom($socket, & $data, $length, $flags, & $address, & $port) { $return_socket_recvfrom = false; // ========== SOCKET_RECVFROM - BEGIN // ===== ABOUT // Receives data from a socket whether or not it is connection-oriented // ===== DESCRIPTION // The socket_recvfrom() function receives length bytes of data in data from address on port port (if the socket is not of type AF_UNIX) using socket. socket_recvfrom() can be used to gather data from both connected and unconnected sockets. Additionally, one or more flags can be specified to modify the behaviour of the function. // The address and port must be passed by reference. If the socket is not connection-oriented, address will be set to the internet protocol address of the remote host or the path to the UNIX socket. If the socket is connection-oriented, address is null. Additionally, the port will contain the port of the remote host in the case of an unconnected AF_INET or AF_INET6 socket. // Note: This function is binary-safe. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_recvfrom( // Socket $socket, // string &$data, // int $length, // int $flags, // string &$address, // int &$port = null // ): int|false // ===== CODE $return_socket_recvfrom = socket_recvfrom( $socket, // Socket socket - The socket must be a Socket instance previously created by socket_create(). $data, // string& data - The data received will be fetched to the variable specified with data. $length, // int length - Up to length bytes will be fetched from remote host. $flags, // int flags - The value of flags can be any combination of the following flags, joined with the binary OR (|) operator. // // Possible values for flags // Flag - Description // MSG_OOB - Process out-of-band data. // MSG_PEEK - Receive data from the beginning of the receive queue without removing it from the queue. // MSG_WAITALL - Block until at least length are received. However, if a signal is caught or the remote host disconnects, the function may return less data. // MSG_DONTWAIT - With this flag set, the function returns even if it would normally have blocked. $address, // string& address - If the socket is of the type AF_UNIX type, address is the path to the file. Else, for unconnected sockets, address is the IP address of, the remote host, or null if the socket is connection-oriented. $port // int& port - This argument only applies to AF_INET and AF_INET6 sockets, and specifies the remote port from which the data is received. If the socket is connection-oriented, port will be null. ); // Return Values // socket_recvfrom() returns the number of bytes received, or false if there was an error. The actual error code can be retrieved by calling socket_last_error(). This error code may be passed to socket_strerror() to get a textual explanation of the error. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // // [examples] // Examples // [example] // Example #1 socket_recvfrom() example // [php] // // $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // socket_bind($socket, '127.0.0.1', 1223); // // $from = ''; // $port = 0; // socket_recvfrom($socket, $buf, 12, 0, $from, $port); // // echo "Received $buf from remote address $from and remote port $port" . PHP_EOL; // [/php] // This example will initiate a UDP socket on port 1223 of 127.0.0.1 and print at most 12 characters received from a remote host. // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-14) // URL: https://www.php.net/manual/en/function.socket-recvfrom.php // ========== SOCKET_RECVFROM - END // SYNTAX: // int|false socket_recvfrom(Socket $socket, string& $data, int $length, int $flags, string& $address, int& $port) return $return_socket_recvfrom; // int|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_RECVFROM // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_RECVMSG // ============================== OFFLINE // ============================== ABOUT // Read a message. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_recvmsg() - PHP_5 >= PHP_5_5_0, PHP_7, PHP_8 // ============================== CODE /* function php_other_sockets_socket_recvmsg($socket, & $message, $flags) { $return_socket_recvmsg = false; // ========== SOCKET_RECVMSG - BEGIN // ===== ABOUT // Read a message // ===== DESCRIPTION // Warning: This function is currently not documented; only its argument list is available. // ===== SUPPORTED // PHP_5 >= PHP_5_5_0, PHP_7, PHP_8 // ===== SYNTAX // socket_recvmsg(Socket $socket, array &$message, int $flags = 0): int|false // ===== CODE $return_socket_recvmsg = socket_recvmsg( $socket, // Socket socket $message, // array& message $flags // int flags ); // Return Values // int|false // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-recvmsg.php // ========== SOCKET_RECVMSG - END // SYNTAX: // int|false socket_recvmsg(Socket $socket, array& $message, int $flags) return $return_socket_recvmsg; // int|false } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_RECVMSG // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_SELECT // ============================== PUBLIC // ============================== ABOUT // Runs the select() system call on the given arrays of sockets with a specified timeout. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_select() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_select(& $read, & $write, & $except, $seconds, $microseconds) { $return_socket_select = false; // ========== SOCKET_SELECT - BEGIN // ===== ABOUT // Runs the select() system call on the given arrays of sockets with a specified timeout // ===== DESCRIPTION // socket_select() accepts arrays of sockets and waits for them to change status. Those coming with BSD sockets background will recognize that those socket arrays are in fact the so-called file descriptor sets. Three independent arrays of sockets are watched. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_select( // ?array &$read, // ?array &$write, // ?array &$except, // ?int $seconds, // int $microseconds = 0 // ): int|false // ===== CODE $return_socket_select = socket_select( $read, // array& read - The sockets listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a socket is also ready on end-of-file, in which case a socket_read() will return a zero length string). $write, // array& write - The sockets listed in the write array will be watched to see if a write will not block. $except, // array& except - The sockets listed in the except array will be watched for exceptions. $seconds, // int seconds - The seconds and microseconds together form the timeout parameter. The timeout is an upper bound on the amount of time elapsed before socket_select() return. seconds may be zero , causing socket_select() to return immediately. This is useful for polling. If seconds is null (no timeout), socket_select() can block indefinitely. $microseconds // int microseconds - // Warning: On exit, the arrays are modified to indicate which socket actually changed status. // You do not need to pass every array to socket_select(). You can leave it out and use an empty array or null instead. Also do not forget that those arrays are passed by reference and will be modified after socket_select() returns. // Note: // Due a limitation in the current Zend Engine it is not possible to pass a constant modifier like null directly as a parameter to a function which expects this parameter to be passed by reference. Instead use a temporary variable or an expression with the leftmost member being a temporary variable: // [example] // Example #1 Using null with socket_select() // [php] // $e = NULL; // socket_select($r, $w, $e, 0); // [/php] // [/example] ); // Return Values // On success socket_select() returns the number of sockets contained in the modified arrays, which may be zero if the timeout expires before anything interesting happens.On error false is returned. The error code can be retrieved with socket_last_error(). // Note: // Be sure to use the === operator when checking for an error. Since the socket_select() may return 0 the comparison with == would evaluate to true: // [example] // Example #2 Understanding socket_select()'s result // [php] // $e = NULL; // if (false === socket_select($r, $w, $e, 0)) { // echo "socket_select() failed, reason: " . // socket_strerror(socket_last_error()) . "\n"; // } // [/php] // [/example] // // [examples] // Examples // [example] // Example #3 socket_select() example // [php] // // Prepare the read array // $read = array($socket1, $socket2); // $write = NULL; // $except = NULL; // $num_changed_sockets = socket_select($read, $write, $except, 0); // // if ($num_changed_sockets === false) { // // Error handling // } else if ($num_changed_sockets > 0) { // // At least at one of the sockets something interesting happened // } // [/php] // [/example] // [/examples] // // Notes // Note: // Be aware that some socket implementations need to be handled very carefully. A few basic rules: // * You should always try to use socket_select() without timeout. Your program should have nothing to do if there is no data available. Code that depends on timeouts is not usually portable and difficult to debug. // * No socket must be added to any set if you do not intend to check its result after the socket_select() call, and respond appropriately. After socket_select() returns, all sockets in all arrays must be checked. Any socket that is available for writing must be written to, and any socket available for reading must be read from. // * If you read/write to a socket returns in the arrays be aware that they do not necessarily read/write the full amount of data you have requested. Be prepared to even only be able to read/write a single byte. // * It's common to most socket implementations that the only exception caught with the except array is out-of-bound data received on a socket. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-select.php // ========== SOCKET_SELECT - END // SYNTAX: // int|false socket_select(array& $read, array& $write, array& $except, int $seconds, int $microseconds) return $return_socket_select; // int|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_SELECT // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_SEND // ============================== PUBLIC // ============================== ABOUT // Sends data to a connected socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_send() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_send($socket, $data, $length, $flags) { $return_socket_send = false; // ========== SOCKET_SEND - BEGIN // ===== ABOUT // Sends data to a connected socket // ===== DESCRIPTION // The function socket_send() sends length bytes to the socket socket from data. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_send( // Socket $socket, // string $data, // int $length, // int $flags // ): int|false // ===== CODE $return_socket_send = socket_send( $socket, // Socket socket - A Socket instance created with socket_create() or socket_accept(). $data, // string data - A buffer containing the data that will be sent to the remote host. $length, // int length - The number of bytes that will be sent to the remote host from data. $flags // int flags - The value of flags can be any combination of the following flags, joined with the binary OR (|) operator. // // Possible values for flags // MSG_OOB - Send OOB (out-of-band) data. // MSG_EOR - Indicate a record mark. The sent data completes the record. // MSG_EOF - Close the sender side of the socket and include an appropriate notification of this at the end of the sent data. The sent data completes the transaction. // MSG_DONTROUTE - Bypass routing, use direct interface. ); // Return Values // socket_send() returns the number of bytes sent, or false on error. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-send.php // ========== SOCKET_SEND - END // SYNTAX: // int|false socket_send(Socket $socket, string $data, int $length, int $flags) return $return_socket_send; // int|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_SEND // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_SENDMSG // ============================== OFFLINE // ============================== ABOUT // Send a message. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_sendmsg() - PHP_5 >= PHP_5_5_0, PHP_7, PHP_8 // ============================== CODE /* function php_other_sockets_socket_sendmsg($socket, $message, $flags = 0) { $return_socket_sendmsg = false; // ========== SOCKET_SENDMSG - BEGIN // ===== ABOUT // Send a message // ===== DESCRIPTION // Warning: This function is currently not documented; only its argument list is available. // ===== SUPPORTED // PHP_5 >= PHP_5_5_0, PHP_7, PHP_8 // ===== SYNTAX // socket_sendmsg(Socket $socket, array $message, int $flags = 0): int|false // ===== CODE $return_socket_sendmsg = socket_sendmsg( $socket, // Socket socket $message, // array message $flags // int flags ); // Return Values // Returns the number of bytes sent, or false on failure. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-sendmsg.php // ========== SOCKET_SENDMSG - END // SYNTAX: // int|false socket_sendmsg(Socket $socket, array $message, int $flags = 0) return $return_socket_sendmsg; // int|false } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_SENDMSG // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_SENDTO // ============================== PUBLIC // ============================== ABOUT // Sends a message to a socket, whether it is connected or not. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_sendto() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_sendto($socket, $data, $length, $flags, $address, $port = null) { $return_socket_sendto = false; // ========== SOCKET_SENDTO - BEGIN // ===== ABOUT // Sends a message to a socket, whether it is connected or not // ===== DESCRIPTION // The function socket_sendto() sends length bytes from data through the socket socket to the port at the address address. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_sendto( // Socket $socket, // string $data, // int $length, // int $flags, // string $address, // ?int $port = null // ): int|false // ===== CODE $return_socket_sendto = socket_sendto( $socket, // Socket socket - A Socket instance created using socket_create(). $data, // string data - The sent data will be taken from buffer data. $length, // int length - length bytes from data will be sent. $flags, // int flags - The value of flags can be any combination of the following flags, joined with the binary OR (|) operator. // // Possible values for flags // MSG_OOB - Send OOB (out-of-band) data. // MSG_EOR - Indicate a record mark. The sent data completes the record. // MSG_EOF - Close the sender side of the socket and include an appropriate notification of this at the end of the sent data. The sent data completes the transaction. // MSG_DONTROUTE - Bypass routing, use direct interface. $address, // string address - IP address of the remote host. $port // int port - port is the remote port number at which the data will be sent. ); // Return Values // socket_sendto() returns the number of bytes sent to the remote host, or false if an error occurred. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // 8.0.0 - port is nullable now. // // [examples] // Examples // [example] // Example #1 socket_sendto() Example // [php] // $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // // $msg = "Ping !"; // $len = strlen($msg); // // socket_sendto($sock, $msg, $len, 0, '127.0.0.1', 1223); // socket_close($sock); // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-14) // URL: https://www.php.net/manual/en/function.socket-sendto.php // ========== SOCKET_SENDTO - END // SYNTAX: // int|false socket_sendto(Socket $socket, string $data, int $length, int $flags, string $address, int $port = null) return $return_socket_sendto; // int|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_SENDTO // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_SET_BLOCK // ============================== PUBLIC // ============================== ABOUT // Sets blocking mode on a socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_set_block() - PHP_4 >= PHP_4_2_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_set_block($socket) { $return_socket_set_block = false; // ========== SOCKET_SET_BLOCK - BEGIN // ===== ABOUT // Sets blocking mode on a socket // ===== DESCRIPTION // The socket_set_block() function removes the O_NONBLOCK flag on the socket specified by the socket parameter. // When an operation (e.g. receive, send, connect, accept, ...) is performed on a blocking socket, the script will pause its execution until it receives a signal or it can perform the operation. // ===== SUPPORTED // PHP_4 >= PHP_4_2_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_set_block(Socket $socket): bool // ===== CODE $return_socket_set_block = socket_set_block( $socket // Socket socket - A Socket instance created with socket_create() or socket_accept(). ); // Return Values // Returns true on success or false on failure. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // // [examples] // Examples // [example] // Example #1 socket_set_block() example // [php] // $socket = socket_create_listen(1223); // socket_set_block($socket); // // socket_accept($socket); // [/php] // This example creates a listening socket on all interfaces on port 1223 and sets the socket to O_BLOCK mode. socket_accept() will hang until there is a connection to accept. // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-14) // URL: https://www.php.net/manual/en/function.socket-set-block.php // ========== SOCKET_SET_BLOCK - END // SYNTAX: // bool socket_set_block(Socket $socket) return $return_socket_set_block; // bool } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_SET_BLOCK // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_SET_NONBLOCK // ============================== PUBLIC // ============================== ABOUT // Sets nonblocking mode for file descriptor fd. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_set_nonblock() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_set_nonblock($socket) { $return_socket_set_nonblock = false; // ========== SOCKET_SET_NONBLOCK - BEGIN // ===== ABOUT // Sets nonblocking mode for file descriptor fd // ===== DESCRIPTION // The socket_set_nonblock() function sets the O_NONBLOCK flag on the socket specified by the socket parameter. // When an operation (e.g. receive, send, connect, accept, ...) is performed on a non-blocking socket, the script will not pause its execution until it receives a signal or it can perform the operation. Rather, if the operation would result in a block, the called function will fail. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_set_nonblock(Socket $socket): bool // ===== CODE $return_socket_set_nonblock = socket_set_nonblock( $socket // Socket socket - A Socket instance created with socket_create() or socket_accept(). ); // Return Values // Returns true on success or false on failure. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // // [examples] // Examples // [example] // Example #1 socket_set_nonblock() example // [php] // $socket = socket_create_listen(1223); // socket_set_nonblock($socket); // // socket_accept($socket); // [/php] // This example creates a listening socket on all interfaces on port 1223 and sets the socket to O_NONBLOCK mode. socket_accept() will immediately fail unless there is a pending connection exactly at this moment. // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-14) // URL: https://www.php.net/manual/en/function.socket-set-nonblock.php // ========== SOCKET_SET_NONBLOCK - END // SYNTAX: // bool socket_set_nonblock(Socket $socket) return $return_socket_set_nonblock; // bool } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_SET_NONBLOCK // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_SET_OPTION // ============================== PUBLIC // ============================== ABOUT // Sets socket options for the socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_set_option() - PHP_4 >= PHP_4_3_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_set_option($socket, $level, $option, $value) { $return_socket_set_option = false; // ========== SOCKET_SET_OPTION - BEGIN // ===== ABOUT // Sets socket options for the socket // ===== DESCRIPTION // The socket_set_option() function sets the option specified by the option parameter, at the specified protocol level, to the value pointed to by the value parameter for the socket. // ===== SUPPORTED // PHP_4 >= PHP_4_3_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_set_option( // Socket $socket, // int $level, // int $option, // array|string|int $value // ): bool // ===== CODE $return_socket_set_option = socket_set_option( $socket, // Socket socket - A Socket instance created with socket_create() or socket_accept(). $level, // int level - The level parameter specifies the protocol level at which the option resides. For example, to set options at the socket level, a level parameter of SOL_SOCKET would be used. Other levels, such as TCP, can be used by specifying the protocol number of that level. Protocol numbers can be found by using the getprotobyname() function. $option, // int option - The available socket options are the same as those for the socket_get_option() function. $value // array|string|int value - The option value. ); // Return Values // Returns true on success or false on failure. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // // [examples] // Examples // [example] // Example #1 socket_set_option() example // [php] // $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // // if (!is_resource($socket)) { // echo 'Unable to create socket: '. socket_strerror(socket_last_error()) . PHP_EOL; // } // // if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) { // echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL; // } // // if (!socket_bind($socket, '127.0.0.1', 1223)) { // echo 'Unable to bind socket: '. socket_strerror(socket_last_error()) . PHP_EOL; // } // // $rval = socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR); // // if ($rval === false) { // echo 'Unable to get socket option: '. socket_strerror(socket_last_error()) . PHP_EOL; // } else if ($rval !== 0) { // echo 'SO_REUSEADDR is set on socket !' . PHP_EOL; // } // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-13) // URL: https://www.php.net/manual/en/function.socket-set-option.php // ========== SOCKET_SET_OPTION - END // SYNTAX: // bool socket_set_option(Socket $socket, int $level, int $option, array|string|int $value) return $return_socket_set_option; // bool } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_SET_OPTION // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_SETOPT // ============================== PUBLIC // ============================== ABOUT // Sets socket options for the socket. // // socket_setopt - Alias of socket_set_option(). // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_setopt() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_setopt($socket, $level, $option, $value) { $return_socket_setopt = false; // ========== SOCKET_SETOPT - BEGIN // ===== ABOUT // socket_setopt - Alias of socket_set_option() // ===== DESCRIPTION // This function is an alias of: socket_set_option(). // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // ===== CODE $return_socket_setopt = socket_setopt( $socket, // Socket socket $level, // int level $option, // int option $value // array|string|int value ); // Return // bool // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-13) // URL: https://www.php.net/manual/en/function.socket-setopt.php // ========== SOCKET_SETOPT - END // ========== SOCKET_SET_OPTION - BEGIN // ===== ABOUT // Sets socket options for the socket // ===== DESCRIPTION // The socket_set_option() function sets the option specified by the option parameter, at the specified protocol level, to the value pointed to by the value parameter for the socket. // ===== SUPPORTED // PHP_4 >= PHP_4_3_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_set_option( // Socket $socket, // int $level, // int $option, // array|string|int $value // ): bool // ===== CODE // $return_socket_set_option = socket_set_option( // $socket, // Socket socket - A Socket instance created with socket_create() or socket_accept(). // $level, // int level - The level parameter specifies the protocol level at which the option resides. For example, to set options at the socket level, a level parameter of SOL_SOCKET would be used. Other levels, such as TCP, can be used by specifying the protocol number of that level. Protocol numbers can be found by using the getprotobyname() function. // $option, // int option - The available socket options are the same as those for the socket_get_option() function. // $value // array|string|int value - The option value. // ); // Return Values // Returns true on success or false on failure. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // // [examples] // Examples // [example] // Example #1 socket_set_option() example // [php] // $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // // if (!is_resource($socket)) { // echo 'Unable to create socket: '. socket_strerror(socket_last_error()) . PHP_EOL; // } // // if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) { // echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL; // } // // if (!socket_bind($socket, '127.0.0.1', 1223)) { // echo 'Unable to bind socket: '. socket_strerror(socket_last_error()) . PHP_EOL; // } // // $rval = socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR); // // if ($rval === false) { // echo 'Unable to get socket option: '. socket_strerror(socket_last_error()) . PHP_EOL; // } else if ($rval !== 0) { // echo 'SO_REUSEADDR is set on socket !' . PHP_EOL; // } // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-13) // URL: https://www.php.net/manual/en/function.socket-set-option.php // ========== SOCKET_SET_OPTION - END // SYNTAX: // bool socket_setopt(Socket $socket, int $level, int $option, array|string|int $value) return $return_socket_setopt; // bool } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_SETOPT // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_SHUTDOWN // ============================== PUBLIC // ============================== ABOUT // Shuts down a socket for receiving, sending, or both. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_shutdown() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_shutdown($socket, $mode = 2) { $return_socket_shutdown = false; // ========== SOCKET_SHUTDOWN - BEGIN // ===== ABOUT // Shuts down a socket for receiving, sending, or both // ===== DESCRIPTION // The socket_shutdown() function allows you to stop incoming, outgoing or all data (the default) from being sent through the socket // Note: The associated buffer, or buffers, may or may not be emptied. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_shutdown(Socket $socket, int $mode = 2): bool // ===== CODE $return_socket_shutdown = socket_shutdown( $socket, // socket - A Socket instance created with socket_create(). $mode // mode - The value of mode can be one of the following: // possible values for mode // 0 - Shutdown socket reading // 1 - Shutdown socket writing // 2 - Shutdown socket reading and writing ); // Return Values // Returns true on success or false on failure. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-shutdown.php // ========== SOCKET_SHUTDOWN - END // SYNTAX: // bool socket_shutdown(Socket $socket, int $mode = 2) return $return_socket_shutdown; // bool } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_SHUTDOWN // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_STRERROR // ============================== PUBLIC // ============================== ABOUT // Return a string describing a socket error. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_strerror() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_strerror($error_code) { $return_socket_strerror = null; // ========== SOCKET_STRERROR - BEGIN // ===== ABOUT // Return a string describing a socket error // ===== DESCRIPTION // socket_strerror() takes as its error_code parameter a socket error code as returned by socket_last_error() and returns the corresponding explanatory text. // Note: Although the error messages generated by the socket extension are in English, the system messages retrieved with this function will appear depending on the current locale (LC_MESSAGES). // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_strerror(int $error_code): string // ===== CODE $return_socket_strerror = socket_strerror( $error_code // int error_code - A valid socket error number, likely produced by socket_last_error(). ); // Return Values // Returns the error message associated with the error_code parameter. // // [examples] // Examples // [example] // Example #1 socket_strerror() example // [php] // if (false == ($socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) { // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; // } // // if (false == (@socket_bind($socket, '127.0.0.1', 80))) { // echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n"; // } // [/php] // The expected output from the above example (assuming the script is not run with root privileges): // [result] // socket_bind() failed: reason: Permission denied // [/result] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-13) // URL: https://www.php.net/manual/en/function.socket-strerror.php // ========== SOCKET_STRERROR - END // SYNTAX: // string socket_strerror(int $error_code) return $return_socket_strerror; // string } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_STRERROR // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_WRITE // ============================== PUBLIC // ============================== ABOUT // Write to a socket. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_write() - PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_other_sockets_socket_write($socket, $data, $length = null) { $return_socket_write = false; // ========== SOCKET_WRITE - BEGIN // ===== ABOUT // Write to a socket // ===== DESCRIPTION // The function socket_write() writes to the socket from the given data. // ===== SUPPORTED // PHP_4 >= PHP_4_1_0, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // socket_write(Socket $socket, string $data, ?int $length = null): int|false // ===== CODE $return_socket_write = socket_write( $socket, // Socket socket $data, // string data - The buffer to be written. $length // int length - The optional parameter length can specify an alternate length of bytes written to the socket. If this length is greater than the buffer length, it is silently truncated to the length of the buffer. ); // Return Values // Returns the number of bytes successfully written to the socket or false on failure. The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual explanation of the error. // Note: It is perfectly valid for socket_write() to return zero which means no bytes have been written. Be sure to use the === operator to check for false in case of an error. // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // 8.0.0 - length is nullable now. // // Notes // Note: socket_write() does not necessarily write all bytes from the given buffer. It's valid that, depending on the network buffers etc., only a certain amount of data, even one byte, is written though your buffer is greater. You have to watch out so you don't unintentionally forget to transmit the rest of your data. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-write.php // ========== SOCKET_WRITE - END // SYNTAX: // int|false socket_write(Socket $socket, string $data, int $length = null) return $return_socket_write; // int|false } // ============================== END // PHP_OTHER_SOCKETS_SOCKET_WRITE // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_WSAPROTOCOL_INFO_EXPORT // ============================== OFFLINE // ============================== ABOUT // Exports the WSAPROTOCOL_INFO Structure. // ============================== SUPPORT // PHP_7 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_wsaprotocol_info_export() - PHP_7 >= PHP_7_3_0, PHP_8 // ============================== CODE /* function php_other_sockets_socket_wsaprotocol_info_export($socket, $process_id) { $return_socket_wsaprotocol_info_export = false; // ========== SOCKET_WSAPROTOCOL_INFO_EXPORT - BEGIN // ===== ABOUT // Exports the WSAPROTOCOL_INFO Structure // ===== DESCRIPTION // Exports the WSAPROTOCOL_INFO structure into shared memory and returns an identifier to be used with socket_wsaprotocol_info_import(). The exported ID is only valid for the given process_id. // Note: This function is available only on Windows. // ===== SUPPORTED // PHP_7 >= PHP_7_3_0, PHP_8 // ===== SYNTAX // socket_wsaprotocol_info_export(Socket $socket, int $process_id): string|false // ===== CODE $return_socket_wsaprotocol_info_export = socket_wsaprotocol_info_export( $socket, // Socket socket - A Socket instance. $process_id // int process_id - The ID of the process which will import the socket. ); // Return Values // Returns an identifier to be used for the import, or false on failure // // Changelog // Version - Description // 8.0.0 - socket is a Socket instance now; previously, it was a resource. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-wsaprotocol-info-export.php // ========== SOCKET_WSAPROTOCOL_INFO_EXPORT - END // SYNTAX: // string|false socket_wsaprotocol_info_export(Socket $socket, int $process_id) return $return_socket_wsaprotocol_info_export; // string|false } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_WSAPROTOCOL_INFO_EXPORT // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_WSAPROTOCOL_INFO_IMPORT // ============================== OFFLINE // ============================== ABOUT // Imports a Socket from another Process. // ============================== SUPPORT // PHP_7 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_wsaprotocol_info_import() - PHP_7 >= PHP_7_3_0, PHP_8 // ============================== CODE /* function php_other_sockets_socket_wsaprotocol_info_import($info_id) { $return_socket_wsaprotocol_info_import = false; // ========== SOCKET_WSAPROTOCOL_INFO_IMPORT - BEGIN // ===== ABOUT // Imports a Socket from another Process // ===== DESCRIPTION // Imports a socket which has formerly been exported from another process. // Note: This function is available only on Windows. // ===== SUPPORTED // PHP_7 >= PHP_7_3_0, PHP_8 // ===== SYNTAX // socket_wsaprotocol_info_import(string $info_id): Socket|false // ===== CODE $return_socket_wsaprotocol_info_import = socket_wsaprotocol_info_import( $info_id // string info_id - The ID which has been returned by a former call to socket_wsaprotocol_info_export(). ); // Return Values // Returns a Socket instance on success, or false on failure // // Changelog // Version - Description // 8.0.0 - On success, this function returns a Socket instance now; previously, a resource was returned. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-wsaprotocol-info-import.php // ========== SOCKET_WSAPROTOCOL_INFO_IMPORT - END // SYNTAX: // Socket|false socket_wsaprotocol_info_import(string $info_id) return $return_socket_wsaprotocol_info_import; // Socket|false } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_WSAPROTOCOL_INFO_IMPORT // ============================== // ============================== BEGIN // PHP_OTHER_SOCKETS_SOCKET_WSAPROTOCOL_INFO_RELEASE // ============================== OFFLINE // ============================== ABOUT // Releases an exported WSAPROTOCOL_INFO Structure. // ============================== SUPPORT // PHP_7 - PHP_8 // ============================== USING FUNCTIONS (1) // socket_wsaprotocol_info_release() - PHP_7 >= PHP_7_3_0, PHP_8 // ============================== CODE /* function php_other_sockets_socket_wsaprotocol_info_release($info_id) { $return_socket_wsaprotocol_info_release = false; // ========== SOCKET_WSAPROTOCOL_INFO_RELEASE - BEGIN // ===== ABOUT // Releases an exported WSAPROTOCOL_INFO Structure // ===== DESCRIPTION // Releases the shared memory corresponding to the given info_id. // Note: This function is available only on Windows. // ===== SUPPORTED // PHP_7 >= PHP_7_3_0, PHP_8 // ===== SYNTAX // socket_wsaprotocol_info_release(string $info_id): bool // ===== CODE $return_socket_wsaprotocol_info_release = socket_wsaprotocol_info_release( $info_id // string info_id - The ID which has been returned by a former call to socket_wsaprotocol_info_export(). ); // Return Values // Returns true on success or false on failure. // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.socket-wsaprotocol-info-release.php // ========== SOCKET_WSAPROTOCOL_INFO_RELEASE - END // SYNTAX: // bool socket_wsaprotocol_info_release(string $info_id) return $return_socket_wsaprotocol_info_release; // bool } */ // ============================== END // PHP_OTHER_SOCKETS_SOCKET_WSAPROTOCOL_INFO_RELEASE // ============================== // ============================== END // PHP_OTHER_SOCKETS // ============================== ?>