Access Violation错误~~
#include <winsock.h>
#include <stdio.h>
#include<openssl/bio.h>
#include<openssl/ssl.h>
#include<openssl/err.h>
#pragma comment(lib,"libeay32.lib")
#pragma comment(lib,"ssleay32.lib")
#pragma comment( lib, "ws2_32.lib" )
#define CERTF "cert.pem" /*客户端的证书(需经CA签名)*/
#define KEYF "priv.key" /*客户端的私钥(建议加密存储)*/
#define CHK_NULL(x) if ((x)==NULL) exit (-1)
#define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(-2); }
#define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(-3); }
#define MY_PORT 6000
int main() {
int err;
//SOCKET listen_sd,sd;
int listen_sd,sd;
struct sockaddr_in sa_serv,sa_cli;
//size_t client_len;
int client_len;
SSL_CTX* ctx;//ssl上下句柄
SSL* ssl;//ssl结构体指针
X509* client_cert;//x509结构体,用户保存客户端证书
char* str;
char buf[50];
SSL_METHOD *meth;//ssl协议
WSADATA wsaData;
WSAStartup(MAKEWORD(1,1),&wsaData);
meth = SSLv23_server_method();//ssl协议版本
ctx = SSL_CTX_new (meth);//新建ssl上下文句柄
//设置服务器证书
if(SSL_CTX_use_certificate_file(ctx,CERTF,SSL_FILETYPE_PEM) <= 0)
{
ERR_print_errors_fp(stderr);
exit(3);
}
//设置服务器私钥
if(SSL_CTX_use_PrivateKey_file(ctx,KEYF,SSL_FILETYPE_PEM) <= 0)
{
ERR_print_errors_fp(stderr);
exit(4);
}
//检查私钥和证书是否匹配
if(!SSL_CTX_check_private_key(ctx))
{
fprintf(stderr,"Private key does not match the certificate public key \n");
exit(5);
}
以上截取了基于openssl通信服务器端代码的一部分。
运行到ctx = SSL_CTX_new (meth);//新建ssl上下文句柄这句出现acces violation错误,请问如何解决?
[解决办法]
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。
[解决办法]
//初始化OpenSSL环境 SSL_load_error_strings(); SSLeay_add_ssl_algorithms();//SSL协议版本,V2、V3自适应 meth = SSLv23_server_method(); //新建SSL上下文句柄 ctx = SSL_CTX_new (meth); if (!ctx) { ERR_print_errors_fp(stderr); exit(2); }