• 回射程序改进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!
    ");
     10         exit(1);
     11     }
     12     sockfd = tcp_connect(argv[1], SERV_PORT);
     13     printf("Success init!
    ");
     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!
    ");
     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!
    ");
     22         exit(1);
     23     }
     24 
     25     if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
     26     {
     27         printf("Error connect!
    ");
     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!
    ");
     45             exit(1);
     46         }
     47 
     48         if ( (n = read(sockfd, recvline, MAXLINE)) > 0 )
     49         {
     50             recvline[n] = '';
     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!
    ");
     19             exit(0);
     20         }
     21         
     22         if ( (childpid = fork()) == 0 )
     23         {   
     24             if (close(listenfd) < 0)
     25             {   
     26                 printf("Error close!
    ");
     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!
    ");
     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!
    ");
     23         exit(1);
     24     }
     25 
     26     if (listen(listenfd, LISTENQ) < 0)
     27     {
     28         printf("Error listen!
    ");
     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] = '';
     44         fputs(buff, stdout);
     45 
     46         if (write(connfd, buff, strlen(buff)) < 0)
     47         {
     48             printf("Error write!
    ");
     49             exit(1);
     50         }
     51     }
     52 
     53     return;
     54 }
  • 相关阅读:
    4种xml解析器区别
    eclipse的快捷键(常用)
    eclipse修改项目访问前缀
    oracle创建用户
    导入安全证书到jdk
    laravel 命令行输出进度条
    sql 事务的四种隔离级别
    supervisor 通过图形界面管理
    supervisor "INFO spawnerr: unknown error making dispatchers for xxx" 错误
    linux expect 的使用
  • 原文地址:https://www.cnblogs.com/lnlin/p/9568279.html
Copyright © 2020-2023  润新知