[英]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.