day38 1220

2023-12-21 06:48:34

作业1:select客户端

  1 #include <myhead.h>
  2 #define SERPORT 8888
  3 #define SERIP "192.168.125.159"
  4 
  5 #define CLIPORT 6666
  6 #define CLIIP "192.168.125.159"
  7 
  8 int main(int argc, const char *argv[])
  9 {
 10     int ret = -1;
 11     int cfd = -1;
 12     cfd = socket(AF_INET,SOCK_STREAM,0);
 13     PERROR_INFO(cfd);
 14 
 15     printf("cfd = %d\n",cfd);
 16 
 17 
 18     int reuse = 1;
 19     ret = setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));
 20     PERROR_INFO(ret);
 21 
 22 
 23     struct sockaddr_in cin;
 24     cin.sin_family = AF_INET;
 25     cin.sin_port = htons(CLIPORT);
 26     cin.sin_addr.s_addr = inet_addr(CLIIP);
 27 
 28     ret = bind(cfd,(struct sockaddr *)&cin,sizeof(cin));
 29     PERROR_INFO(ret);
 30 
 31 
 32     struct sockaddr_in sin;
 33     sin.sin_family = AF_INET;
 34     sin.sin_port = htons(SERPORT);
 35     sin.sin_addr.s_addr = inet_addr(SERIP);
 36     
 37     ret = connect(cfd,(struct sockaddr *)&sin,sizeof(sin));
 38     PERROR_INFO(ret);
 39 
 40     char wbuf[128];
 41 
 42     fd_set readfds;
 43     FD_ZERO(&readfds);
 44     FD_SET(0,&readfds);
 45     FD_SET(cfd,&readfds);
 46 
 47     fd_set tempfds;
 48 
 49 
 50     while (1)
 51     {
 52         tempfds = readfds;
 53         ret = select(cfd+1,&tempfds,NULL,NULL,NULL);
 54 
 55 
 56         if (FD_ISSET(0,&tempfds))
 57         {
 58 
 59             bzero(wbuf,sizeof(wbuf));
 60             fgets(wbuf,sizeof(wbuf),stdin);
 61             wbuf[strlen(wbuf)-1] = 0;
 62 
 63             send(cfd,wbuf,sizeof(wbuf),0);
 64             bzero(wbuf,sizeof(wbuf));
 65 
 66         }
 67 
 68         if (FD_ISSET(cfd,&tempfds))
 69         {
 70             ret = recv(cfd,wbuf,sizeof(wbuf),0);
 71             if (ret == 0)                                                                                                                                                                                                        
 72             {
 73                 printf("serve is offline\n");
 74                 break;
 75             }
 76             printf("[%s:%d]: %s\n",SERIP,SERPORT,wbuf);
 77         }
 78 
 79 
 80     }
 81     close(cfd);
 82 
 83     return 0;
 84 }

作业2:poll服务器

  1 #include <myhead.h>
  2 #define PORT 8888
  3 #define IP "192.168.125.159"
  4 
  5 
  6 int main(int argc, const char *argv[])
  7 {
  8     int ret = -1;
  9     int sfd = -1;
 10     sfd = socket(AF_INET,SOCK_STREAM,0);
 11     PERROR_INFO(sfd);
 12 
 13     printf("sfd = %d\n",sfd);
 14 
 15     int reuse = 1;
 16     ret = setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));
 17     PERROR_INFO(ret);
 18 
 19 
 20     struct sockaddr_in sin;
 21     sin.sin_family = AF_INET;
 22     sin.sin_port = htons(PORT);
 23     sin.sin_addr.s_addr = inet_addr(IP);
 24 
 25     ret = bind(sfd,(struct sockaddr *)&sin,sizeof(sin));
 26     PERROR_INFO(ret);
 27 
 28     ret = listen(sfd,128);
 29     PERROR_INFO(ret);
 30 
 31     //ret = accept(sfd,NULL,NULL);
 32     //PERROR_INFO(ret);
 33 
 34     struct sockaddr_in cin;
 35     socklen_t socklen = sizeof(cin);
 36     int newfd = -1;
 37 
 38     char buf[128] = "";
 39 
 40     struct pollfd fds[1024];
 41     fds[0].fd = 0;
 42     fds[0].events = POLLIN;
 43 
 44     fds[1].fd = sfd;
 45     fds[1].events = POLLIN;
 46 
 47     int count = 2;//顺序表的总数
 48 
 49 
 50     char rbuf[128] = "";
 51     struct sockaddr_in cins[1024];
 52 
 53     while (1)
 54     {
 55 
 56         ret = poll(fds,count,-1);
 57 
 58         //printf("aaaaaaaa\n");
 59         if (ret == 0)
 60         {
 61             printf("time out\n");
 62             return -1;
 63         }
 64         else if(ret < 0)
 65         {
 66             PERROR_INFO(-1);
 67         }
 68         for (int index = 0;index <count ;index++ )
 69         {
 70 
 71             if (fds[index].revents != POLLIN)
 72             {
 73                 continue;
 74             }
 75 
 76 
 77             if (fds[index].fd  == sfd)
 78             {
 79                 newfd = accept(sfd,(struct sockaddr *)&cin,&socklen);
 80                 PERROR_INFO(newfd);
 81 
 82                 printf("[%s:%d] :is connect\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
 83                 count++;
 84                 fds[count-1].fd = newfd;
 85                 fds[count-1].events = POLLIN;
 86                 cins[newfd] = cin;
 87 
 88             }
 89             else if (fds[index].fd == 0)
 90             {
 91 
 92                 scanf("%s",buf);
 93                 printf("get message is %s\n",buf);
 94 
 95                 for (int i = 2;i <count ;i++ )
 96                 {
 97                     send(fds[i].fd,buf,sizeof(buf),0);
 98                 }
 99 
100             }
101             else
102             {
103                 bzero(rbuf,sizeof(rbuf));
104                 ret = recv(fds[index].fd,rbuf,sizeof(rbuf),0);
105 
106                 if (ret == 0)
107                 {
108                     printf("client is offline!!!\n");
109                     close(fds[index].fd);
110                     for (int i = index;i < count-2 ;i++ )
111                     {
112                         fds[i] = fds[i+1];
113                     }
114                     count--;
115                     index--;
116                     continue;
117                 }
118                 printf("[%s:%d]  :recive message is %s\n",inet_ntoa(cins[fds[index].fd].sin_addr),ntohs(cins[fds[index].fd].sin_port),rbuf);
119                 //printf("recive message is %s\n",rbuf);
120                 strcat(rbuf,"*_*");
121                 send(fds[index].fd,rbuf,sizeof(rbuf),0);
122 
123             }                                                                                                                                                                                                                                                                                                                                                                                                                                                          
124 
125         }
126     }
127     close(sfd);
128 
129     return 0;
130 }
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
~                        

思维导图

文章来源:https://blog.csdn.net/weixin_51788918/article/details/135115709
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。