TCP多主机聊天程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liu_zhen_kai/article/details/82824146

一台服务器的一个服务端为多台主机的客户端提供TCP服务项目。

多进程实现

  • 多进程服务端代码
  

    1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <unistd.h>
      4 #include <sys/types.h>
      5 #include <sys/socket.h>
      6 #include <netinet/in.h>
      7 #include <arpa/inet.h>
      8 #include <string.h>
      9 
     10 void serverIO(int fd)
     11 {
     12     while(1)
     13     {
     14         char buf[1024];
     15         int s = read(fd, buf, 1023);
     16         if(s < 0)
     17         {
     18             perror("read");
     19             break;
     20         }
     21         else if(s == 0)
     22         {
     23             printf("Connect Was Destroyed!!\n");
     24             break;
     25         }
     26         else
     27         {
     28             buf[s + 1] = '\0';
     29             printf("Client Say # %s\n", buf);
     30             continue;
     31         }
     32     }
     33 }
     34 
     35 int main(int argc, char* argv[])
     36 
     37 {
     38     if(argc != 3)
     39     {
     40         printf("Please Input With IP and Port!!\n");
     41         return 1;
     42     }
     43 
     44     int listen_sock = socket(AF_INET, SOCK_STREAM, 0);
     45     if(listen_sock < 0)
     46     {
     47         perror("socket");
     48         return 2;
     49     }
     50 
     51     struct sockaddr_in server;
     52     server.sin_addr.s_addr = inet_addr(argv[1]);
     53     server.sin_port = htons(atoi(argv[2]));
     54     server.sin_family = AF_INET;
     55     if(bind(listen_sock, (struct sockaddr*)&server, sizeof(server)) == -1)
     56     {
     57         perror("bind");
     58         return 3;
     59     }
     60 
     61     //开始监听
     62     if(listen(listen_sock, 5) < 0)
     63     {
     64         perror("listen");
     65         return 4;
     66     }
     67 
     68     printf("Listen Successfully!!\n");
     69 
     70     while(1)
     71     {
     72         struct sockaddr_in client;
     73         socklen_t len = sizeof(client);
     74         int new_sock = accept(listen_sock, (struct sockaddr*)&client, &len);
     75         if(new_sock < 0)
     76         {
     77             perror("accept");
     78             continue;
     79         }
     80         printf("Accept Successfully , Message From [%s][%d]!!\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
     81         //为了实现可以有多个客户端访问的同时,服务器能对其都提供服务,因此采用多进程版本
     82         pid_t pid = fork();
     83         if(pid == 0){
     84 
     85             close(listen_sock);
     86 
     87             if(fork() > 0)
     88             {
     89                 exit(0);
     90             }
     91             //在子进程中再创建一个孙子进程,再使子进程退出,而祖父进程不需要等待孙子进程,所以这样就能解决进程等待的问题
     92 
     93             serverIO(new_sock);
     94             exit(0);
     95         }
     96         else
     97         {
     98             close(new_sock);//父进程是获取成功队列,子进程提供服务,而一个进程的文件描述符个数有限,所以new_sock在父进程中必须被关闭
     99             waitpid(pid, NULL, 0);
    100         }
    101         serverIO(new_sock);
    102     }
    103 
    104     return 0;
    105 }

  • 多进程客户端代码
1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <sys/types.h>
  4 #include <netinet/in.h>
  5 #include <arpa/inet.h>
  6 #include <sys/socket.h>
  7 #include <string.h>
  8 
  9 int main(int argc, char* argv[])
 10 {
 11     if(argc != 3)
 12     {
 13         printf("Without IP and port...!!!\n");
 14         return 0;
 15     }
 16 
 17     int sock = socket(AF_INET, SOCK_STREAM, 0);
 18     if(sock < 0)
 19     {
 20         perror("socket");
 21         return 1;
 22     }
 23 
 24     struct sockaddr_in server;
 25     server.sin_family = AF_INET;
 26     server.sin_port = htons(atoi(argv[2]));
 27     server.sin_addr.s_addr = inet_addr(argv[1]);
 28 
 29     if(connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0){
 30         perror("connect");
 31         return 3;
 32     }
 33 
 34     printf("connect success...!!!\n");
 35 
 36     char buf[1024];
 37     while(1){
 38         printf("Please Input : ");
 39         scanf("%s", buf);
 40         write(sock, buf, strlen(buf)-1);
 41     }
 42 
 43     return 0;
 44 }

多线程实现

  • 多线程下的服务端代码
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 #include <sys/types.h>
  5 #include <sys/socket.h>
  6 #include <netinet/in.h>
  7 #include <arpa/inet.h>
  8 #include <string.h>
  9 
 10 
 11 
 12 void serverIO(int fd)
 13 {   
 14     while(1)
 15     {   
 16         char buf[1024];
 17         int s = read(fd, buf, 1023);
 18         if(s < 0)
 19         {   
 20             perror("read");
 21             break;
 22         }
 23         else if(s == 0)
 24         {   
 25             printf("Connect Was Destroyed!!\n");
 26             break;
 27         }
 28         else
 29         {   
 30             printf("Client Say # %s\n", buf);
 31             continue;
 32         }
 33     }
 34 }
 35 
 36 void* service(void* arg)
 37 {   
 38     serverIO((int)arg);
 39 }
 40 
 41 int main(int argc, char* argv[])
 42 
 43 {
 44     if(argc != 3)
 45     {
 46         printf("Please Input With IP and Port!!\n");
 47         return 1;
 48     }
 49 
 50     int listen_sock = socket(AF_INET, SOCK_STREAM, 0);
 51     if(listen_sock < 0)
 52     {
 53         perror("socket");
 54         return 2;
 55     }
 56 
 57     struct sockaddr_in server;
 58     server.sin_addr.s_addr = inet_addr(argv[1]);
 59     server.sin_port = htons(atoi(argv[2]));
 60     server.sin_family = AF_INET;
 61     if(bind(listen_sock, (struct sockaddr*)&server, sizeof(server)) == -1)
 62     {
 63         perror("bind");
 64         return 3;
 65     }
 66 
 67     //开始监听
 68     if(listen(listen_sock, 5) < 0)
 69     {
 70         perror("listen");
 71         return 4;
 72     }
 73 
 74     printf("Listen Successfully!!\n");
 75 
 76     while(1)
 77     {
 78         struct sockaddr_in client;
 79         socklen_t len = sizeof(client);
 80         int new_sock = accept(listen_sock, (struct sockaddr*)&client, &len);
 81         if(new_sock < 0)
 82         {
 83             perror("accept");
 84             continue;
 85         }
 86         pthread_t id;
 87         printf("Accept Successfully , Message From [%s][%d]!!\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
 88         //多线程版本的server服务器  
 89         pthread_create(&id, NULL, service, (void*)new_sock);
 90         pthread_detach(&id);
 91     }
 92 
 93     return 0;
 94 }

  • 多线程下的客户端代码
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <sys/types.h>
  4 #include <netinet/in.h>
  5 #include <arpa/inet.h>
  6 #include <sys/socket.h>
  7 #include <string.h>
  8 
  9 int main(int argc, char* argv[])
 10 {
 11     if(argc != 3)
 12     {
 13         printf("Without IP and port...!!!\n");
 14         return 0;
 15     }
 16 
 17     int sock = socket(AF_INET, SOCK_STREAM, 0);
 18     if(sock < 0)
 19     {
 20         perror("socket");
 21         return 1;
 22     }
 23 
 24     struct sockaddr_in server;
 25     server.sin_family = AF_INET;
 26     server.sin_port = htons(atoi(argv[2]));
 27     server.sin_addr.s_addr = inet_addr(argv[1]);
 28 
 29     if(connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0){
 30         perror("connect");
 31         return 3;
 32     }
 33 
 34     printf("connect success...!!!\n");
 35 
 36     char buf[1024];
 37     while(1){
 38         printf("Please Input : ");
 39         scanf("%s", buf);
 40         write(sock, buf, strlen(buf)-1);
 41     }
 42 
 43     return 0;
 44 }

猜你喜欢

转载自blog.csdn.net/liu_zhen_kai/article/details/82824146