编程实现与时间戳服务器的通信

在上一篇博文《介绍一个法国的时间戳服务器》中,介绍了获取时间戳响应的方法,分为两步:1. 使用 OpenSSL 生成一个时间戳请求文件;2. 使用 Linux 下的 curl 命令,发送时间戳请求、接收时间戳响应。

要想在 Windows 下执行第 2 步,有两种途径:1) 下载为 Windows 平台实现的 curl 程序,可以到 下载;2)不用 curl 命令,,自己编程实现向那个法国的时间戳服务器发送时间戳请求、接收时间戳响应。

为了更好地了解与时间戳服务器通信的过程细节,这里介绍一下第 2 种途径,即编程实现,先介绍 C 语言实现:a) 为了读取时间戳请求文件,要实现获取文件大小的函数,实现过程如下: 文件1 get_small_file_size.h

#ifndef GET_SMALL_FILE_SIZE_H #define GET_SMALL_FILE_SIZE_H#ifdef __cplusplusextern "C" {#endif/***************************************************函数名称:GetSmallFileSize*功能: 获取文件的大小,结果以字节为单位*参数:file_name[in]文件名file_byte_size[out] 文件大小*返回值:0 成功-1 失败*备注: 该函数对实际文件大小有限制,文件大小不能超过 (2G-1) Bytes, 因为调用了 ftell() 函数,该函数的返回值是 long 类型,能表示的 整数最大不会超过 (2G-1) Bytes**************************************************/int GetSmallFileSize(char *file_name, long *file_byte_size);#ifdef __cplusplus}#endif#endif /* end of GET_SMALL_FILE_SIZE_H */

文件2 get_small_file_size.c

#include "get_small_file_size.h"#include <stdio.h>int GetSmallFileSize(char *file_name, long *file_byte_size){ FILE * fp; if ( !(fp=fopen(file_name, "rb")) ) {printf("Open file %s failed!\n", file_name);return (-1); } fseek(fp, 0L, SEEK_END); *file_byte_size=ftell(fp); fclose(fp); return 0; }

b) 为了与时间戳服务器通信,要实现用 Windows socket 方式通信的函数,实现过程如下: 文件3 socket_communication.h

#ifndef SOCKET_COMMUNICATION_H #define SOCKET_COMMUNICATION_H#ifdef __cplusplusextern "C" {#endif/***************************************************函数名称:socket_communication*功能: 以 socket 通信方式向指定 IP 地址和端口号的主机发送请求数据,接收响应数据*参数:ip[in]对方主机的 IP 地址port[in]对方主机接收数据时使用的端口号request[in]缓冲区首地址,该缓冲区用来存放要发送的请求数据request_len[in]要发送的请求数据的长度,以字节为单位response[out]缓冲区首地址,该缓冲区用来存放接收到的响应数据response_buffer_len[in] 存放接收到的响应数据的缓冲区大小,以字节为单位response_len[out]实际接收到的响应数据的长度,以字节为单位*返回值:0 成功-1 失败**************************************************/int socket_communication(unsigned char *ip,unsigned int port,unsigned char *request,unsigned int request_len,unsigned char *response,unsigned int response_buffer_len,unsigned int *response_len);#ifdef __cplusplus}#endif#endif /* end of SOCKET_COMMUNICATION_H */

文件4 socket_communication.c

#include "socket_communication.h"#include <stdio.h>#include <Winsock2.h>#pragma comment(lib,"WS2_32.lib")int socket_communication(unsigned char *ip,unsigned int port,unsigned char *request,unsigned int request_len,unsigned char *response,unsigned int response_buffer_len,unsigned int *response_len){ int result, received_data_len; char *p; WSADATA wsaData; SOCKET sockClient; SOCKADDR_IN addrSrv; result = WSAStartup(MAKEWORD(2, 2), &wsaData); if (result != NO_ERROR) {printf("WSAStartup function failed with error: %d at %s, line %d!\n", result, __FILE__, __LINE__);return (-1); } sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if( sockClient == INVALID_SOCKET ) {printf("Create socket failed with error: %ld at %s, line %d!\n", WSAGetLastError(), __FILE__, __LINE__);WSACleanup();return (-1); } addrSrv.sin_addr.s_addr = inet_addr((char *)(ip)); addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(port); result = connect(sockClient, (SOCKADDR*)(&addrSrv), sizeof(SOCKADDR)); if (result == SOCKET_ERROR) {printf("Invoke connect() function failed with error: %ld at %s, line %d!\n", WSAGetLastError(), __FILE__, __LINE__);closesocket(sockClient);WSACleanup( );return (-1); } printf("Sending data…"); result = send(sockClient, (char *)request, (int)request_len, 0); if (result == -1) {printf("Send data failed at %s, line %d!\n", __FILE__, __LINE__);closesocket(sockClient);WSACleanup( );return (-1); } printf("%d bytes have been sent.\n", result); printf("Receiving data…"); p = (char *)response; received_data_len = 0; while (1) {result = recv(sockClient, p, (response_buffer_len – received_data_len), 0);if (result == -1){printf("Receive data failed at %s, line %d!\n", __FILE__, __LINE__);closesocket(sockClient);WSACleanup();return (-1);} if (result == 0)break; p += result; received_data_len += result; } *response_len = received_data_len; printf("Receiving data complete.\n"); printf("%d bytes have been received.\n", received_data_len);result = closesocket(sockClient); if (result == SOCKET_ERROR) {printf("Invoke closesocket() function failed with error: %ld at %s, line %d!\n", WSAGetLastError(), __FILE__, __LINE__);WSACleanup();return (-1); } WSACleanup(); return 0;}

可是,我却迈不开步伐,怎么也走不出那个圈……

编程实现与时间戳服务器的通信

相关文章:

你感兴趣的文章:

标签云: