Content · Web Development · PHP

PHP socket_accept() Function: Usage and Examples

Accepts an incoming connection from a listening.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Accepts an incoming connection from a listening socket. This guide demonstrates its use in PHP code and explains when the result or side effect needs extra checks.

What It Does


Accepts an incoming connection from a listening socket.

Syntax and Parameters


The examples below show a real call to socket_accept with suitable arguments. Read the function call in context: its connection, handle, input data or other prerequisites are created before use.

Requirements: Sockets extension; operating-system networking support.

These examples are for an isolated development environment. Hostnames, credentials and remote paths must be replaced with your approved configuration.

Example 1: Core Behavior


Use local loopback or a connected socket pair to avoid unintended public network exposure.

<?php
$listener = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$listener) throw new RuntimeException("Cannot create server socket");
socket_bind($listener, "127.0.0.1", 0);
socket_listen($listener, 2);
socket_set_nonblock($listener);
$client = socket_accept($listener);
echo $client instanceof Socket ? "Accepted" : "No client before timeout";
if ($client) socket_close($client); socket_close($listener);

Observe the returned value or changed resource state. A successful function call can still require additional checks before a higher-level operation is considered successful.

Example 2: Alternative or Failure Case


The operation may return a status or a partial byte count depending on timing and transport.

<?php
$listener = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$listener) throw new RuntimeException("Cannot create server socket");
socket_bind($listener, "127.0.0.1", 0);
socket_listen($listener, 2);
socket_set_nonblock($listener);
$remote = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_getsockname($listener, $ip, $port);
socket_connect($remote, "127.0.0.1", $port);
$client = socket_accept($listener);
echo $client instanceof Socket ? "Accepted" : "No client before timeout";
if ($client) socket_close($client); socket_close($remote); socket_close($listener);

Compare the different inputs, environment assumptions or result checks. Do not use a transport-level success value as proof that all business-level requirements were met.

Practical Example: Applying the Rule


Apply explicit timeouts and check errors before integrating the code into a long-running server.

<?php
$listener = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$listener) throw new RuntimeException("Cannot create server socket");
socket_bind($listener, "127.0.0.1", 0);
socket_listen($listener, 2);
socket_set_nonblock($listener);
$remote = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_getsockname($listener, $ip, $port);
socket_connect($remote, "127.0.0.1", $port);
$client = socket_accept($listener);
if ($client instanceof Socket) { socket_send($remote, "ping", 4, 0); $data=""; socket_recv($client, $data, 16, 0); echo $data; }
if ($client) socket_close($client); socket_close($remote); socket_close($listener);

In an application, convert low-level failures into controlled responses and log only non-sensitive diagnostic context.

Important Behavior and Edge Cases


It can block until a client connects; configure timeouts and avoid unlimited clients.

Network operations can block or read/write only part of a message. Treat end-of-stream, timeout and transient errors separately.

Comparison with Related Functions or Patterns


ApproachWhen it differs
socket_acceptAccepts an incoming connection from a listening socket.
Related API or patternsocket_listen prepares a bound stream socket to accept connections.

Common Beginner Mistakes


MistakeCorrect approach
Omitting address-family checks or timeouts when handling socket resourcesIt can block until a client connects; configure timeouts and avoid unlimited clients.
Substituting a related API without checking its semanticssocket_listen prepares a bound stream socket to accept connections.

Key Takeaways


  • Accepts an incoming connection from a listening socket.
  • It can block until a client connects; configure timeouts and avoid unlimited clients.
  • socket_listen prepares a bound stream socket to accept connections.

Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.

Share this resourceSend this page to someone who may find it useful.