Current Location: Home> Latest Articles> What Are the Differences and Considerations of Using stream_socket_shutdown Function in UDP and TCP Protocols?

What Are the Differences and Considerations of Using stream_socket_shutdown Function in UDP and TCP Protocols?

M66 2025-06-22

1. stream_socket_shutdown Basic Syntax

First, let's look at the basic syntax of the stream_socket_shutdown function:

bool stream_socket_shutdown ( resource $stream , int $how )
  • $stream: This refers to the socket resource that has been opened via functions like stream_socket_client or stream_socket_server.

  • $how: This defines the shutdown mode. You can use one of the following constants:

    • STREAM_SHUT_RDWR: Shuts down both reading and writing operations.

    • STREAM_SHUT_RD: Shuts down the reading operation.

    • STREAM_SHUT_WR: Shuts down the writing operation.

2. Application in TCP Protocol

The TCP protocol is connection-oriented, meaning a connection must be established before communication, and the connection must be gracefully closed after communication ends. The stream_socket_shutdown function plays a vital role in TCP connections, especially after data transfer is completed, to ensure the connection is properly closed and to prevent resource leaks.

The typical operation flow in TCP is as follows:

  1. Establish a connection using stream_socket_client or stream_socket_server.

  2. After data transfer is complete, use stream_socket_shutdown($stream, STREAM_SHUT_WR) to shut down the writing operation (inform the remote host that no more data will be sent).

  3. Then, wait for the remote host to finish sending data and close the connection, typically using stream_socket_shutdown($stream, STREAM_SHUT_RD) to shut down the reading operation.

A typical example of closing a TCP connection:

$stream = stream_socket_client("tcp://m66.net:80", $errno, $errstr);
<p>if (!$stream) {<br>
echo "Error: $errno - $errstr\n";<br>
} else {<br>
fwrite($stream, "GET / HTTP/1.1\r\nHost: m66.net\r\nConnection: close\r\n\r\n");</p>
stream_socket_shutdown($stream, STREAM_SHUT_WR);

// Read the response
while ($response = fgets($stream)) {
    echo $response;
}

// Close the read operation and the connection
stream_socket_shutdown($stream, STREAM_SHUT_RD);

fclose($stream);

}

Considerations:

  • In TCP, stream_socket_shutdown allows you to shut down reading and writing operations separately, which is very useful for gracefully closing connections.

  • When using STREAM_SHUT_WR, the remote host usually receives a "connection closed" signal, prompting it to also close the connection and release resources.

3. Application in UDP Protocol

Unlike TCP, the UDP protocol is connectionless. Therefore, there is no need to establish a persistent connection or gracefully close the connection like in TCP. The stream_socket_shutdown function has limited use in UDP, as UDP does not involve stream control or connection shutdown.

The typical usage in UDP protocol:

  1. Establish a UDP socket using stream_socket_client or stream_socket_server.

  2. After sending a packet, there is no need to explicitly call stream_socket_shutdown to shut down reading or writing operations since UDP does not have the concept of a "connection."

A typical example of sending data with UDP:

$stream = stream_socket_client("udp://m66.net:12345", $errno, $errstr);
<p>if (!$stream) {<br>
echo "Error: $errno - $errstr\n";<br>
} else {<br>
fwrite($stream, "Hello, UDP server!");<br>
fclose($stream);<br>
}<br>

Considerations:

  • In UDP, the stream_socket_shutdown function is usually not needed. Even if it is called, it does not affect the connection as UDP has no explicit connection concept.

  • Therefore, using the stream_socket_shutdown function in UDP scenarios does not significantly impact the data transmission behavior.

4. Summary

The stream_socket_shutdown function shows clear differences in its application between TCP and UDP. For TCP, it helps to gracefully close the connection, ensuring complete data transfer and releasing resources. In UDP, due to its connectionless nature, stream_socket_shutdown plays a less significant role and is typically unnecessary.

When using stream_socket_shutdown, developers should choose the appropriate shutdown method based on the protocol type being used. In TCP, correctly using STREAM_SHUT_WR and STREAM_SHUT_RD ensures proper connection closure and timely resource release, while in UDP, this function is usually not needed.