Chinaunix首页 | 论坛 | 博客
  • 博客访问: 275660
  • 博文数量: 113
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1044
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-15 16:09
文章分类

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: C/C++

2015-09-17 23:29:44


点击(此处)折叠或打开

  1. 直接上源码,保存为server.c即可.[喝小酒的网摘]http://blog.hehehehehe.cn/a/17219.htm

  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <arpa/inet.h>
  8. #include <netinet/in.h>

  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <sys/wait.h>
  12. #include <sys/time.h>
  13. #include <sys/types.h>

  14. #define MAXBUF 1024

  15. typedef struct CLIENT {
  16.     int fd;
  17.     struct sockaddr_in addr;    
  18. }CLIENT;

  19. /***************************
  20. **server for multi-client
  21. **PF_SETSIZE=1024
  22. ****************************/
  23. int main(int argc, char** argv)
  24. {
  25.     int i,n,maxi = -1;
  26.     int nready;
  27.     int slisten,sockfd,maxfd=-1,connectfd;
  28.     
  29.     unsigned int myport,lisnum;

  30.     struct sockaddr_in my_addr,addr;
  31.     struct timeval tv;
  32.     
  33.     socklen_t len;
  34.     fd_set rset,allset;    
  35.     
  36.     char buf[MAXBUF + 1];
  37.     CLIENT client[FD_SETSIZE];

  38.     if(argv[1])
  39.         myport = atoi(argv[1]);
  40.     else
  41.         myport = 1234;

  42.     if(argv[2])
  43.         lisnum = atoi(argv[2]);
  44.     else
  45.         lisnum = FD_SETSIZE;

  46.     if((slisten = socket(AF_INET,SOCK_STREAM,0)) == -1)
  47.     {
  48.         perror("socket");
  49.         exit(1);
  50.     }

  51.     bzero(&my_addr,sizeof(my_addr));
  52.     my_addr.sin_family = AF_INET;
  53.     my_addr.sin_port = htons(myport);
  54.     my_addr.sin_addr.s_addr = INADDR_ANY;

  55.     if(bind(slisten, (struct sockaddr *)&my_addr, sizeof(my_addr)) == -1) {
  56.             perror("bind");
  57.             exit(1);
  58.         }

  59.     if (listen(slisten, lisnum) == -1) {
  60.             perror("listen");
  61.             exit(1);
  62.         }

  63.     for(i=0;i<FD_SETSIZE;i++)
  64.     {
  65.         client[i].fd = -1;
  66.     }

  67.     FD_ZERO(&allset);
  68.         FD_SET(slisten, &allset);
  69.     maxfd = slisten;
  70.     
  71.     printf("Waiting for connections and data...
  72. ");
  73.     while (1)
  74.     {        
  75.         rset = allset;     

  76.         tv.tv_sec = 1;
  77.                tv.tv_usec = 0;
  78.           
  79.                nready = select(maxfd + 1, &rset, NULL, NULL, &tv);

  80.         if(nready == 0)
  81.             continue;
  82.         else if(nready < 0)
  83.         {
  84.             printf("select failed!
  85. ");
  86.             break;
  87.         }    
  88.         else
  89.         {
  90.             if(FD_ISSET(slisten,&rset)) // new connection
  91.             {            
  92.                 len = sizeof(struct sockaddr);
  93.                 if((connectfd = accept(slisten,
  94.                     (struct sockaddr*)&addr,&len)) == -1)
  95.                 {
  96.                     perror("accept() error
  97. ");
  98.                     continue;
  99.                 }
  100.                 for(i=0;i<FD_SETSIZE;i++)
  101.                 {
  102.                     if(client[i].fd < 0)
  103.                     {
  104.                         client[i].fd = connectfd;
  105.                         client[i].addr = addr;                 
  106.                         printf("Yout got a connection from %s.
  107. ",
  108.                         inet_ntoa(client[i].addr.sin_addr));
  109.                         break;
  110.                     }
  111.                 }
  112.                 if(i == FD_SETSIZE)            
  113.                     printf("too many connections");            
  114.                 FD_SET(connectfd,&allset);
  115.                 if(connectfd > maxfd)
  116.                     maxfd = connectfd;
  117.                 if(i > maxi)
  118.                     maxi = i;
  119.             }
  120.             else
  121.             {            
  122.                 for(i=0;i<=maxi;i++)
  123.                 {            
  124.                     if((sockfd = client[i].fd)<0)
  125.                         continue;                
  126.                     if(FD_ISSET(sockfd,&rset))
  127.                     {                        
  128.                         bzero(buf,MAXBUF + 1);
  129.                         if((n = recv(sockfd,buf,MAXBUF,0)) > 0)
  130.                         {
  131.     printf("received data:%s
  132.  from %s
  133. ",buf,inet_ntoa(client[i].addr.sin_addr));    
  134.                         }                
  135.                         else
  136.                         {
  137.                          printf("disconnected by client!
  138. ");
  139.                             close(sockfd);
  140.                             FD_CLR(sockfd,&allset);
  141.                             client[i].fd = -1;
  142.                         }
  143.                     }
  144.                 }
  145.             }
  146.         }    
  147.     }
  148.     close(slisten);
  149. }

阅读(1189) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~