2012-04-02 41 views
1

這讓我爬上了牆壁。我找不到爲什麼這個來源不打開一個套接字。這很簡單,但不起作用。有人可以幫助我嗎?感謝您的考慮!順便說一句:我沒有在屏幕上顯示文本,並使用BIO_do_accept()函數進行阻止。一些神祕不工作的OpenSSL源碼

#include <openssl/bio.h> 
#include <openssl/err.h> 
#include <openssl/rand.h> 
#include <openssl/ssl.h> 
#include <openssl/x509v3.h> 

#include <iostream> 
#include <process.h> 
using namespace std; 

int main() { 

    SSL_load_error_strings(); 
    SSL_library_init(); 
    OpenSSL_add_all_algorithms(); 

    BIO *abio, *cbio, *cbio2; 
    ERR_load_crypto_strings(); 
    abio = BIO_new_accept("4444"); 

    /* First call to BIO_accept() sets up accept BIO */ 
    if(BIO_do_accept(abio) <= 0) { 
     fprintf(stderr, "Error setting up accept\n"); 
     ERR_print_errors_fp(stderr); 
     exit(0); 
    } 

    /* Wait for incoming connection */ 
    if(BIO_do_accept(abio) <= 0) { 
     fprintf(stderr, "Error accepting connection\n"); 
     ERR_print_errors_fp(stderr); 
     exit(0); 
    } 

    fprintf(stderr, "Connection 1 established\n"); 
    /* Retrieve BIO for connection */ 
    cbio = BIO_pop(abio); 
    BIO_puts(cbio, "Connection 1: Sending out Data on initial connection\n"); 
    fprintf(stderr, "Sent out data on connection 1\n"); 
} 
+0

你真的必須調用BIO_do_accept()兩次嗎? – Castilho 2012-04-02 09:38:36

+0

是的,我不知道爲什麼,但是我做 – Confident 2012-04-02 09:40:22

+0

所以,它阻止在BIO_do_accept()...哪裏的客戶端代碼連接到它? – 2012-04-03 09:11:05

回答

1

我剛剛測試了這個(在Cygwin上,用gcc 4.5.3和安裝了OpenSSL-devel的1.0.1)

code posted in chat編譯

g++ -std=c++0x ./test.cpp -lssl -lcrypto -o test 

生成的代碼顯然不起作用,因爲代碼指的是server.crtserver.key

openssl genrsa -out server.key 1024 
openssl req -new -key server.key -out server.csr 
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt 

使用不受保護的密鑰創建自簽名證書(您將使用genrsa -des3向密鑰添加密碼)。現在

,我可以適當地測試:

test& # in the background 
openssl s_client -connect localhost:12120 

這個土地你在一種啓用SSL telnet客戶端,它很好地工作。

+0

修復了引用的代碼鏈接 – sehe 2012-04-03 13:06:43