PHP stream_socket_client() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_socket_client() opens a client connection to a remote or local socket address.

The address looks like tcp://127.0.0.1:8080 or ssl://example.com:443. Optional error code and message variables explain failures. You can pass a timeout, flags, and a stream context. For TLS, keep verify_peer enabled in production SSL contexts.

Syntax

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

PHP
$fp = stream_socket_client('tcp://127.0.0.1:8080', $errno, $errstr, 5);
CallWhat it does
stream_socket_client('tcp://127.0.0.1:8080')Connects to a local TCP port and returns a stream.
stream_socket_client($addr, $errno, $errstr, 3)Connects with a 3-second timeout and fills error variables on failure.
stream_socket_client('ssl://example.com:443', $e, $m, 10, STREAM_CLIENT_CONNECT, $ctx)Opens a TLS client using a stream context.

A listening server must already be running for a real TCP connect. Tutorials often show fixed sample output with that comment. Prefer stream_socket_pair() when you only need two local ends offline. Never disable verify_peer for production TLS.

Library declarations
PHP
stream_socket_client(string $address, ?int &$error_code = null, ?string &$error_message = null, ?float $timeout = null, int $flags = STREAM_CLIENT_CONNECT, ?resource $context = null): resource|false

Parameters

ParameterDescription
addressRequired. Socket URL such as tcp://host:port or ssl://host:443.
error_codeOptional. By-reference integer filled on failure.
error_messageOptional. By-reference string filled on failure.
timeoutOptional. Connect timeout in seconds. null uses default_socket_timeout.
flagsOptional. Connection flags such as STREAM_CLIENT_CONNECT.
contextOptional. A stream context from stream_context_create().

Return Value

Return ValueDescription
resourceAn open client stream on success.
falseThe connection failed.

PHP Examples

Example 1 — Client Call Shape

Show the usual client call with fixed sample text. A real connect needs a listening server.

PHP
<?php
// Needs a listening server, e.g. stream_socket_server('tcp://127.0.0.1:8080').
// Illustrative sample (not a live network connect):
$errno = 0;
$errstr = '';
echo 'target: tcp://127.0.0.1:8080', PHP_EOL;
echo 'on success: stream resource', PHP_EOL;
echo 'on failure: false, errno=', $errno, ' errstr=', $errstr, PHP_EOL;

How It Works

  1. The address uses a transport prefix like tcp://.
  2. Optional $errno and $errstr explain failures.
  3. A live server must already listen on that port.
  4. On success you get a stream for fread()/fwrite().

Example 2 — Offline Pair Instead of Client

Practice sending data between two ends with stream_socket_pair() when no server is available.

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

fwrite($clientLike, "GET /\n");
echo fread($serverLike, 6), PHP_EOL;

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

How It Works

  1. stream_socket_pair() gives two connected streams offline.
  2. One end acts like a client writer.
  3. The other end reads the same bytes.
  4. Use this pattern when teaching without a network listener.

Example 3 — SSL Context Reminder

Show a safe SSL context sketch that keeps peer verification on.

PHP
<?php
$ctx = stream_context_create([
    'ssl' => [
        'verify_peer' => true,
        'verify_peer_name' => true,
    ],
]);
echo 'verify_peer=', var_export(stream_context_get_options($ctx)['ssl']['verify_peer'], true), PHP_EOL;
echo 'keep verification on for production TLS', PHP_EOL;

How It Works

  1. stream_context_create() builds options for ssl:// clients.
  2. verify_peer true checks the certificate chain.
  3. verify_peer_name true checks the host name.
  4. Do not turn these off for production traffic.

Frequently Asked Questions

It opens a client socket connection to an address such as tcp://host:port and returns a stream.
Pass $errno and $errstr by reference. On failure they hold the error code and message.
No. The timeout mainly limits connecting. Use stream_set_timeout() for later read/write waits.
Use an ssl:// or tls:// address and a stream context. Keep verify_peer enabled for production.
stream_socket_pair() creates two local connected ends without a listening server.

Next: stream_socket_enable_crypto()

stream_socket_enable_crypto() turns encryption on or off for an already connected stream.

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