【问题标题】:SSL_write() and SSL_read() non-blocking problemSSL_write() 和 SSL_read() 非阻塞问题
【发布时间】:2021-02-18 10:57:43
【问题描述】:
int bytes;

bytes = SSL_write(ssl, buf, num);
bytes = SSL_read(ssl, buf, num);

有没有可能字节数大于0,却出现了SSL_ERROR_WANT_READ或SSL_ERROR_WANT_WRITE?

【问题讨论】:

    标签: c linux ssl openssl nonblocking


    【解决方案1】:

    从技术上讲,不会,在非阻塞场景中,服务器或客户端可能随时发起握手。唯一一次 bytes 应该大于 0 是传输成功,返回值应该是实际写入的字节数。如果 bytes 为 ==0,则可以使用 SSL_get_error_() 捕获错误。类似下面的内容可能会帮助您捕获错误并进行处理。

    int sending=1;
    while (sending)
    {
        bytes=SSL_write(ssl,buf,num);
        if(bytes>0)
        {
            sending=0; //we have sent all the bytes
            break;
        }
    //if we get here there was an error
    if(SSL_get_error(ssl,bytes) == SSL_ERROR_WANT_READ || SSL_ERROR_WANT_WRITE)
    {
         printf("there was an error during I/O operation\n");
         //we are still in sending=1 so the IO operation will be attempted again. 
    }
    

    }

    可能有更好的方法来做到这一点,但这是一些非常基本的错误检查,以查看您的返回值是什么。另请注意,SSL_get_error() 必须从发生 I/O 操作的同一线程中调用。

    【讨论】:

      猜你喜欢
      • 2021-01-10
      • 1970-01-01
      • 2018-03-13
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 2015-09-19
      • 2015-08-17
      相关资源
      最近更新 更多