Current Location: Home> Latest Articles> How to Send Data Using stream_socket_sendto and Other Stream Handling Functions After socket_export_stream Conversion

How to Send Data Using stream_socket_sendto and Other Stream Handling Functions After socket_export_stream Conversion

M66 2025-06-29

In PHP, socket_export_stream() is a very useful function that converts a low-level socket resource into a higher-level stream, enabling the use of more advanced stream functions like fwrite(), stream_socket_sendto(), stream_select(), and others for data sending and receiving. This conversion is particularly useful in environments where the stream_* series of functions are needed, such as event-driven models or when managing stream contexts.

However, many developers, after using socket_export_stream(), are unsure how to properly send data using functions like stream_socket_sendto(). This article will thoroughly explain the process.

1. Overview of socket_export_stream()

$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, '0.0.0.0', 12345);
<p>// Convert the socket resource to a stream resource<br>
$stream = socket_export_stream($socket);<br>

This $stream is now a PHP stream resource, which can be manipulated just like a file. At this point, you can use stream functions in place of socket functions for data handling.

2. Sending Data Using stream_socket_sendto()

stream_socket_sendto() is typically used to send data over connectionless protocols, like UDP. When using it with the converted stream resource, there are several important points to note:

  • The stream must be unbuffered, or you must manually flush it using fflush;

  • The address format should be in standard URI format;

  • For UDP, you must provide the destination address.

$remote = 'udp://m66.net:12345';
$message = "Hello via stream!";
<p>// Note: The stream must be writable, and the target address must be correct<br>
$bytesSent = stream_socket_sendto($stream, $message, 0, $remote);</p>
<p>if ($bytesSent === false) {<br>
echo "Failed to send.\n";<br>
} else {<br>
echo "Successfully sent $bytesSent bytes.\n";<br>
}<br>

3. Managing Multiple Streams Using stream_select()

With stream_select(), you can monitor multiple streams (such as multiple resources converted with socket_export_stream) to check if they are readable or writable, which is perfect for non-blocking I/O programming.

$read = [$stream];
$write = null;
$except = null;
<p>$changed = stream_select($read, $write, $except, 5);</p>
<p>if ($changed > 0) {<br>
$data = fread($stream, 1024);<br>
echo "Received data: $data\n";<br>
}<br>

4. Simplified Data Handling with fwrite / fread

If you don’t need to explicitly specify the target address, you can directly use fwrite():

fwrite($stream, "Simple write to stream\n");

Note that this method is suitable for connection-oriented protocols or when the remote UDP socket has already been established after binding.

5. Conclusion

By converting the raw socket into a PHP stream using socket_export_stream(), you can easily use advanced functions like stream_socket_sendto(), fwrite(), stream_select(), and others for data transmission and reception. This approach enhances the maintainability and flexibility of your code. During usage, be mindful of the target address format, the resource’s read/write status, and the protocol’s characteristics (such as whether UDP is connected). When configured correctly, this method is ideal for building modern, high-performance network communication components.