【问题标题】:Unable to establish connection using OpenSSL BIO interface无法使用 OpenSSL BIO 接口建立连接
【发布时间】:2013-04-12 13:12:40
【问题描述】:

我在 VS2010 中调试。 BIO_do_connect() 在以下代码中失败。我做错了什么?

(pBio在使用前已正确设置)

static const uint32_t kuSleepIntervalInMs = 50;

...
uint32_t uTimeTaken = 0;
...

BIO_set_nbio(pBio, 1);

for (;;)
{
    if (uTimeTaken > 10000)
        return ERR_CONNECTION_TIMED_OUT;

    if (BIO_do_connect(pBio) > 0)
        break;

    if (BIO_should_retry(pBio))
    {
        Sleep(kuSleepIntervalInMs);

        uTimeTaken += kuSleepIntervalInMs;

        continue;
    }

    BIO_free_all(pBio);

    return ERR_FAILED_TO_ESTABLISH_CONNECTION;
}

看来,如果我增加睡眠间隔(例如增加到 500),BIO_do_connect 工作正常,但我想知道为什么它会因更短的间隔值而失败。

【问题讨论】:

    标签: c networking openssl connection


    【解决方案1】:

    自从发布我最初的问题后,我已改用 select(),因此问题不再有效。

    而不是做

    uTimeTaken += kuSleepIntervalInMs;
    

    我现在在做:

    int nRet;
    int fdSocket;
    fd_set connectionfds;
    struct timeval timeout;
    
    BIO_set_nbio(pBio, 1);
    
    nRet = BIO_do_connect(pBio);
    
    if ((nRet <= 0) && !BIO_should_retry(pBio))
        // failed to establish connection.
    
    if (BIO_get_fd(pBio, &fdSocket) <= 0)
        // failed to get fd.
    
    if (nRet <= 0)
    {
        FD_ZERO(&connectionfds);
        FD_SET(fdSocket, &connectionfds);
    
        timeout.tv_usec = 0;
        timeout.tv_sec = 10;
    
        nRet = select(fdSocket + 1, NULL, &connectionfds, NULL, &timeout);
        if (nRet == 0)
            // timeout has occurred.
    }
    

    见我的另一个post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-29
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多