Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268772
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: 网络与安全

2016-07-28 17:06:19


  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<stdlib.h>
  4. #include<sys/socket.h>
  5. #include<sys/select.h>
  6. #include<netinet/in.h>
  7. #include<arpa/inet.h>
  8. #include<unistd.h>
  9. #include<string.h>

  10. #define _BACKLOG_ 5 //监听队列里允许等待的最大值

  11. int fds[20];//用来存放需要处理的IO事件

  12. int creat_sock(char *ip,char *port)
  13. {
  14.     int sock = socket(AF_INET,SOCK_STREAM,0);
  15.     if(sock < 0){
  16.         perror("creat_sock error");
  17.         exit(1);
  18.     }

  19.     struct sockaddr_in local;
  20.     local.sin_family = AF_INET;
  21.     local.sin_port = htons(atoi(port));
  22.     local.sin_addr.s_addr = inet_addr(ip);
  23.     
  24.     if( bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0){
  25.         perror("bind");
  26.         exit(2);
  27.      }

  28.     if(listen(sock,_BACKLOG_) < 0 ){
  29.         perror("listen");
  30.         exit(4);
  31.      }

  32.     return sock;
  33. }

  34. int main(int argc,char* argv[])
  35. {
  36.     if(argc != 3){
  37.         printf("Please use : %s [ip] [port]\n",argv[0]);
  38.         exit(3);
  39.     }
  40.     int listen_sock = creat_sock(argv[1],argv[2]);

  41.     size_t fds_num = sizeof(fds)/sizeof(fds[0]);
  42.     size_t i = 0;
  43.     for(;i < fds_num;++i)
  44.     {
  45.         fds[i] = -1;
  46.     }
  47.     
  48.     int max_fd = listen_sock;
  49.     fd_set rset;
  50.     
  51.     while(1){
  52.         FD_ZERO(&rset);
  53.         FD_SET(listen_sock,&rset);
  54.         struct timeval timeout = {10 , 0};

  55.         size_t i = 0;
  56.         for(;i < fds_num;++i)
  57.         {
  58.             if(fds[i] > 0 ){
  59.                 FD_SET(fds[i] ,&rset);
  60.                 if(max_fd < fds[i]){
  61.                     max_fd = fds[i];
  62.                 }
  63.             }
  64.         }

  65.         switch(select(max_fd+1,&rset,NULL,NULL,&timeout))
  66.         {
  67.             case -1:
  68.                 perror("select");
  69.                 break;
  70.             case 0:
  71.                 printf("time out..\n");
  72.             default:
  73.             {
  74.                 size_t i = 0;
  75.                 for(;i < fds_num;++i)
  76.                 {
  77.                  //当为listen_socket事件就绪的时候,就表明有新的连接请求
  78.                     if(FD_ISSET(fds[i],&rset) && fds[i] == listen_sock)
  79.                     {
  80.                         struct sockaddr_in client;
  81.                         int accept_sock = accept(listen_sock,(struct sockaddr*)&client,sizeof(client));
  82.                         if(accept_sock < 0){
  83.                             perror("accept");
  84.                             exit(5);
  85.                         }

  86.                         printf("connect by a client, ip:%s port:%d\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));

  87.                         size_t i = 0;
  88.                         for(;i < fds_num;++i)//将新接受的描述符存入集合中
  89.                         {
  90.                             if(fds[i] == -1){
  91.                                 fds[i] = accept_sock;
  92.                                 break;
  93.                             }
  94.                         }
  95.                         if(i == fds_num)
  96.                         {
  97.                             close(accept_sock);
  98.                         }
  99.                     }
  100.                     //普通请求
  101.                     else if(FD_ISSET(fds[i],&rset) && (fds[i] > 0))
  102.                     {
  103.                         char buf[1024];
  104.                         memset(buf,'\0',sizeof(buf));
  105.                         ssize_t size = read(fds[i],buf,sizeof(buf)-1);
  106.                         if(size < 0){
  107.                             perror("read");
  108.                             exit(6);
  109.                         }else if(size == 0){
  110.                             printf("client close..\n");
  111.                             close(fds[i]);
  112.                             fds[i] = -1;
  113.                         }else{
  114.                             printf("client say: %s\n",buf);
  115.                         }
  116.                         
  117.                     }
  118.                     else{}
  119.                 }
  120.                 
  121.             }
  122.             break;
  123.         }
  124.     }
  125.     return 0;
  126. }

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