{
    "slug": "php_socket_programming",
    "term": "Socket Programming with PHP",
    "category": "php",
    "difficulty": "advanced",
    "short": "PHP supports raw socket programming via the Sockets extension (socket_create) and stream sockets (stream_socket_client) — used for custom protocols, WebSocket servers, and network tools.",
    "long": "Two socket APIs in PHP: (1) Sockets extension (socket_create, socket_bind, socket_listen, socket_accept) — low-level, POSIX-like. (2) Stream sockets (stream_socket_client, stream_socket_server) — higher level, supports SSL, uses fread/fwrite. For most cases, use Guzzle (HTTP), AMQP libraries (queues), or ReactPHP/Swoole for async sockets. Raw sockets are useful for: custom TCP/UDP protocols, network diagnostics, building WebSocket servers. Non-blocking sockets with socket_set_nonblock and socket_select() enable handling multiple connections. PHP's socket support is blocking by default — use ReactPHP or Swoole for production async networking.",
    "aliases": [],
    "tags": [
        "php",
        "sockets",
        "networking",
        "cli",
        "advanced"
    ],
    "misconception": "PHP can't do real network server programming — it can, but blocking I/O limits scalability. Use ReactPHP/Swoole for production async socket servers.",
    "why_it_matters": "Understanding PHP socket APIs is essential for building custom protocol handlers, testing network services, and understanding how framework HTTP servers work.",
    "common_mistakes": [
        "Using blocking sockets in production servers — limits to one connection at a time.",
        "Not setting SO_REUSEADDR — port in use errors after restart.",
        "Not handling partial reads — TCP streams can split messages across packets."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_sapi_types",
        "php_pcntl_signals",
        "async_processing",
        "oauth2"
    ],
    "prerequisites": [
        "php_sapi_types",
        "php_pcntl_signals"
    ],
    "refs": [
        "https://www.php.net/manual/en/book.sockets.php",
        "https://reactphp.org"
    ],
    "bad_code": "// Blocking TCP server — handles one connection at a time:\n$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);\nsocket_bind($socket, '0.0.0.0', 8080);\nsocket_listen($socket);\n$client = socket_accept($socket); // Blocks until connection",
    "good_code": "// Non-blocking with stream sockets:\n$server = stream_socket_server('tcp://0.0.0.0:8080', $errno, $errstr);\nstream_set_blocking($server, false);\n\nwhile (true) {\n    $client = @stream_socket_accept($server, 0);\n    if ($client) {\n        $data = fread($client, 1024);\n        fwrite($client, \"HTTP/1.1 200 OK\\r\\n\\r\\nHello\");\n        fclose($client);\n    }\n    // In production: use ReactPHP or Swoole for proper async\n}",
    "quick_fix": "Use stream_socket_client/server for SSL and simpler API. For production async: use ReactPHP or Swoole. Always set SO_REUSEADDR. Handle partial TCP reads.",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_socket_programming",
        "html_url": "https://codeclaritylab.com/glossary/php_socket_programming",
        "json_url": "https://codeclaritylab.com/glossary/php_socket_programming.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Socket Programming with PHP](https://codeclaritylab.com/glossary/php_socket_programming) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_socket_programming"
            }
        }
    }
}