簡體   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);

有沒有可能bytes大於0,卻出現SSL_ERROR_WANT_READ或者SSL_ERROR_WANT_WRITE?

從技術上講不,在非阻塞場景中,服務器或客戶端可以隨時發起握手。 唯一一次字節應該大於 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 操作的同一個線程調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM