SSL_read如何知道读完数据
如题,SSL_read每次只返回一个记录,如何知道它已经读完数据了?我是这样做的,不知道是否正确?
- C/C++ code
int CSsl::RecvData(SSL* ssl,SOCKET sock,char* buf){ int nRead=0; timeval tval; tval.tv_sec = 5; tval.tv_usec = 0; int len=4096; while(1) { fd_set fds; FD_ZERO(&fds); FD_SET(sock, &fds); int iSelect = select(FD_SETSIZE, &fds, NULL, NULL, &tval); if(iSelect !=1) { break; } int err = SSL_read (ssl, buf+nRead, len-nRead); if(err<=0) { break; } nRead+=err; } return nRead;}[解决办法]
通常一个一个字节读:
- C/C++ code
while((err = SSL_read(ssl,p,1)) > 0) { p++; if((--MaxLen) <= 0) break; }
[解决办法]
如果你每次读取的长度len是固定的,那你的方法也没错
[解决办法]
没用过,是否应该判断 <=0 GOOGLE之摘录返回值供参考
RETURN VALUES
The following return values can occur:
>0
The read operation was successful; the return value is the number of bytes actually read from the TLS/SSL connection.
0
The read operation was not successful. The reason may either be a clean shutdown due to a ``close notify'' alert sent by the peer (in which case the SSL_RECEIVED_SHUTDOWN flag in the ssl shutdown state is set (see SSL_shutdown(3), SSL_set_shutdown(3)). It is also possible, that the peer simply shut down the underlying transport and the shutdown is incomplete. Call SSL_get_error() with the return value ret to find out, whether an error occurred or the connection was shut down cleanly (SSL_ERROR_ZERO_RETURN).
SSLv2 (deprecated) does not support a shutdown alert protocol, so it can only be detected, whether the underlying connection was closed. It cannot be checked, whether the closure was initiated by the peer or by something else.
<0
The read operation was not successful, because either an error occurred or action must be taken by the calling process. Call SSL_get_error() with the return value ret to find out the reason.