Linux----网络编程(TCP网络通信服务器客户端编程流程与其循环实现)

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

一、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     }

猜你喜欢

转载自blog.csdn.net/qq_41026740/article/details/83099707