2011-06-29 19 views
1

私はOpenSSLライブラリを学習しています。 この問題はBIO_read()は常に0の値を返すということです私のコードBIO_readは常に0を返します

#include <openssl/ssl.h> 
#include <openssl/bio.h> 
#include <openssl/err.h> 
#include <stdio.h> 

void connect_openssl(); 

int main (int argc, char **argv) 
{ 
    CRYPTO_malloc_init(); 
    SSL_library_init(); 
    SSL_load_error_strings(); 
    ERR_load_BIO_strings(); 
    OpenSSL_add_all_algorithms(); 

    connect_openssl(); 

    return 0; 
} 



void connect_openssl() 
{ 
    BIO *bio = BIO_new_connect("google.com:80"); 
    char buffer[1024]; 

    if (bio == NULL) 
    { 
     printf("Error creating BIO! \n"); 
     //ERR_print_errors(stderr); 
     return; 
    } 

    if (BIO_do_connect(bio) <= 0) 
    { 
     printf("Failed to connect! \n"); 
     return; 
    } 

    char buff[1024];; 
    char send[1024]; 

    memset(send, 0, sizeof(send)); 
    strcat(send, "GET/HTTP/1.1\nHost:google.com\nUser Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)\nConnection: Close\n\n"); 

    BIO_puts(bio, send); 

    while (1) 
    { 
     int x = BIO_read(bio, buff, sizeof(buff)-1); 
     if (x == 0) 
     { 
      printf("x==0\n"); 
      break; 
     } 
     else if (x < 0) 
     { 
      if (! BIO_should_retry(bio)) 
      { 
       printf("\nRead failed!\n"); 
       BIO_free_all(bio); 
       return; 
      } 
     } 
     else 
     { 
      buffer[x] = 0; 
      printf("DATA:\n\n"); 
      printf("%s", buffer); 
     } 
    } 

    BIO_free_all(bio); 
    return; 
} 

です。誰かがこのコードの何が間違っているか教えてもらえますか? なんてこった、愚かな質問:

+0

私は_always_が '0'を返すとは思わない。私があなたのプログラムを走らせたとき、私は次の出力を得ました: 'DATA:\ n \ n @、'ûx== 0'。それを通る少なくとも1つのループが非ゼロを返しました。 (実際の改行ですが、コメントはそれを許しません)。 – sarnold

答えて

0

自動応答/ アプリケーションの間違った作業のギルティが二行

buffer[x] = 0; 
printf("%s", buffer); 

ているこれは間違いなく、私はより多くの睡眠を必要とする

buff[x] = 0; 
printf("%s", buff); 

でなければなりません。

関連する問題