Function name: socket_recv()
Function description: The socket_recv() function receives data from the connected socket.
Applicable version: PHP 4 >= 4.1.0, PHP 5, PHP 7
Usage: int socket_recv ( resource $socket, string &$buf, int $len, int $flags )
parameter:
Return value: When successful, return the number of bytes of the received data. On failure, return false and may set socket_last_error() to get the error code.
Example: The following example demonstrates how to use the socket_recv() function to receive data from a connected socket.
<?php // 创建一个TCP socket $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // 连接到服务器 socket_connect($socket, '127.0.0.1', 8080); // 发送数据到服务器 $message = "Hello, server!"; socket_send($socket, $message, strlen($message), 0); // 接收服务器返回的数据 $receivedData = ''; socket_recv($socket, $receivedData, 1024, 0); // 关闭socket连接 socket_close($socket); // 输出接收到的数据 echo $receivedData; ?-->In the example above, a TCP socket is first created and connected to the server. Then, use the socket_send() function to send the data to the server. Next, use the socket_recv() function to receive the data returned by the server and store it in the $receivedData variable. Finally, close the socket connection and output the received data to the screen.