Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

socket_shutdown

Description

The socket_shutdown of Sockets for PHP shuts down a socket for receiving, sending, or both.

Syntax

socket_shutdown(
    Socket $socket,
    int $mode = 2
): bool

Parameters

socket

A Socket instance created with socket_create().

mode

The value of mode can be one of the following:

ValueDescription
0Shutdown socket reading
1Shutdown socket writing
2Shutdown socket reading and writing

Return

Returns true on success or false on failure.

Examples

1 · socket

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$serversocket = socket_create($domain, $type, $protocol);

    if($serversocket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $address = '127.0.0.1';
    $port = 5000;

    $socket_bind = socket_bind($serversocket, $address, $port);

    if($socket_bind === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_bind: $socket_strerror");
    }

    $socket_listen = socket_listen($serversocket);

    if($socket_listen === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_listen: $socket_strerror");
    }

    $clientsocket = socket_create($domain, $type, $protocol);

        if($clientsocket === false)
        {
            $error_code = socket_last_error();

            $socket_strerror = socket_strerror($error_code);

            die("socket_create: $socket_strerror");
        }

        $socket_connect = socket_connect($clientsocket, $address, $port);

        if($socket_connect === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_connect: $socket_strerror");
        }

        $socket = socket_accept($serversocket);

            if($socket === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_accept: $socket_strerror");
            }

            $data = 'data';

            $socket_write = socket_write($socket, $data);

            if($socket_write === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_write: $socket_strerror");
            }

            $return = socket_shutdown($socket);

            var_export($return);
            //echo PHP_EOL;

        socket_close($socket);

        $length = 2048;

        $socket_read = socket_read($clientsocket, $length);

        if($socket_read === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_read: $socket_strerror");
        }

        //$return = socket_shutdown($clientsocket);

        //var_export($return);
        //echo PHP_EOL;

    socket_close($clientsocket);

    //$return = socket_shutdown($serversocket);

    //var_export($return);

socket_close($serversocket);

?>
true

2 · mode · 0

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$serversocket = socket_create($domain, $type, $protocol);

    if($serversocket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $address = '127.0.0.1';
    $port = 5001;

    $socket_bind = socket_bind($serversocket, $address, $port);

    if($socket_bind === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_bind: $socket_strerror");
    }

    $socket_listen = socket_listen($serversocket);

    if($socket_listen === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_listen: $socket_strerror");
    }

    $clientsocket = socket_create($domain, $type, $protocol);

        if($clientsocket === false)
        {
            $error_code = socket_last_error();

            $socket_strerror = socket_strerror($error_code);

            die("socket_create: $socket_strerror");
        }

        $socket_connect = socket_connect($clientsocket, $address, $port);

        if($socket_connect === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_connect: $socket_strerror");
        }

        $socket = socket_accept($serversocket);

            if($socket === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_accept: $socket_strerror");
            }

            $data = 'data';

            $socket_write = socket_write($socket, $data);

            if($socket_write === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_write: $socket_strerror");
            }

            $mode = 0;

            $return = socket_shutdown($socket, $mode);

            var_export($return);
            //echo PHP_EOL;

        socket_close($socket);

        $length = 2048;

        $socket_read = socket_read($clientsocket, $length);

        if($socket_read === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_read: $socket_strerror");
        }

        //$return = socket_shutdown($clientsocket, $mode);

        //var_export($return);
        //echo PHP_EOL;

    socket_close($clientsocket);

    //$return = socket_shutdown($serversocket, $mode);

    //var_export($return);

socket_close($serversocket);

?>
true

3 · mode · 1

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$serversocket = socket_create($domain, $type, $protocol);

    if($serversocket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $address = '127.0.0.1';
    $port = 5002;

    $socket_bind = socket_bind($serversocket, $address, $port);

    if($socket_bind === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_bind: $socket_strerror");
    }

    $socket_listen = socket_listen($serversocket);

    if($socket_listen === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_listen: $socket_strerror");
    }

    $clientsocket = socket_create($domain, $type, $protocol);

        if($clientsocket === false)
        {
            $error_code = socket_last_error();

            $socket_strerror = socket_strerror($error_code);

            die("socket_create: $socket_strerror");
        }

        $socket_connect = socket_connect($clientsocket, $address, $port);

        if($socket_connect === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_connect: $socket_strerror");
        }

        $socket = socket_accept($serversocket);

            if($socket === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_accept: $socket_strerror");
            }

            $data = 'data';

            $socket_write = socket_write($socket, $data);

            if($socket_write === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_write: $socket_strerror");
            }

            $mode = 1;

            $return = socket_shutdown($socket, $mode);

            var_export($return);
            //echo PHP_EOL;

        socket_close($socket);

        $length = 2048;

        $socket_read = socket_read($clientsocket, $length);

        if($socket_read === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_read: $socket_strerror");
        }

        //$return = socket_shutdown($clientsocket, $mode);

        //var_export($return);
        //echo PHP_EOL;

    socket_close($clientsocket);

    //$return = socket_shutdown($serversocket, $mode);

    //var_export($return);

socket_close($serversocket);

?>
true

4 · mode · 2

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$serversocket = socket_create($domain, $type, $protocol);

    if($serversocket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $address = '127.0.0.1';
    $port = 5003;

    $socket_bind = socket_bind($serversocket, $address, $port);

    if($socket_bind === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_bind: $socket_strerror");
    }

    $socket_listen = socket_listen($serversocket);

    if($socket_listen === false)
    {
        $error_code = socket_last_error($serversocket);

        $socket_strerror = socket_strerror($error_code);

        die("socket_listen: $socket_strerror");
    }

    $clientsocket = socket_create($domain, $type, $protocol);

        if($clientsocket === false)
        {
            $error_code = socket_last_error();

            $socket_strerror = socket_strerror($error_code);

            die("socket_create: $socket_strerror");
        }

        $socket_connect = socket_connect($clientsocket, $address, $port);

        if($socket_connect === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_connect: $socket_strerror");
        }

        $socket = socket_accept($serversocket);

            if($socket === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_accept: $socket_strerror");
            }

            $data = 'data';

            $socket_write = socket_write($socket, $data);

            if($socket_write === false)
            {
                $error_code = socket_last_error($socket);

                $socket_strerror = socket_strerror($error_code);

                die("socket_write: $socket_strerror");
            }

            $mode = 2;

            $return = socket_shutdown($socket, $mode);

            var_export($return);
            //echo PHP_EOL;

        socket_close($socket);

        $length = 2048;

        $socket_read = socket_read($clientsocket, $length);

        if($socket_read === false)
        {
            $error_code = socket_last_error($clientsocket);

            $socket_strerror = socket_strerror($error_code);

            die("socket_read: $socket_strerror");
        }

        //$return = socket_shutdown($clientsocket, $mode);

        //var_export($return);
        //echo PHP_EOL;

    socket_close($clientsocket);

    //$return = socket_shutdown($serversocket, $mode);

    //var_export($return);

socket_close($serversocket);

?>
true
HomeMenu