如果像下面这样的异步连接失败顺便说一句,任何人都知道是否有可能检索一些错误信息(如 C 中的 getsockopt SO_ERROR):我没有使用套接字扩展,因为流提供了 ssl 包装器
<?php
$ctx = stream_context_create();
stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));
$destination = "tcp://92.247.12.242:8081";
$socket = stream_socket_client($destination, $errno, $errstr,
10, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT, $ctx);a
// plain socket
var_dump($socket);
// no error as expected
var_dump($errno);
var_dump($errstr);
stream_set_blocking($socket, false);
/* Prepare the read array */
$read = array($socket);
$write = array($socket);
$except = NULL;
if (false === ($num_changed_streams = stream_select($read, $write, $except, 10))) {
var_dump("select failed?");
/* Error handling */
} elseif ($num_changed_streams > 0) {
/* At least on one of the streams something interesting happened */
var_dump("event");
// read fails, so the connect fails but why?
$result = stream_socket_recvfrom($socket, 1024);
var_dump($result);
// no error again
var_dump($errno);
var_dump($errstr);
// nothing interesting
var_dump(stream_get_meta_data($socket));
}
// wont get called
function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
$args = func_get_args();
var_dump($args);
}
谢谢