In PHP, we can use the socket extension to build network service applications. The socket_accept() function is one of the essential components when constructing a TCP-based server application. This article provides a complete example to demonstrate how to use socket_accept() to implement a simple Echo Server.
The socket_accept() function is used to accept a connection request from a client. It blocks on the server socket until a connection is received. Upon success, it returns a new socket resource that can be used to communicate with the client.
Syntax:
resource socket_accept ( resource $socket )
$socket is the server socket created and bound using socket_create() and socket_bind().
Returns a socket resource used for communication with the client.
Below are the complete steps to create a simple Echo Server:
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($server === false) {
die("socket_create() failed: " . socket_strerror(socket_last_error()) . "\n");
}
$host = '0.0.0.0';
$port = 12345;
if (!socket_bind($server, $host, $port)) {
die("socket_bind() failed: " . socket_strerror(socket_last_error($server)) . "\n");
}
if (!socket_listen($server, 5)) {
die("socket_listen() failed: " . socket_strerror(socket_last_error($server)) . "\n");
}
echo "Server started, listening on port $port ...\n";
while (true) {
$client = socket_accept($server);
if ($client === false) {
echo "socket_accept() failed: " . socket_strerror(socket_last_error($server)) . "\n";
continue;
}
socket_write($client, $msg, strlen($msg));
while (true) {
$input = socket_read($client, 2048, PHP_NORMAL_READ);
if ($input === false || trim($input) === 'exit') {
break;
}
$input = trim($input);
$output = "You entered: $input\n";
socket_write($client, $output, strlen($output));
}
socket_close($client);
echo "Client connection closed.\n";
}
When you want to stop the server, simply close the main socket:
socket_close($server);
You can connect to the server using Telnet or any client tool that supports TCP connections:
telnet m66.net 12345
Once connected, the server will respond with a welcome message and echo back whatever you type.
The PHP socket extension is not enabled by default. You need to enable it in the php.ini configuration file.
This example uses blocking mode, which is suitable for demonstrations and learning purposes. For production environments, it is recommended to use non-blocking mode or a method like stream_select() to support multiple clients.
Make sure your firewall and operating system allow communication on the listening port.
With socket_accept(), PHP is capable of building basic TCP network applications. The Echo Server implemented in this article is a great starting point for understanding socket programming mechanisms. You can build upon it to create more complex services like chat rooms or remote control systems.
The Echo Server is one of the simplest models in socket programming, yet it opens the door to network programming. Once you've mastered these basics, PHP’s potential in network communication becomes limitless.