(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

socket_shutdownFecha um socket para receber, enviar dados ou ambos.


socket_shutdown(resource $socket, int $how = ?): bool

Esta função é EXPERIMENTAL. O comportamento desta função, seu nome e documentação poderão mudar sem aviso prévio em futuras versões do PHP. Use por sua conta e risco.

A função socket_shutdown() permite que você pare a entrada, saída ou todos os dados (este é o padrão) de serem enviados através do socket

O valor de how pode ser um dos seguintes:

possible values for how
0 Fecha o socjet para leitura
1 Fecha o socket para escrita
2 Fecha o socket para leitura e escrita

add a note

User Contributed Notes 5 notes

ludvig dot ericson at gmail dot com
18 years ago
Sockets should be first shutdown and then closed.
// Sample: Closing sockets gracefully
socket_shutdown($sock, 2);
9 months ago
Shutting down is a formality two peers can do before closing their connections. It's not required, but it can help assert your I/O procedures, so it's useful.

If writing is shut down, trying to send will result in a pipe error, and the remote peer will read an empty string after receiving all other pending data.

If reading is shut down, trying to receive will return an empty string, and the remote peer will get a pipe error if they try to send.

Writing should be shut down first between two peers. Remaining data should then be read and handled. If anything is sent at this point it should be a "goodbye" (nothing that requires the remote peer to write back to you). Finally, reading can be shut down.

Selection on a shut down channel will always succeed. Similarly, the remote peer will always succeed at selecting the opposite channel.
richard dot thomas at psysolutions dot com
17 years ago
That is not a good example of a graceful shutdown. One should close the sending side of the socket and continue to read until the remote end closes its sending connection.
renmengyang567 at gmail dot com
4 years ago
In this case, the TCP client is gracefully disconnected from the server



// create for tcp
$sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_bind($sock, '',5200);
$fp = fopen('./socket_shutdown.php','rb');
$clnt_sock = socket_accept($sock);

feof($fp)) {
$str = fread($fp,BUF_SIZE);

$eof = "\n";

//disconnect output stream(断开输入流)
$ret = socket_read($clnt_sock, 100);
printf("Message from client:%s\n",$ret);

// for tcp-client
$clnt_sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_connect($clnt_sock, '', 5200);
while ((
$cnt= @socket_read($clnt_sock, 10,PHP_NORMAL_READ)) !==false) {
"receive file data".PHP_EOL;
socket_write($clnt_sock, "Tank you");
recycling dot sp dot am at gmail dot com
13 years ago
Shutdown and SOL_TCP:
= socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_shutdown($a, 2)
PHP Warning: socket_shutdown(): unable to shutdown socket [107]: Transport endpoint is not connected

Shutdown and SOL_UDP:
= socket_create(AF_INET, SOCK_STREAM, SOL_UDP);
socket_shutdown($a, 2)
PHP Warning: socket_shutdown(): unable to shutdown socket [107]: Transport endpoint is not connected

Conclusion: if you are not actually connected, shutdown will fails with socket_error = 107, Transport endpoint is not connected. This is true for both TPC and UDP connection (which is suprising, UDP being a connectionless protocol). This is true no matter the value set for the how parameter.
To Top