1. 程式人生 > >Linux----網路程式設計(TCP網路通訊伺服器客戶端程式設計流程與其迴圈實現)

Linux----網路程式設計(TCP網路通訊伺服器客戶端程式設計流程與其迴圈實現)

一、TCP的伺服器客戶端程式設計流程

1、伺服器 ser.c

  1 #include <stdio.h>
  2 #include <assert.h>
  3 #include <stdlib.h>
  4 #include <unistd.h>
  5 #include <string.h>
  6 #include <sys/socket.h>
  7 #include <netinet/in.h>
  8 #include <arpa/inet.h>
  9 
 10 int main()
 11 {
 12     int sockfd = socket(AF_INET, SOCK_STREAM, 0);//建立套接字
 13     assert(sockfd != -1);
 14 
 15     struct sockaddr_in saddr,caddr;//服務端,客戶端
 16     memset(&saddr, 0, sizeof(saddr));//清屏
 17     saddr.sin_family = AF_INET;//協議
 18     saddr.sin_port = htons(6000);//埠,1024 root 5000,主機轉網路
 19     saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
 20 
 21     int res = bind(sockfd, (struct sockaddr*)&saddr, sizeof(saddr));//繫結
 22     assert(res != -1);
 23 
 24     listen(sockfd, 5);//創立監聽佇列
 25     while(1)
 26     {
 27         int len = sizeof(caddr);
 28         //c連結套接字 ,sockfd接聽套接字
 29        int c = accept(sockfd,(struct sockaddr*)&caddr,&len);
 30        if(c < 0)
 31        {
 32            continue;
 33            }
 34     char buff[128] = {0};
 35     recv(c, buff, 127, 0); //有可能阻塞,一次只能處理一個客戶端
 36     printf("buff=%s\n",buff);
 37     send(c, "ok", 2, 0);
 38     close(c);
 39         }
 40     }

2.客戶端cli.c

socket.h>
  7 #include <netinet/in.h>
  8 #include <arpa/inet.h>
  9 
 10 int main()
 11 {
 12     int sockfd = (AF_INET, SOCK_STREAM, 0);
 13     assert(sockfd != -1);
 14 
 15     struct sockaddr_in saddr;
 16     memset(&saddr, 0, sizeof(saddr));//清空saddr
 17     saddr.sin_family = AF_INET;
 18     saddr.sin_port = htons(6000);
 19     saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
 20 
 21     int res = connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
 22     assert(res != -1);
 23 
 24     char buff[128] = {0};
 25 
 26     printf("input:\n");
 27     fgets(buff, 128, stdin);
 28 
 29     send(sockfd, buff, strlen(buff), 0);
 30     memset(buff, 0, 128);
 31     recv(sockfd, buff, 127,0);
 32     printf("buff=%s\n",buff);
 33 
 34     close(sockfd);
 35     }

二、TCP伺服器客戶端的迴圈實現

1、伺服器端ser1.c

  1 #include <string.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <unistd.h>
  5 #include <assert.h>
  6 #include <sys/socket.h>
  7 #include <netinet/in.h>
  8 #include <arpa/inet.h>
  9 
 10 //伺服器與客戶端的迴圈實現
 11 int main()
 12 {
 13     int sockfd = socket(AF_INET, SOCK_STREAM, 0);//套接字
 14     assert(sockfd != -1);
 15     struct sockaddr_in saddr, caddr;
 16     memset(&saddr,0, sizeof(saddr));
 17     saddr.sin_family = AF_INET;
 18     saddr.sin_port = htons(6001);
 19     saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
 20 
 21     int res = bind(sockfd, (struct sockaddr*)&saddr, sizeof(saddr));
 22     assert(res != -1);
 23     listen(sockfd, 5);//轉為被動監聽套接字,5表示監聽佇列長度
 24 
 25     while(1)
 26     {
 27         int len = sizeof(caddr);
 28         int c = accept(sockfd, (struct sockaddr*)&caddr, &len);
 29         if(c < 0)
 30         {
 31             continue;
 32             }
 33         printf("accept c=%d\n",c);
 34 
 35     while(1)
 36     {
 37         char buff[128] = {0};
 38         int n = recv(c, buff, 127, 0);//返回值為0,表示對方已關閉,開始進行四次揮手
 39         if(n <= 0)
 40         {
 41             break;//若對方關閉連線,則退出小迴圈
 42             }
 43         printf("buff=%s\n", buff);
 44         send(c, "ok", 2, 0);
 45         }
 46          close(c);
 47         }
 48     }
           

2、客戶端cli.c

  1 #include <string.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <unistd.h>
  5 #include <assert.h>
  6 #include <sys/socket.h>
  7 #include <netinet/in.h>
  8 #include <arpa/inet.h>
  9 
 10 //伺服器與客戶端的迴圈實現
 11 int main()
 12 {
 13     int sockfd = socket(AF_INET, SOCK_STREAM, 0);
 14     assert(sockfd != -1);
 15     struct sockaddr_in saddr;
 16     memset(&saddr, 0, sizeof(saddr));
 17     saddr.sin_family = AF_INET;
 18     saddr.sin_port = htons(6001);
 19     saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
 20 
 21     int res = connect(sockfd,(struct sockaddr*)&saddr, sizeof(saddr));
 22     assert(res != -1);
 23 
 24     while(1)
 25     {
 26         char buff[128] = {0};
 27         printf("input:\n");
 28         fgets(buff,128,stdin);
 29         if(strncmp(buff,"end",3) == 0)
 30         {
 31             break;
 32             }
 33         send(sockfd, buff, strlen(buff), 0);
 34         memset(buff, 0, 128);
 35         recv(sockfd, buff, 127, 0);//127改為1,表示一個一個接收
 36         printf("buff=%s\n",buff);
 37         }
 38       close(sockfd);
 39       exit(0);
 40     }