PHP stream_socket_sendto() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

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

Pass the payload string, optional flags, and an address when the transport needs one (common with UDP). It returns the number of bytes sent, or false on failure. Pair it with stream_socket_recvfrom() and stream_socket_pair() for offline demos.

Syntax

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

PHP
$n = stream_socket_sendto($socket, $data);
CallWhat it does
stream_socket_sendto($socket, 'ping')Sends the text ping on a connected socket.
stream_socket_sendto($socket, $payload, 0, $addr)Sends to an explicit address when required.
stream_socket_sendto($sock, "hi\n")Sends a short line including a newline.

On a connected stream or pair end, you can often omit the address. For UDP-style sends, provide host:port in $address. Check the returned byte count against strlen($data).

Library declarations
PHP
stream_socket_sendto(resource $socket, string $data, int $flags = 0, string $address = ""): int|false

Parameters

ParameterDescription
socketRequired. A socket stream to write to.
dataRequired. The bytes to send.
flagsOptional. Socket flags. Defaults to 0.
addressOptional. Destination address string when the call needs one.

Return Value

Return ValueDescription
intThe number of bytes successfully sent.
falseThe send failed.

PHP Examples

Example 1 — Send and Count Bytes

Send a short string across a socket pair and print how many bytes went out.

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

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

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

How It Works

  1. Create an offline connected pair.
  2. sendto returns 5 for the five-character payload.
  3. recvfrom on the other end reads hello.
  4. Close both ends.

Example 2 — Empty Address on a Connected Pair

Omit a destination address when the stream is already connected.

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

$n = stream_socket_sendto($a, 'OK', 0, '');
echo $n === 2 ? 'ok' : 'fail', PHP_EOL;
echo stream_socket_recvfrom($b, 2), PHP_EOL;

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

How It Works

  1. An empty address is fine on a connected pair end.
  2. The return value 2 matches strlen('OK').
  3. The other end receives OK.
  4. UDP demos would pass a real host:port instead.

Example 3 — Illustrative UDP Address

Show the address argument shape with fixed tutorial text (listening server required for real UDP).

PHP
<?php
// Example: stream_socket_sendto($sock, 'hi', 0, '127.0.0.1:9999');
// Needs a real listening UDP server for a live send.
echo 'sample address: 127.0.0.1:9999', PHP_EOL;
echo 'sendto uses address for connectionless transports', PHP_EOL;

How It Works

  1. UDP-style sends need a destination address.
  2. The string looks like host:port.
  3. A listening server must exist for a real network send.
  4. Use stream_socket_pair() when practicing offline.

Frequently Asked Questions

It sends a string on a socket stream and returns how many bytes were sent, or false on failure.
When the transport is connectionless (such as UDP) or otherwise requires an explicit destination.
Create a stream_socket_pair(), sendto on one end, and recvfrom on the other.
Only part of the data was sent. Send the remainder in another call if you need the full payload delivered.
fwrite() also writes to streams. sendto is the socket-oriented form with flags and an optional address.

Next: stream_socket_server()

stream_socket_server() creates a server socket that binds and listens on an address.

stream_socket_server() 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