day38 1220

作业1:select客户端

  1 #include 
  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 
  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 

思维导图

你可能感兴趣的:(算法,数据结构)