Current Location: Home> Latest Articles> How to Achieve Bi-directional Communication Between Client and Server Using socket_export_stream?

How to Achieve Bi-directional Communication Between Client and Server Using socket_export_stream?

M66 2025-06-22

In PHP network programming, the socket_* family of functions provides developers with low-level network communication capabilities. While the stream_socket_* family of functions is more advanced and user-friendly, the socket_* functions still hold irreplaceable advantages in certain scenarios that require greater control over the low-level operations. This article will discuss how to use socket_export_stream to convert a low-level socket into a stream, enabling bi-directional communication between client and server.

What is socket_export_stream?

socket_export_stream is a function provided by PHP that exports a low-level socket resource into a stream resource. This allows the use of familiar file stream functions like fread(), fwrite(), and others to operate on socket data. This method combines the flexibility of low-level sockets with the ease of the stream API.

stream socket_export_stream(Socket $socket): resource|false

The returned value is a stream type resource that can work with functions like fopen, stream_select, fgets, etc.


1. Creating the Server Socket

The server creates a TCP socket using socket_create() and binds it to a local address and port.

$serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($serverSocket, '0.0.0.0', 9501);
socket_listen($serverSocket);
echo "Server started, listening on port 9501...\n";

2. Accepting Connections and Exporting to Stream

The server blocks and waits for client connection requests, then exports the connection resource into a readable and writable stream.

while (true) {
    $clientSocket = socket_accept($serverSocket);
    if ($clientSocket === false) {
        echo "Connection acceptance failed\n";
        continue;
    }
if ($clientStream === false) {
    echo "Stream export failed\n";
    socket_close($clientSocket);
    continue;
}

fwrite($clientStream, "Welcome to the m66.net server\n");

while (!feof($clientStream)) {
    $data = fgets($clientStream);
    if ($data === false) break;
    echo "Client says: $data";
    fwrite($clientStream, "You said: $data");
}

fclose($clientStream);
socket_close($clientSocket);

}

3. Client Connecting to Server

The client also uses socket_create to connect to the server, then uses socket_export_stream for data reading and writing.

$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($client, 'm66.net', 9501);
<p>$stream = socket_export_stream($client);<br>
if ($stream === false) {<br>
echo "Stream export failed\n";<br>
exit(1);<br>
}</p>
<p>echo fgets($stream); // Receive welcome message</p>
<p>fwrite($stream, "Hello, server!\n");<br>
echo fgets($stream); // Read echo</p>
<p>fclose($stream);<br>
socket_close($client);<br>


Advantages and Considerations

Advantages

  • Concise Code: The stream interface simplifies the process of reading and writing socket data;

  • High Compatibility: It can be used with functions like stream_select, stream_set_blocking, and others;

  • Natural Bi-directional Communication: Real-time responses can be achieved through continuous reading and writing in a loop.

Considerations

  • socket_export_stream can only be used with connected sockets;

  • After use, both the stream and socket need to be closed to prevent resource leaks;

  • Network communication should consider error handling and timeout control to avoid blocking or crashes.