回射程序改进1
回射程序改良,将源main函数中的某些操作定义为单独的函数,使其便于修改
chat_cli.c 文件
1 #include "net.h" 2 3 int main(int argc, char **argv) 4 { 5 int sockfd; 6 7 if (argc != 2) 8 { 9 printf("Error arg!\n"); 10 exit(1); 11 } 12 sockfd = tcp_connect(argv[1], SERV_PORT); 13 printf("Success init!\n"); 14 cli_io(sockfd); 15 16 return 0; 17 }
fun_cli.c 文件
1 #include "net.h" 2 3 // 建立一个TCP套接字(IPv4),并与给定主机端口连接,并返回连接后的套接字 4 int tcp_connect(char *ser_ip, int port) 5 { 6 int sockfd; 7 struct sockaddr_in servaddr; 8 9 if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) 10 { 11 printf("Error socket!\n"); 12 exit(1); 13 } 14 15 bzero(&servaddr, sizeof(servaddr)); 16 servaddr.sin_family = AF_INET; 17 servaddr.sin_port = htons(port); 18 19 if (inet_pton(AF_INET, ser_ip, &servaddr.sin_addr) <= 0) 20 { 21 printf("Error inet_pton!\n"); 22 exit(1); 23 } 24 25 if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) 26 { 27 printf("Error connect!\n"); 28 exit(1); 29 } 30 31 return sockfd; 32 } 33 34 // 回射程序客户端对套接字的读写 35 void cli_io(int sockfd) 36 { 37 int n; 38 char sendline[MAXLINE], recvline[MAXLINE]; 39 40 while (fgets(sendline, MAXLINE, stdin) != NULL) 41 { 42 if (write(sockfd, sendline, strlen(sendline)) < 0) 43 { 44 printf("Error write!\n"); 45 exit(1); 46 } 47 48 if ( (n = read(sockfd, recvline, MAXLINE)) > 0 ) 49 { 50 recvline[n] = '\0'; 51 fputs(recvline, stdout); 52 } 53 } 54 55 return; 56 }
chat_serv.c 文件
1 #include "net.h" 2 3 int main(int argc, char **argv) 4 { 5 int listenfd, connfd, n; 6 struct cliaddr; 7 pid_t childpid; 8 socklen_t clilen; 9 10 listenfd = tcp_listen(SERV_PORT); 11 12 for ( ; ; ) 13 { 14 clilen = sizeof(cliaddr); 15 16 if ( (connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) < 0 ) 17 { 18 printf("Error accept!\n"); 19 exit(0); 20 } 21 22 if ( (childpid = fork()) == 0 ) 23 { 24 if (close(listenfd) < 0) 25 { 26 printf("Error close!\n"); 27 exit(1); 28 } 29 30 serv_io(connfd); 31 exit(0); 32 } 33 } 34 }
fun_serv.c文件
1 #include "net.h" 2 3 // 创建一个tcp套接字,并在指定端口上监听 4 int tcp_listen(int port) 5 { 6 int listenfd; 7 struct sockaddr_in servaddr; 8 9 if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) 10 { 11 printf("Error socket!\n"); 12 exit(1); 13 } 14 15 bzero(&servaddr, sizeof(servaddr)); 16 servaddr.sin_family = AF_INET; 17 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 18 servaddr.sin_port = htons(port); 19 20 if (bind(listenfd, (SA *) &servaddr, sizeof(servaddr)) < 0) 21 { 22 printf("Error bind!\n"); 23 exit(1); 24 } 25 26 if (listen(listenfd, LISTENQ) < 0) 27 { 28 printf("Error listen!\n"); 29 exit(1); 30 } 31 32 return listenfd; 33 } 34 35 // 服务端io 36 void serv_io(int connfd) 37 { 38 char buff[MAXLINE]; 39 int n; 40 41 while ( (n = read(connfd, buff, MAXLINE)) > 0 ) 42 { 43 buff[n] = '\0'; 44 fputs(buff, stdout); 45 46 if (write(connfd, buff, strlen(buff)) < 0) 47 { 48 printf("Error write!\n"); 49 exit(1); 50 } 51 } 52 53 return; 54 }
转载请注明出处
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步