socket_create
Create sockets (communication endpoint)
Function: socket_create()
Applicable version: PHP 4, PHP 5, PHP 7
Usage: The socket_create() function is used to create a socket resource.
Syntax: resource socket_create(int $domain, int $type, int $protocol)
parameter:
Return value: Returns a socket resource when successful, and returns false when failure.
Example:
<?php // 创建一个TCP套接字$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { echo "套接字创建失败: " . socket_strerror(socket_last_error()) . PHP_EOL; } else { echo "套接字创建成功!" . PHP_EOL; // 关闭套接字socket_close($socket); } ?>
In the above example, a TCP socket is created using the socket_create() function, which specifies that the protocol family is IPv4 (AF_INET), the socket type is stream socket (SOCK_STREAM), and the protocol is TCP (SOL_TCP). If the creation is successful, print "Socket creation is successful!", otherwise print the error message that failed to create. Finally, use the socket_close() function to close the socket.