BIO_do_handshake return 0
BIO_do_handshake return 0
我试图写一个非阻塞连接,当我执行BIO_do_handshake()时,使用下面的代码它总是返回0。我知道你不一定需要'do_handshake()',但似乎所有事情都应该做,以确保握手完成。
我应该调用BIO_should_retry()吗?
void SSLAdaptor::Connect( SSL_CTX *ctx
, const std::string &hostname
, int port
, bool nonBlocking )
{
struct hostent *hp;
struct sockaddr_in addr;
SOCKET sockFD;
if( !( hp = gethostbyname( hostname.c_str() ) ) )
{
return;
}
memset( &addr, 0, sizeof( addr ) );
addr.sin_addr= *( struct in_addr* )hp->h_addr_list[0];
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
if( ( sockFD =socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) <0 )
{
return;
}
if(connect(sockFD,(struct sockaddr *)&addr, sizeof(addr))<0)
{
return;
}
SSL *ssl;
BIO *sbio;
ssl = SSL_new( ctx );
sbio = BIO_new_socket( sockFD, BIO_NOCLOSE );
SSL_set_bio( ssl, sbio, sbio );
if( SSL_connect( ssl ) <= 0 )
{
return;
}
long hsReturn = BIO_do_handshake( sbio );
if ( hsReturn <= 0 )
{
return;
}
}
我的(非阻塞)BIO读取返回垃圾:实际:↨♥+o 1E¿={û²∙ìφáD'öQñ↨▼≈☺预期:这是一个测试消息
int SSLSocket::BIORead( std::string &buffer, const int size )
{
int bytesRead = -1;
std::vector<char> readBuffer(size);
std::cout << "Before BIO_read() call" << std::endl;
bytesRead = BIO_read( m_bio, readBuffer.data(), size );
std::cout << "After BIO_read() call" << std::endl;
std::cout << "BIO_read() read : " << bytesRead << " bytes" << std::endl;
std::string tmpBuffer = std::string( readBuffer.begin(), readBuffer.end() );
std::cout << "BIO_read() buffer : " << tmpBuffer << " bytes" << std::endl;
if ( bytesRead < 0 )
{
// Check if should retry read, if not then an error on the connection
// has occurred and the user notified.
if ( BIO_should_retry( m_bio ) )
{
bytesRead = 0;
}
else
{
// real error
bytesRead = -1;
}
}
else if ( bytesRead == 0 )
{
// Nothing was read
bytesRead = 0;
}
else
{
buffer = std::string( readBuffer.begin(), readBuffer.end() );
}
return bytesRead;
}
SSL_connect已经进行了握手,但是您必须正确地使用非阻塞套接字,例如检查SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE并等待所需的条件,只要SSL_connect返回<0。所以不需要调用BIO_do_handshake
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- C++函数中的精确"return"矩
- '[](std::list& list)<int>{return std::move(list)}(list)' 是否保证将 'list' 留空?
- 如何使用 do while 循环确定最高值和最低值
- 方法错误"not all control paths return a value"和方法不返回值
- do while 循环中的 if 语句以 yes 或 no 结尾
- 以某种方式告诉编译器"Do not process line of code"
- GLFW glfwCreateWindowSurface return -7
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- 在 C 或 C++ 中,循环可以同时具有 "do" 和"while"部分吗?
- 如何解决类成员函数中的"return a value"错误?
- 使用 while 或 do while 退出循环
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- C++概念assignable_from不接受 const&-return 运算符=
- 为什么当我输入一个被接受的数字时,我的 do-while 循环没有中断?
- Malloc void return char 数组有时不起作用(Terry Davis 对 C++);
- "Control reaches end on non-void function" with do { return result; } while(condition);