PHP stream_socket_recvfrom() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_socket_recvfrom() reads up to a given number of bytes from a socket stream and can capture the sender address.

It is useful for connectionless or addressed receives. Pass the max length, optional flags, and an optional by-reference $address. For offline teaching, pair it with stream_socket_pair() and stream_socket_sendto().

Syntax

Call stream_socket_recvfrom() with the values you need. Start with this line.

PHP
$data = stream_socket_recvfrom($socket, 1024, 0, $address);
CallWhat it does
stream_socket_recvfrom($socket, 1024)Reads up to 1024 bytes from the socket.
stream_socket_recvfrom($socket, 64, 0, $from)Reads data and stores the sender address in $from.
stream_socket_recvfrom($sock, 8)Reads a small fixed-size message.

Returns the received string, or false on failure. Unlike fread(), it can fill $address with the remote side. On a stream pair, sendto/recvfrom still move bytes between the two ends for simple demos.

Library declarations
PHP
stream_socket_recvfrom(resource $socket, int $length, int $flags = 0, ?string &$address = null): string|false

Parameters

ParameterDescription
socketRequired. A socket stream to read from.
lengthRequired. Maximum number of bytes to read.
flagsOptional. Socket flags. Defaults to 0.
addressOptional. By-reference string filled with the sender address when available.

Return Value

Return ValueDescription
stringThe bytes that were received.
falseThe read failed.

PHP Examples

Example 1 — Receive Across a Socket Pair

Send with stream_socket_sendto() and receive with stream_socket_recvfrom().

PHP
<?php
$pair = stream_socket_pair(STREAM_PF_INET, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
[$a, $b] = $pair;

stream_socket_sendto($a, 'hello');
$msg = stream_socket_recvfrom($b, 5);
echo $msg, PHP_EOL;

fclose($a);
fclose($b);

How It Works

  1. Create an offline pair of connected sockets.
  2. sendto writes on end $a.
  3. recvfrom reads the same bytes on end $b.
  4. Close both streams afterward.

Example 2 — Capture a Length Limit

Ask for fewer bytes than were sent and show the truncated read.

PHP
<?php
$pair = stream_socket_pair(STREAM_PF_INET, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
[$a, $b] = $pair;

stream_socket_sendto($a, 'ABCDEFGH');
$part = stream_socket_recvfrom($b, 4);
echo $part, PHP_EOL;

fclose($a);
fclose($b);

How It Works

  1. Eight bytes are sent on $a.
  2. recvfrom asks for only 4 bytes.
  3. The returned string is ABCD.
  4. Remaining bytes stay available for a later read.

Example 3 — Address Variable Placeholder

Show the $address argument with a fixed sample peer string for tutorials.

PHP
<?php
// On UDP or accepted TCP clients, $address may become '127.0.0.1:54321'.
$address = '127.0.0.1:54321';
echo 'sample sender: ', $address, PHP_EOL;
echo 'recvfrom fills $address when the transport provides it', PHP_EOL;

How It Works

  1. Pass $address by reference as the fourth argument.
  2. Some transports fill it with the sender name.
  3. Logging the address helps debug packet sources.
  4. Pair demos may leave address empty depending on platform.

Frequently Asked Questions

It reads up to length bytes from a socket and can store the sender address in a by-reference variable.
Both can read bytes. recvfrom adds the optional address parameter used with many socket transports.
Yes. You get only the bytes that were available, up to the maximum you asked for.
Use stream_socket_pair() with stream_socket_sendto() on the other end.
The receive failed. Check the stream state and any warnings before retrying.

Next: stream_socket_sendto()

stream_socket_sendto() sends data on a socket stream, optionally to a specific address.

stream_socket_sendto() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful