基于UDP的IO多路复用一例

 

服务器端:

   1:  #include <stdio.h>
   2:  #include <stdlib.h>
   3:  #include <sys/socket.h>
   4:  #include <netinet/in.h>
   5:  #include <arpa/inet.h>
   6:  #include <string.h>
   7:  #include <sys/time.h>
   8:  #include <sys/types.h>
   9:  #include <unistd.h>
  10:   
  11:  typedef struct sockaddr SA;
  12:  #define N 256
  13:   
  14:  int main(int argc, char *argv[])
  15:  {
  16:      int sockfd;
  17:      char buffer[N];    
  18:      char buffer2[N];    
  19:      struct sockaddr_in myaddr, peeraddr;
  20:      socklen_t peerlen = sizeof(peeraddr);
  21:      int maxfd;
  22:      int i;
  23:   
  24:      fd_set rdfs;
  25:   
  26:      if((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  27:      {
  28:          perror("fail to socket");
  29:          exit(-1);
  30:      }
  31:   
  32:      bzero(&myaddr, sizeof(myaddr));
  33:      myaddr.sin_family = PF_INET;
  34:      myaddr.sin_port = htons(atoi(argv[2]));
  35:      myaddr.sin_addr.s_addr = inet_addr(argv[1]); 
  36:   
  37:      if(bind(sockfd, (SA *)&myaddr, sizeof(myaddr)) < 0)
  38:      {
  39:          perror("fail to bind");
  40:          exit(-1);
  41:      }
  42:   
  43:      bzero(&peeraddr, peerlen);
  44:      maxfd = sockfd;
  45:   
  46:   
  47:      while(1)
  48:      {
  49:          FD_ZERO(&rdfs);
  50:          FD_SET(0, &rdfs);
  51:          FD_SET(sockfd, &rdfs);
  52:   
  53:          select(maxfd+1, &rdfs, NULL, NULL, NULL);
  54:   
  55:          for(i=0; i<=maxfd; i++)
  56:          {
  57:              if(FD_ISSET(i, &rdfs) == 1)
  58:              {
  59:                  if(i == 0)
  60:                  {
  61:                      fgets(buffer2, N, stdin);
  62:                      printf("you input %s", buffer2);
  63:                  }
  64:                  if(i == sockfd)
  65:                  {
  66:                      recvfrom(sockfd, buffer, N, 0, (SA *)&peeraddr, &peerlen);
  67:                      sendto(sockfd, buffer, N, 0, (SA *)&peeraddr, peerlen);
  68:                  }
  69:              }
  70:          }
  71:   
  72:      }
  73:   
  74:      return 0;
  75:  }

客户端:

   1:  #include <unistd.h>
   2:  #include <stdio.h>
   3:  #include <stdlib.h>
   4:  #include <sys/socket.h>
   5:  #include <netinet/in.h>
   6:  #include <arpa/inet.h>
   7:  #include <string.h>
   8:   
   9:  typedef struct sockaddr SA;
  10:  #define N 256
  11:   
  12:  int main(int argc, char *argv[])
  13:  {
  14:      int sockfd;
  15:      char buffer[N];    
  16:      struct sockaddr_in serveraddr;
  17:   
  18:      if((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  19:      {
  20:          perror("fail to socket");
  21:          exit(-1);
  22:      }
  23:   
  24:      bzero(&serveraddr, sizeof(serveraddr));
  25:      serveraddr.sin_family = PF_INET;
  26:      serveraddr.sin_port = htons(atoi(argv[2]));
  27:      serveraddr.sin_addr.s_addr = inet_addr(argv[1]); 
  28:   
  29:      while(1)
  30:      {
  31:          fgets(buffer, N, stdin);
  32:          if(strncmp(buffer, "quit", 4) == 0)
  33:          {
  34:              break;
  35:          }
  36:          sendto(sockfd, buffer, N, 0, (SA *)&serveraddr, sizeof(serveraddr));
  37:          bzero(buffer, N);
  38:          recvfrom(sockfd, buffer, N, 0, NULL, NULL);
  39:          printf("receive from server > %s",buffer); 
  40:      }
  41:      close(sockfd);
  42:      return 0;
  43:  }

运行:

image

posted @ 2013-03-15 20:51  摩斯电码  阅读(1069)  评论(0编辑  收藏  举报