Current Location: Home> Function Categories> socket_recv

socket_recv

Receive data from connected socket
Name:socket_recv
Category:Sockets
Programming Language:php
One-line Description:Receive data from connected socket

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:

  • $socket: Connected socket resource, created through socket_create() and socket_connect().
  • &$buf: The received data will be stored in this variable as a string.
  • $len: The maximum length of data to be received.
  • $flags: Optional parameter to specify additional reception options. Commonly used options are MSG_OOB (process out-of-band data) and MSG_WAITALL (waiting for all data to arrive).

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.

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.

Similar Functions
Popular Articles