PHP stream_socket_server() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

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

The address looks like tcp://127.0.0.1:8080. Optional error variables explain failures. After a successful listen, call stream_socket_accept() for each client. Offline tutorials often show fixed sample output and note that a real listener is needed for accept/client demos.

Syntax

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

PHP
$server = stream_socket_server('tcp://127.0.0.1:8080', $errno, $errstr);
CallWhat it does
stream_socket_server('tcp://127.0.0.1:0')Binds an ephemeral local port and listens.
stream_socket_server($addr, $errno, $errstr)Creates a server and fills error variables on failure.
stream_socket_server($addr, $e, $m, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $ctx)Creates a server with an explicit context.

Default flags bind and listen. Port 0 lets the OS choose a free port. Use stream_socket_get_name($server, false) to learn the bound address. Prefer stream_socket_pair() when you only need two connected ends without listening.

Library declarations
PHP
stream_socket_server(string $address, ?int &$error_code = null, ?string &$error_message = null, int $flags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, ?resource $context = null): resource|false

Parameters

ParameterDescription
addressRequired. Socket URL such as tcp://127.0.0.1:8080.
error_codeOptional. By-reference integer filled on failure.
error_messageOptional. By-reference string filled on failure.
flagsOptional. Usually STREAM_SERVER_BIND | STREAM_SERVER_LISTEN.
contextOptional. A stream context from stream_context_create().

Return Value

Return ValueDescription
resourceA listening server stream on success.
falseThe server socket could not be created.

PHP Examples

Example 1 — Server Call Shape

Show the usual server setup with fixed sample text for beginners.

PHP
<?php
// Example: $server = stream_socket_server('tcp://127.0.0.1:8080', $errno, $errstr);
// Then: $client = stream_socket_accept($server);
echo 'listen address: tcp://127.0.0.1:8080', PHP_EOL;
echo 'next step: stream_socket_accept()', PHP_EOL;

How It Works

  1. stream_socket_server() binds and listens.
  2. Check false and read $errno/$errstr on failure.
  3. Accept clients with stream_socket_accept().
  4. A live port must be free and permitted.

Example 2 — Offline Pair Instead of Listen

Use a socket pair when you want connected ends without binding a TCP port.

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

fwrite($clientLike, 'hi');
echo fread($serverLike, 2), PHP_EOL;

fclose($serverLike);
fclose($clientLike);

How It Works

  1. A pair gives two ends without listen/accept.
  2. One end stands in for the server side.
  3. The other end stands in for the client.
  4. Great for offline send/receive practice.

Example 3 — Ephemeral Port Idea

Document binding port 0 so the OS picks a free port, with fixed sample output.

PHP
<?php
// Example: $server = stream_socket_server('tcp://127.0.0.1:0', $errno, $errstr);
// $name = stream_socket_get_name($server, false); // e.g. 127.0.0.1:51500
echo 'request port: 0 (OS chooses)', PHP_EOL;
echo 'sample bound name: 127.0.0.1:51500', PHP_EOL;

How It Works

  1. Port 0 asks the OS for a free port.
  2. stream_socket_get_name($server, false) reveals it.
  3. Useful for tests that must avoid collisions.
  4. Still requires a real bind for live use.

Frequently Asked Questions

It creates a server socket that binds and listens on an address such as tcp://127.0.0.1:8080.
Call stream_socket_accept() to accept each incoming client connection.
Pass $error_code and $error_message by reference. They are filled when the call returns false.
Yes. Use stream_socket_pair() for offline connected streams.
The operating system chooses an unused port. Read it back with stream_socket_get_name().

Next: stream_socket_shutdown()

stream_socket_shutdown() shuts down part or all of a full-duplex socket connection.

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