简体   繁体   English

SSL_write() 和 SSL_read() 非阻塞问题

[英]SSL_write() and SSL_read() non-blocking problem

int bytes;

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

Is it possible that bytes are greater than 0, but SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE appears?有没有可能bytes大于0,却出现SSL_ERROR_WANT_READ或者SSL_ERROR_WANT_WRITE?

Technically no, in a non-blocking scenario there could be a handshake initiation at anytime from the server or the client.从技术上讲不,在非阻塞场景中,服务器或客户端可以随时发起握手。 The only time bytes should be greater than 0 is with a successful transfer, the return value should be the actual number of bytes written.唯一一次字节应该大于 0 是成功传输,返回值应该是实际写入的字节数。 if bytes is ==0 then there was an error that can be captured using SSL_get_error_().如果 bytes ==0 则存在可以使用 SSL_get_error_() 捕获的错误。 something like below might help you capture the error and handle it.类似下面的内容可能会帮助您捕获错误并进行处理。

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

} }

There is probably a better way to do this, but this is some very basic error checking to see what your return values hold.可能有更好的方法来执行此操作,但这是一些非常基本的错误检查,以查看您的返回值。 Another note, the SSL_get_error() has to be called from the same thread that the I/O operation takes place from.另外请注意,SSL_get_error() 必须从发生 I/O 操作的同一个线程调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OpenSSL非阻塞套接字SSL_read()不可预测 - OpenSSL Non-Blocking Socket SSL_read() unpredictable 基于epoll的非阻塞ssl_read()陷入循环 - epoll based non-blocking ssl_read() stuck in a loop 是否可以使用SSL_write和SSL_read发送结构? - Is it possible to send a struct with SSL_write and SSL_read? 当SSL_read()返回WANT_READ时调用SSL_write() - Calling SSL_write() when SSL_read() returned WANT_READ 如何在非阻塞情况下使用 SSL_read() 和 SSL_accept() - How to use SSL_read() and SSL_accept() on non blocking SSL_Read / SSL_Write中的重新协商应该调用什么 - What should be called for re-negotiation in SSL_Read/SSL_Write OpenSSL:在没有SSL_read()/ SSL_write()的情况下执行en / / decryption - OpenSSL: perform en-/decryption without SSL_read() / SSL_write() OpenSSL-SSL_write问题 - OpenSSL - SSL_write problem 如果我想使用非阻塞套接字调用SSL_shutdown(),我是否需要先保持调用SSL_read()直到产生SSL_ERROR_WANT_READ? - If I want to call SSL_shutdown() with non-blocking sockets, do I need to keep calling SSL_read() first until it yields SSL_ERROR_WANT_READ? 在 openssl TLS1.3 中, SSL_write 会产生 SSL_ERROR_WANT_READ 吗? SSL_read 会产生 SSL_ERROR_WANT_WRITE 吗? - In openssl TLS1.3, will SSL_write yield SSL_ERROR_WANT_READ ? And will SSL_read yield SSL_ERROR_WANT_WRITE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM