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.
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);
The $socket parameter is the listening socket created earlier using socket_create(), socket_bind(), and socket_listen().
The return value is a new socket resource used for interacting with the client.
Create a TCP socket.
Bind the IP and port.
Listen for client connections.
Use socket_accept() to wait for and accept client connections.
Complete the WebSocket handshake with the client.
Proceed with data communication.
<?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);
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: