/** * gcc server.c -lcrypto -o server */ #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <errno.h> #include <openssl/md5.h> #include <string.h> #include <stdio.h> void gen_md5(char *data, char *md5) { unsigned char md[16] = {0}; int i = 0; char tmp[3] = {'\0'}; MD5((const unsigned char *)data, strlen((char *)data), md); for (i = 0; i < 16; i++) { sprintf(tmp, "%02X", md[i]); strcat(md5, tmp); } } void print_package(char *package) { char package_md5[33] = {0}; char package_data[100] = {0}; char cal_md5[33] = {'\0'}; memcpy(package_md5, &package[1], 32); memcpy(package_data, &package[33], strlen(package) - 33); printf("数据包的长度 【%d】 \n", package[0]); printf("数据包md5 【%s】 \n", package_md5); printf("数据包字符串 【%s】 \n", package_data); gen_md5(package_data, cal_md5); if (strcmp(package_md5, cal_md5) == 0) { puts("md5校验通过"); } else { printf("md5校验未通过package_md5 %s和cal_md5 %s不匹配", package_md5, cal_md5); } } int main() { int listenfd, connfd; struct sockaddr_in servaddr; char package[129]; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(5000); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); listenfd = socket(AF_INET, SOCK_STREAM, 0); bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); listen(listenfd, 5); for (;;) { connfd = accept(listenfd, (struct sockaddr *)NULL, NULL); puts("***********************************"); memset(package, 0, sizeof(package)); recv(connfd, package, 129, 0); puts("接收数据ok"); print_package(package); write(connfd, "ok", 3); close(connfd); puts("关闭连接"); puts("***********************************"); } }
/** * gcc client.c -lcrypto -o client */ #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <errno.h> #include <openssl/md5.h> #include <string.h> #include <stdio.h> void gen_md5(char *data, char *md5) { unsigned char md[16] = {0}; int i = 0; char tmp[3] = {'\0'}; MD5((const unsigned char *)data, strlen((char *)data), md); for (i = 0; i < 16; i++) { sprintf(tmp, "%02X", md[i]); strcat(md5, tmp); } } void build_package(char *data, char *package) { char md5[33] = {'\0'}; int length; gen_md5(data, md5); length = strlen((char *)md5) + strlen((char *)data); memset(package, '\0', sizeof(package)); package[0] = length; strcpy(&package[1], (char *)md5); strcpy(&package[33], (char *)data); // strcpy(&package[33], "helle world"); } int main(void) { int sockfd, n; struct sockaddr_in servaddr; char server_reply[1024]={0}; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(5000); inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr); sockfd = socket(AF_INET, SOCK_STREAM, 0); connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); char data[] = "hello world"; char package[129] = {0}; build_package(data, package); send(sockfd, package, strlen(package), 0); puts("发送数据ok"); recv(sockfd, server_reply, 1024, 0); puts("接收数据ok"); puts(server_reply); close(sockfd); return 0; }