int sockfd = socket(AF_INET, SOCK_STREAM, 0)
struct hostent *host
host = gethostbyname("www.example.com")
struct sockaddr_in dest_addr
dest_addr.sin_family = AF_INET
dest_addr.sin_port = htons(443)
dest_addr.sin_addr.s_addr = *(long*)(host->h_addr)
connect(sockfd, (struct sockaddr*) &dest_addr, sizeof(dest_addr))
将SSL连接与已经建立的TCP连接绑定,例如:
SSL_set_fd(ssl, sockfd);
进行SSL握手,例如:
SSL_connect(ssl);
构造HTTP请求,并通过SSL连接发送到服务器,例如:
char request[1024];
sprintf(request, "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n");
SSL_write(ssl, request, strlen(request));
从服务器读取响应,例如:
char response[4096]
int len = SSL_read(ssl, response, sizeof(response) - 1)
response[len] = '\0'
关闭SSL连接,例如:
SSL_shutdown(ssl);
SSL_free(ssl);
close(sockfd);
释放SSL上下文,例如:
SSL_CTX_free(ctx);