C代码运行中报Segmentation fault,求指点

C语言 码拜 8年前 (2016-09-12) 1081次浏览
C代码如下:

/*
* https_client.c —— 无差错处理和证书逻辑处理的简易HTTPS客户端
* 用法: 
* gcc -o https_client -lssl -lcrypt https_client.c
* https_client 主机名
*/
#include <stdio.h>
#include <memory.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
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
求指点!

解决方案

68

你已经指定协议是HTTPS了,参数中的主机名就不要带https://了,./https_client www.baidu.com试试

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C代码运行中报Segmentation fault,求指点
喜欢 (0)
[1034331897@qq.com]
分享 (0)