Network Programming in PHP: From Sockets to WebSockets

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Network Programming in PHP: From Sockets to WebSockets

\n\n

When we talk about PHP, the first thing that comes to mind is web development: form processing, HTML generation, working with databases. But PHP is much more. Have you ever wondered that your PHP script can become not just a "responder" to HTTP requests, but a full-fledged server that communicates with other programs directly, through sockets? This opens the door to the world of chat servers, game lobbies, monitoring systems, and IoT devices.

\n\n

In this article, we will take off the rose-colored glasses and break down network programming in PHP with real examples. You will learn what sockets are, how to make PHP listen on a port, how to send and receive data over TCP and UDP, and even touch the magic of WebSockets. Let's go!

\n\n

What is a socket and why does PHP need it?

\n\n

A socket is a software interface for exchanging data between processes that can be running on the same machine or on different computers on a network. Imagine a telephone handset: you have a number (IP address) and a port (extension number). You pick up the handset (create a socket), dial the number (connect), and speak (transmit data).

\n\n

In PHP, sockets are implemented through the sockets extension. It is usually disabled by default, but it is easy to enable. Network programming in PHP allows you to create:

\n\n
    \n
  • TCP servers and clients (for example, for a chat or data exchange with another service).
  • \n
  • UDP applications (streaming, monitoring, where packet loss is not critical).
  • \n
  • WebSocket servers (for real-time applications in the browser).
  • \n
\n\n

The main advantage is that you stay in the familiar PHP environment without switching to C, Java, or Node.js. Of course, for high-load systems, PHP is not ideal (due to single-threading), but for prototypes, internal tools, or low loads, it is just right.

\n\n

Creating a TCP server in PHP

\n\n

Let's start with the classic: a TCP server that accepts connections and sends a greeting. This is the basis of any php socket application.

\n\n

Step 1. Creating a socket

\n

The socket_create() function is the first step. It takes three parameters: the protocol family (AF_INET for IPv4), the socket type (SOCK_STREAM for TCP), and the protocol (0 — let the system choose).

\n\n
<?php\n// Create a TCP socket\n$socket = socket_create(AF_INET, SOCK_STREAM, 0);\nif ($socket === false) {\n    echo "Socket creation error: " . socket_strerror(socket_last_error()) . "\\n";\n    exit;\n}\necho "Socket created successfully.\\n";\n?>
\n\n

Step 2. Binding to an address and port

\n

Use socket_bind(). Specify the IP address (0.0.0.0 — listen on all interfaces) and port (for example, 8080).

\n\n
<?php\n$address = '0.0.0.0';\n$port = 8080;\n\nif (socket_bind($socket, $address, $port) === false) {\n    echo "Bind error: " . socket_strerror(socket_last_error($socket)) . "\\n";\n    exit;\n}\necho "Socket bound to $address:$port\\n";\n?>
\n\n

Step 3. Listening for incoming connections

\n

Put the socket into listening mode using socket_listen(). The second parameter is the maximum connection queue length.

\n\n
<?php\nif (socket_listen($socket, 5) === false) {\n    echo "Listen error: " . socket_strerror(socket_last_error($socket)) . "\\n";\n    exit;\n}\necho "Server started, waiting for connections...\\n";\n?>
\n\n

Step 4. Accepting a client

\n

The blocking function socket_accept(). It waits until someone connects and returns a new socket for communicating with that client.

\n\n
<?php\nwhile (true) {\n    $client = socket_accept($socket);\n    if ($client === false) {\n        echo "Accept connection error: " . socket_strerror(socket_last_error($socket)) . "\\n";\n        break;\n    }\n\n    echo "New client connected.\\n";\n\n    // From                

Blogs

Book Recommendations