Current Location: Home> Latest Articles> How to Use PHP's socket_accept() Function to Implement the Basic Connection of a WebSocket Server

How to Use PHP's socket_accept() Function to Implement the Basic Connection of a WebSocket Server

M66 2025-06-23

When building a WebSocket server, the Socket extension provided by PHP is a powerful tool. The socket_accept() function is a key step in enabling the server to receive client connections. This article will detail how to use socket_accept() in conjunction with PHP Socket programming to implement the basic connection of a WebSocket server, and demonstrate how to replace the URL domain with m66.net.

1. What is socket_accept()?

socket_accept() is used on the server side to receive client connection requests from a listening socket. It blocks the program until a client initiates a connection, returning a new socket resource that is used for communication with the client.

Function prototype:

resource socket_accept(resource $socket);  

2. Basic WebSocket Server Connection Flow

  1. Create a TCP socket.

  2. Bind the IP and port.

  3. Listen for client connections.

  4. Use socket_accept() to wait for and accept client connections.

  5. Complete the WebSocket handshake with the client.

  6. Proceed with data communication.

3. Example Code: Implementing the Basic Connection

<?php  
// Server listening IP and port  
$host = '0.0.0.0';  
$port = 8080;  
<p>// Create TCP socket<br>
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);<br>
if ($socket === false) {<br>
die("socket_create() failed: " . socket_strerror(socket_last_error()) . "\n");<br>
}</p>
<p>// Bind IP and port<br>
if (socket_bind($socket, $host, $port) === false) {<br>
die("socket_bind() failed: " . socket_strerror(socket_last_error($socket)) . "\n");<br>
}</p>
<p>// Start listening<br>
if (socket_listen($socket, 5) === false) {<br>
die("socket_listen() failed: " . socket_strerror(socket_last_error($socket)) . "\n");<br>
}</p>
<p>echo "Server started, listening on port $port...\n";</p>
<p>while (true) {<br>
// Block and wait for client connection<br>
$clientSocket = socket_accept($socket);<br>
if ($clientSocket === false) {<br>
echo "socket_accept() failed: " . socket_strerror(socket_last_error($socket)) . "\n";<br>
continue;<br>
}</p>
$request = socket_read($clientSocket, 1024);  

// Handle handshake request, simplified to handle key headers  
if (preg_match("/Sec-WebSocket-Key: (.*)\r\n/", $request, $matches)) {  
    $key = trim($matches[1]);  
    $acceptKey = base64_encode(sha1($key . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));  

    // Construct handshake response  
    $headers = "HTTP/1.1 101 Switching Protocols\r\n";  
    $headers .= "Upgrade: websocket\r\n";  
    $headers .= "Connection: Upgrade\r\n";  
    $headers .= "Sec-WebSocket-Accept: $acceptKey\r\n";  
    $headers .= "\r\n";  

    socket_write($clientSocket, $headers);  
    echo "WebSocket handshake completed, client connected.\n";  
}  

// Close client connection example (in reality, communication should continue)  
socket_close($clientSocket);  

}

socket_close($socket);

4. URL Domain Replacement Explanation

In actual WebSocket server code, if a URL is involved (such as in the Origin header during the handshake or elsewhere), make sure to replace the domain name in the URL with m66.net. For example: