关于网友提出的“ C代码运行中报Segmentation fault,求指点”问题疑问,本网通过在网上对“ C代码运行中报Segmentation fault,求指点”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: C代码运行中报Segmentation fault,求指点描述:
COpenSSL
C代码如下:
/*
* https_client.c —— 无差错处理和证书逻辑处理的简易HTTPS客户端
* 用法:
* gcc -o https_client -lssl -lcrypt https_client.c
* https_client 主机名
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void main(int argc, char **argv)
{
SSL *ssl;
SSL_CTX *ctx;
SSL_METHOD *client_method;
X509 *server_cert;
int sd,err;
char *str,*hostname,outbuf[4096],inbuf[4096],host_header[512];
struct hostent *host_entry;
struct sockaddr_in server_socket_address;
struct in_addr ip;
char *ptr, **pptr;
char ipstr[32];
/*
* (1) 初始化SSL库,通过调用SSL_CTX_new创建本地上下文,以记录握手参数及与SSL连接有关的其他状态
*/
SSLeay_add_ssl_algorithms();
client_method = SSLv23_client_method();
SSL_load_error_strings();
ctx = SSL_CTX_new(client_method);
printf("(1) SSL 上下文初始化完毕 \n\n");
/*
* (2) 将服务端主机名转换为IP地址
*/
hostname = argv[1];
host_entry = gethostbyname(hostname);
bcopy(host_entry->h_addr, &(ip.s_addr), host_entry->h_length);
printf("(2) '%s' 的IP地址为 '%s'\n\n", hostname, inet_ntoa(ip));
}
然而执行结果如下:
(1) SSL 上下文初始化完毕
Segmentation fault
求指点!
解决方案1:
你已经指定协议是HTTPS了,参数中的主机名就不要带https://了,./https_client www.baidu.com试试