阅读量:0
1,实验内容
1,操作人员在命令行窗口输入一行字符并回车后,客户端进程立刻从命令行(本质即stdin)读取数据,并将该行信息发送给服务器。
2,服务器收到该行信息后,会将该信息原封不动的返回给客户端,即所谓消息回声(Message Echo)。
3,客户端收到服务器返回的消息回声后,将其打印输出至屏幕(本质即stdout ) 。
4,客户端在从命令行收到EXIT指令后退出。
5,在启动1个客户端连接上服务器开展交互时,再启动另一个客户端连接服务器,观察体验是什么现象,并尝试分析现象背后的底层逻辑。
2,实验环境
Linux:Ubuntu 22.04
gcc:11.3.0
3,实验流程
4,代码实现
服务器(server.c)
//server.c #include <arpa/inet.h> #include <errno.h> #include <netinet/in.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <sys/socket.h> #include <unistd.h> void usage(const char *str) { printf("%s usage:<IP><PORT><veri_code>\n", str); } int sigint_flag = 0; // 信号 void handle_sigint(int sig) { printf("[srv] SIGINT is coming!\n"); sigint_flag = 1; } void srv_biz(int connfd) {} //./serv 127.0.0.1 8000 int main(int argc, char *argv[]) { char *left = "("; char *right = ")"; // 存放转换后的ip地址 char ip_str[20] = {0}; // 安装使用sigint信号处理器 struct sigaction sa; sa.sa_flags = 0; sa.sa_handler = handle_sigint; sigemptyset(&sa.sa_mask); // 注册信号处理器 sigaction(SIGINT, &sa, NULL); struct sockaddr_in serv_addr, clie_addr; int listenfd, connectfd; int ret; socklen_t clie_addr_len; // 判断输入的参数 char *veri_code = argv[3]; if (argc != 4) { usage(argv[0]); return 1; } // 建立套接字,返回监听套接字 listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; // 地址族协议(IPv4orIPv6) serv_addr.sin_port = htons(atoi(argv[2])); // 端口 serv_addr.sin_addr.s_addr = inet_addr(argv[1]); // IP地址 // tes存放返回结果 const char *tes; // 使用inet_ntop函数转换ip tes = inet_ntop(AF_INET, &serv_addr.sin_addr, ip_str, sizeof(ip_str)); // 打印初始化信息 printf("[srv] server[%s:%d][%s] is initializing!\n", ip_str, ntohs(serv_addr.sin_port), argv[3]); printf("[srv] Server has initialized!\n"); if (listenfd < 0) { perror("socket"); return 1; } // 给套接字绑定一个端口和IP ret = bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); if (ret < 0) { perror("bind"); return 1; } // 开始监听 ret = listen(listenfd, 20); if (ret < 0) { perror("listen"); return 1; } // 开启accpet()主循环,直至sigint_flag指示服务器退出; while (!sigint_flag) { clie_addr_len = sizeof(clie_addr); // 要一个客户端进程进行处理,返回参数主要是有一个客户端通信描述符cfd,程序就是通过这个cfd和客户端进行交互 connectfd = accept(listenfd, (struct sockaddr *)&clie_addr, &clie_addr_len); if (connectfd < 0) { if (errno == EINTR) continue; else perror("accept"); return 1; } else { tes = inet_ntop(AF_INET, &clie_addr.sin_addr, ip_str, sizeof(ip_str)); printf("[srv] client[%s:%d] is accepted!\n", ip_str, (int)ntohs(clie_addr.sin_port)); } int n = 0; char buf[140] = {0}; char *to_exit = "EXIT"; // 收发业务小循环 while (1) { memset(buf, 0, 140); // 最多读sizeof(buf)-1,留下一个空间放\0 n = read(connectfd, buf, sizeof(buf) - 1); int if_exit = strncmp(buf, to_exit, 4); if (n <= 0 || if_exit == 0) { printf("[srv] client[%s:%d] is closed!\n", ip_str, (int)ntohs(clie_addr.sin_port)); break; } buf[n] = 0; printf("[ECH_RQT]%s", buf); char ret_buf[140]; memset(ret_buf, 0, 140); strcat(ret_buf, left); strcat(ret_buf, veri_code); strcat(ret_buf, right); int length = strlen(ret_buf); for (int i = 0; i < strlen(buf); i++) { ret_buf[length + i] = buf[i]; if (buf[i] == '\n') break; } ret_buf[strlen(ret_buf)] = '\0'; write(connectfd, ret_buf, strlen(ret_buf) + 2); memset(ret_buf, 0, 140); } // 关闭连接 close(connectfd); printf("[srv] connfd is closed!\n"); } // 关闭监听 close(listenfd); printf("[srv]listenfd is closed!\n"); printf("[srv]server is to return! \n!"); return 0; }
客户端(client.c)
//client.c #include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <sys/socket.h> #include <unistd.h> void usage(const char *str) { printf("%s usage:<IP><PORT><veri_code>\n", str); } int main(int argc, char *argv[]) { // 存放ip_str char ip_str[20] = {0}; if (argc != 3) { usage(argv[0]); return 1; } struct sockaddr_in serv_addr; int sockfd; // 返回客户端的进程描述符 sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket"); return 1; } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(atoi(argv[2])); serv_addr.sin_addr.s_addr = inet_addr(argv[1]); // connect表示客户端与服务端尝试建立连接 // connect里面的输入参数有要连接服务端的地址 // connect执行的时候就会被服务端listen检测到 // 如果accept没有阻塞的话,这时候就会进行三次握手 // 三次握手完成以后,服务端执行完accept,客户端执行完connect // 发起客户端请求连接 int ret = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); if (ret < 0) { perror("connect"); return 1; } else { // 转换IP地址 inet_ntop(AF_INET, &serv_addr.sin_addr, ip_str, sizeof(ip_str)); printf("[cli] server[%s:%d] is connected!\n", ip_str, ntohs(serv_addr.sin_port)); } char buf[140] = {0}; char *to_exit = "EXIT"; while (1) { memset(buf, 0, 140); fgets(buf, 140, stdin); int if_exit = strncmp(buf, to_exit, 4); if (if_exit == 0) break; // 写入并打印写入内容 write(sockfd, buf, strlen(buf)); printf("[ECH_RQT]%s", buf); memset(buf, 0, 140); read(sockfd, &buf, sizeof(buf) - 1); // 显示服务器发送的内容 printf("[ECH_REP]%s", buf); memset(buf, 0, 140); } close(ret); printf("[cli]listenfd is closed!\n"); printf("[cli]server is to return! \n!"); return 0; }
5,编译运行
编译服务器(server.c)
gcc -o server.o server.c
编译客户端(client.c)
gcc -o client.o client.c
初始化服务器
./server.o 127.0.0.1 12345 8888 //127.0.0.1为ip地址,12345为端口号,8888为验证码
客户端连接至服务器
./client.o 127.0.0.1 12345
6,测试结果
客户端
[cli] server[127.0.0.1:12345] is connected! hello,world! [ECH_RQT]hello,world! [ECH_REP](8888)hello,world! this is Max speaking. [ECH_RQT]this is Max speaking. [ECH_REP](8888)this is Max speaking. inkqonciorwemciojurtbvlanjkx [ECH_RQT]inkqonciorwemciojurtbvlanjkx [ECH_REP](8888)inkqonciorwemciojurtbvlanjkx EXIT [cli]listenfd is closed! [cli]server is to return!
服务器
[srv] server[127.0.0.1:12345][8888] is initializing! [srv] server has initialized! [srv] client[127.0.0.1:52602] is accepted! [ECH_RQT]hello,world! [ECH_RQT]this is Max speaking. [ECH_RQT]inkqonciorwemciojurtbvlanjkx [srv] client[127.0.0.1:52602] is closed! [srv] connfd is closed! [srv] SIGINT is coming! [srv]listenfd is closed! [srv]server is to return!