C语言 Openssl BIO_read does not return



我有一个问题与BIO_read我没有摆脱。我想将来自服务器的行打印到屏幕上。在gdb的帮助下,我可以找到问题所在,但我现在不知道该怎么办。问题是函数被正确调用,但是当BIO_read被执行时,感觉就像一个无尽的循环,因为它没有返回或结束。我的代码是这样的:

//This function should print one line to screen
int WebPrintLine(BIO *bio) {
unsigned char *x;
unsigned int i;
unsigned char buffer[4096];
unsigned int sizeofbuffer = sizeof(buffer);
//Print each char until newline char or terminator char appear and next loop turn if it would affect memory not owned by the arry buffer
do {
    WebRead(bio, buffer, sizeofbuffer);
    for( i = 0; (buffer[i] != 'n') && (buffer[i] != '') && (i < sizeofbuffer); i++ )
        putchar(buffer[i]);
}while( (buffer[i] != 'n') && (buffer[i] != '') && (x != 0) );
putchar('n');
return 0;
}

/*This function should receive and returns a char to the buffer
BIO_gets could also be used to get just one line but it does not work always so I have  not put it in
Return 0 on error*/
unsigned char *WebRead(BIO *bio, unsigned char buffer[], int sizeofbuffer) {
int bytes_read; //Temporarily store how many bytes were read: for error checking
bytes_read = BIO_read(bio, buffer, sizeofbuffer);
if( bytes_read == 0 ) {
    //No more data available on an non-blocking connection
    return 0;
}
else if( bytes_read < 0 ) {
    //Error occured, retry and if this fails return  0
    if( ! BIO_should_retry(bio) )
        return 0;
}
return buffer;
}

我期待着你的来信。

函数WebPrintLine之前被调用了两次并且工作正常。但是在gdb中,当调用BIO_read时,程序会卡住。

有没有人有过这样的经历,我找不到任何相关的东西,在他们的网站:openssl没有提到这样的错误,或者我还没有找到它。

Ok,

我发现我的错误了。如果有人和我有同样的问题,我就写这篇文章:
BIO_write(bio, buf_write, strlenbuf)

问题不是BIO_write,而是strlenbuf的值。我在buf_write数组中添加了两个字符,但没有更新变量strlenbuf,并存储了字符串的长度(strlenbuf = strlen(buf_write)),所以字符串terminato('')没有发送,所以我还没有完成我的服务器请求,这导致了这个问题。

相关内容

  • 没有找到相关文章

最新更新