Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

c - Unable to establish connection using OpenSSL BIO interface

I'm debugging in VS2010. BIO_do_connect() fails in the following code. What am I doing wrong?

(pBio is properly set up before use)

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

It appears that if I increase the sleep interval (for example to 500), BIO_do_connect works fine but I'd like to know why it fails with shorter interval values.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since posting my original question, I've switched to use select() so the problem is no longer valid.

Instead of doing

uTimeTaken += kuSleepIntervalInMs;

I'm now doing:

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.
}

See my other post.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...