PHP Sockets
Sockets are a low-level way to communicate between different processes, either on the same machine or across a network. The Sockets extension in PHP provides a low-level interface for network communication, giving you more control than higher-level abstractions like stream_socket_client() or cURL.
The Sockets extension implements a BSD-style socket interface.
Creating a Socket
To create a socket, you use the socket_create() function.
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: " . socket_strerror(socket_last_error()) . "\n";
}
?>
AF_INET: Specifies the address family (IPv4).SOCK_STREAM: Specifies the socket type (a full-duplex, connection-based byte stream). This is used by TCP.SOL_TCP: Specifies the protocol.
Socket Server Example (TCP)
A simple TCP server might look like this:
<?php
error_reporting(E_ALL);
// Create a TCP/IP socket.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("Could not create socket\n");
}
// Bind the socket to an address and port.
$address = '127.0.0.1';
$port = 10000;
if (socket_bind($socket, $address, $port) === false) {
die("Could not bind to address\n");
}
// Start listening for connections.
if (socket_listen($socket, 5) === false) {
die("Could not listen on socket\n");
}
echo "Listening on $address:$port...\n";
do {
// Accept an incoming connection.
if (($client_socket = socket_accept($socket)) === false) {
echo "socket_accept() failed: " . socket_strerror(socket_last_error($socket)) . "\n";
break;
}
// Read the client's message.
$msg = socket_read($client_socket, 1024);
echo "Received: $msg";
// Write a response back to the client.
$response = "Hello, client!\n";
socket_write($client_socket, $response, strlen($response));
// Close the client socket.
socket_close($client_socket);
} while (true);
socket_close($socket);
?>
Socket Client Example (TCP)
A client to connect to the server above:
<?php
$address = '127.0.0.1';
$port = 10000;
// Create a socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("Could not create socket\n");
}
// Connect to the server
$result = socket_connect($socket, $address, $port);
if ($result === false) {
die("Could not connect to server\n");
}
// Send a message to the server
$message = "Hello, server!\n";
socket_write($socket, $message, strlen($message));
// Read the server's response
$response = socket_read($socket, 1024);
echo "Response from server: $response";
// Close the socket
socket_close($socket);
?>
Use Cases
- Custom Protocols: Sockets are used for implementing custom network protocols.
- Real-time Applications: Chat servers, game servers, and other real-time applications often use sockets.
- Inter-Process Communication: Sockets can be used for communication between different processes on the same machine.
While powerful, the Sockets extension is more complex to use than higher-level networking functions. For common tasks like making HTTP requests, cURL or the Guzzle library are usually a better choice. Sockets are for when you need to go beyond standard protocols.