C语言 UDP服务器通过多路IO复用同时处理标准输入和套接字。当输入为quit时程序结束;当通过套接字收到对方消息时回射给对方

select();函数:

select函数原型:

int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeval);

void FD_CLR(int fd, fd_set *set);

int FD_ISSET(int fd, fd_set *set);

void FD_SET(int fd, fd_set *set);

void FD_ZERO(fd_set *set);

服务器程序:

#define SIZE 32

int main()

{

      int sockfd=socket(AF_INET,SOCK_DGRAM,0);

      if(sockfd==-1)

      {

           perror("socket");

           return -1;

      }

      struct sockaddr_in saddr;

      bzero(&saddr,sizeof(saddr));

      saddr.sin_family=AF_INET;

      saddr.sin_port=htons(7777);

      saddr.sin_addr.s_addr=inet_addr("192.168.9.253");

      fd_set rfds,fds;

      FD_ZERO(&rfds);

      FD_SET(0,&rfds);

      FD_SET(sockfd,&rfds);

      int nfds=sockfd+1;

     

      int i=0;

     

      int ret=bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));

      if(ret==-1)

      {

           perror("bind");

           close(sockfd);

           return -1;

      }

     

      char buf[SIZE]={0};

      while(1)

      {

           fds=rfds;

           select(nfds,&fds,NULL,NULL,NULL);

           for(i=0;i<nfds;i++)

           {

                 if(FD_ISSET(i,&fds))

                 {

                      if(i==0)

                      {

                            bzero(&buf,sizeof(buf));

                            fgets(buf,sizeof(buf)-1,stdin);

                            buf[strlen(buf)-1]='\0';

                      }else if(i==sockfd)

                      {

                            struct sockaddr dest;

                            socklen_t len=sizeof(dest);

                            ret=recvfrom(i,buf,sizeof(buf),0,(struct sockaddr*)&dest,&len);

                            if(ret==-1)

                            {

                                  perror("recvfrom");

                                  close(sockfd);

                                  return -1;

                            }

                            sendto(i,&buf,sizeof(buf),0,(struct sockaddr*)&dest,len);

                            printf("buf=%s\n",buf);

                            FD_SET(i,&rfds);

                            nfds=nfds>i+1? nfds:i+1;

                      }

                      }

                 }

           }

      close(i);

      return 0;

}

客户端程序:

#define SIZE 32

int main()

{

       int sockfd=socket(AF_INET,SOCK_DGRAM,0);

       if(sockfd==-1)

       {

              perror("socket");

              return -1;

       }

       char buf[SIZE]={0};

       struct sockaddr_in caddr;

       caddr.sin_family=AF_INET;

       caddr.sin_port=htons(7777);

       caddr.sin_addr.s_addr=inet_addr("192.168.9.253");

      

       fd_set rfds,fds;

       FD_ZERO(&rfds);

       FD_SET(0,&rfds);

       FD_SET(sockfd,&rfds);

      

       do{

              fds=rfds;

              select(4,&fds,NULL,NULL,NULL);

              if(FD_ISSET(0,&fds))

              {

                     bzero(&buf,sizeof(buf));

                     fgets(buf,sizeof(buf),stdin);

                     printf("-------\n");

                     int ret=sendto(sockfd,&buf,sizeof(buf),0,(struct sockaddr*)&caddr,sizeof(caddr));

                     if(ret==-1)

                     {

                            perror("sendto");

                            close(sockfd);

                            return -1;

                     }

                     printf("------->>>>>>>>>>s\n");

                     //write(sockfd,buf,sizeof(buf));

              }

              else if(FD_ISSET(sockfd,&fds))

              {

                     bzero(&buf,sizeof(buf));

                     recvfrom(sockfd,&buf,sizeof(buf),0,NULL,NULL);

                     printf("buf=%s\n",buf);

                    

              }

       }while(0!=strncmp(buf,"quit",4));

      

      

       close(sockfd);

       return 0;

}

上一篇:从入门到入土:基于C语言采用UDP协议实现通信功能的程序


下一篇:AspNetCore linux下运行