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

socket_connect

Description

The socket_connect of Sockets for PHP initiates a connection on a socket.

Syntax

socket_connect(
    Socket $socket,
    string $address,
    ?int $port = null
): bool

Parameters

socket

A Socket instance created with socket_create().

address

The address parameter is either an IPv4 address in dotted-quad notation (e.g. 127.0.0.1) if socket is AF_INET, a valid IPv6 address (e.g. ::1) if IPv6 support is enabled and socket is AF_INET6 or the pathname of a Unix domain socket, if the socket family is AF_UNIX.

port

The port parameter is only used and is mandatory when connecting to an AF_INET or an AF_INET6 socket, and designates the port on the remote host to which a connection should be made.

Return

Returns true on success or false on failure.

The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual explanation of the error.

NOTE: If the socket is non-blocking then this function returns false with an error Operation now in progress.

Examples

1 · socket address

<?

$domain = AF_UNIX;
$type = SOCK_STREAM;
$protocol = 0;

$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 = '/tmp/mysocket';

    if(file_exists($address))
    {
        unlink($address);
    }

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

    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");
    }

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

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

            $socket_strerror = socket_strerror($error_code);

            die("socket_create: $socket_strerror");
        }

        $return = socket_connect($socket, $address);

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

            $socket_strerror = socket_strerror($error_code);

            die("socket_connect: $socket_strerror");
        }

        var_export($return);

    socket_close($socket);

socket_close($serversocket);

?>
true

2 · port

<?

$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");
    }

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

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

            $socket_strerror = socket_strerror($error_code);

            die("socket_create: $socket_strerror");
        }

        $return = socket_connect($socket, $address, $port);

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

            $socket_strerror = socket_strerror($error_code);

            die("socket_connect: $socket_strerror");
        }

        var_export($return);

    socket_close($socket);

socket_close($serversocket);

?>
true
HomeMenu