获取套接字错误字符串时异步连接失败 [英] get socket error string when async connect fails

查看:206
本文介绍了获取套接字错误字符串时异步连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人知道,如果可能获取一些错误信息(类似于C的getsockopt SO_ERROR)如果一个异步连接像下面的失败
BTW:我不是使用套接字扩展事业流提供了一个SSL包装

anybody knows if its possible to retrieve some error info (like getsockopt SO_ERROR in C) if an async connect like the following fails btw: im not using the socket extension cause streams provide an ssl wrapper

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

THX

推荐答案

我在描述了同样的问题。我发现有点变通办法的:

I'm having the same issue as described. I have found a bit of a workaround:

我在类包装我的每一个流,并设置超时属性。之前我调用connect,我产生时发生了超时的时间戳。然后我在这就要求 stream_select()

I wrap each of my 'streams' in a Stream class and set a timeout property. Before I call connect, I generate a timestamp of when the timeout should occur. I then have a loop in a Manager class which calls stream_select().

成功的连接都放在 $写阵列。我有一个调用 $&于流的GT循环底部另一个呼叫; checkTimeout()。内有一个 stream_select() 0组要求 tv_sec tv_usec 。如果$这个 - >连接不中 $出现写,我认为它已经超时。

Successful connections are placed in the $write array. I have another call at the bottom of the loop that calls $stream->checkTimeout(). Within that there is a stream_select() call with 0 set for tv_sec and tv_usec. If $this->conn doesn't appear within $write, I assume it has timed out.

这篇关于获取套接字错误字符串时异步连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆