CAMason
CAMason

Reputation: 1122

Non-blocking socket connection over SSL?

Is it possible to use socket_create/socket_connect over SSL in non-blocking mode?

I'm currently using \fsockopen() which natively supports an 'ssl://' prefix. However, fsockopen() will block until the connection has been made. This is undesirable in a script that can have around 50 simultaneous sockets waiting to be read.

I've created a test script to use \socket_create(), which can be switched to non-blocking mode before \socket_connect() is called. However, it doesn't appear to have any support for SSL connections (unlike the fsockopen() wrapper).

Upvotes: 5

Views: 3433

Answers (1)

Artefacto
Artefacto

Reputation: 97825

The answer would be to use instead stream_socket_client with the flag STREAM_CLIENT_ASYNC_CONNECT; however there seems to be some bug with SSL:

<?php
$socket = stream_socket_client(
        'ssl://197.136.197.92:443', $errno, $errstr,
        3, // timeout should be ignored when ASYNC
        STREAM_CLIENT_ASYNC_CONNECT
);
if (!$socket) {
    echo "errno = ".$errno."\nerrstr = ".$errstr."\n";
    exit;
}

On Linux there seems to be an infinite loop with:

write(3, "\26\3\1\0o\1\0\0k\3\1L\325w/\337u\343uV\341\365}H\331\21k\313\341Q\f\356\""..., 116) = -1 EAGAIN (Resource temporarily unavailable)

On some BSD variant on http://codepad.viper-7.com/ :

<br />
<b>Warning</b>:  stream_socket_client() [<a href='function.stream-socket-client'>function.stream-socket-client</a>]: SSL: connection timeout in <b>/tmp/cpQ8Gv7B</b> on line <b>9</b><br />
<br />
<b>Warning</b>:  stream_socket_client() [<a href='function.stream-socket-client'>function.stream-socket-client</a>]: Failed to enable crypto in <b>/tmp/cpQ8Gv7B</b> on line <b>9</b><br />
<br />
<b>Warning</b>:  stream_socket_client() [<a href='function.stream-socket-client'>function.stream-socket-client</a>]: unable to connect to ssl://197.136.197.92:443 (Unknown error) in <b>/tmp/cpQ8Gv7B</b> on line <b>9</b><br />
errno = 115
errstr = 

See also bug #49295.

Upvotes: 3

Related Questions