Created
December 22, 2012 03:40
-
-
Save anonymous/4357385 to your computer and use it in GitHub Desktop.
Non-blocking client connection -- works without error to connect to non-blocking SSL socket server written in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$uri = 'tcp://127.0.0.1:9382'; | |
$context = stream_context_create(['ssl' => [ | |
'verify_peer' => FALSE, | |
'allow_self_signed' => TRUE | |
]]); | |
$socket = stream_socket_client( | |
$uri, | |
$errNo, | |
$errStr, | |
42, // <--- not used with ASYNC connections, so the value doesn't matter | |
STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT, | |
$context | |
); | |
if (FALSE === $socket && $errNo !== 10035) { | |
$errorMsg = "Socket connection failure: tcp://$host:$port"; | |
$errorMsg .= $errNo ? "; [Error# $errNo] $errStr" : ''; | |
throw new Exception($errorMsg); | |
} | |
while (TRUE) { | |
$read = $ex = null; | |
$write = array($socket); | |
if (stream_select($read, $write, $ex, 0, 0)) { | |
echo "sock ready to write\r\n"; | |
break; | |
} | |
} | |
while (TRUE) { | |
$crypto = stream_socket_enable_crypto($socket, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT); | |
if ($crypto == TRUE) { | |
echo "crypto enabled on " . stream_socket_get_name($socket, TRUE) . "!\r\n"; | |
break; | |
} elseif ($crypto === FALSE) { | |
echo "an error occurred while attempting to enable crypto"; | |
break; | |
} | |
} | |
fclose($socket); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment