gpt4 book ai didi

c - 非阻塞 BIO_do_connect 在没有互联网连接时阻塞

转载 作者:太空狗 更新时间:2023-10-29 11:21:35 26 4
gpt4 key购买 nike

我正在使用 Openssl-0.9.8x,如下所示:

bio = BIO_new_ssl_connect(ctx);
BIO_get_ssl(bio, & ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
BIO_set_nbio(bio, 1);
in_addr_t serverIP = inet_addr(HTTPS_SERVER_IP);
BIO_set_conn_ip(bio, &serverIP );
BIO_set_conn_port(bio, HTTPS_SERVER_PORT_STR);
while(1) {
printf("BIO_do_connect start>>>>\n");
if(BIO_do_connect(bio) <= 0 && BIO_should_retry(bio)) {
sleep(1);
printf("BIO_do_connect retry>>>>\n");
}
else {
printf("Connect success.\n");
}
}

当互联网连接正常时(即它可以连接到服务器),它工作正常。但是,当 Internet 连接受限时(即无法连接到服务器),BIO_do_connect() 在重试一次或多次后被阻止。输出如下:

BIO_do_connect start>>>>
BIO_do_connect retry>>>>
BIO_do_connect start>>>>
BIO_do_connect retry>>>>
BIO_do_connect start>>>>

最后在BIO_do_connect(...)中被阻塞了?为什么会这样?

最佳答案

这可能是您使用了SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY)

来自 0.9.8 手册页:

SSL_MODE_AUTO_RETRY

Never bother the application with retries if the transport is blocking. If a renegotiation take place during normal operation, a SSL_read() or SSL_write() would return with -1 and indicate the need to retry with SSL_ERROR_WANT_READ. In a non-blocking environment applications must be prepared to handle incomplete read/write operations. In a blocking environment, applications are not always prepared to deal with read/write operations returning without success report. The flag SSL_MODE_AUTO_RETRY will cause read/write operations to only return after the handshake and successful completion.

SSL_MODE_AUTO_RETRY 的作用是自动重试否则会返回到应用程序代码的操作(即使在使用阻塞连接时)。当您想要非阻塞操作时使用它没有任何意义。

尝试完全删除该行。

顺便说一句,0.9.8 已不再受支持,不再接收安全更新。您真的应该升级到更新的版本。

关于c - 非阻塞 BIO_do_connect 在没有互联网连接时阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38886648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com