Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360459
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 1138
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-20 16:18
个人简介

最多140个字

文章分类

全部博文(60)

文章存档

2016年(1)

2015年(34)

2014年(25)

分类: 网络与安全

2015-08-15 10:53:08


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

  12. #define MAX_EVENT_NUM 1024
  13. #define TCP_BUFFER_SIZE 512
  14. #define UDP_BUFFER_SIZE 1024

  15. int setnonblocking(int fd)//设置非阻塞;
  16. {
  17.     int oldopt=fcntl(fd,F_GETFL);
  18.     int newopt=oldopt|O_NONBLOCK;
  19.     fcntl(fd,F_SETFL,newopt);
  20.     return oldopt;
  21. }

  22. void addfd(int epollfd,int fd)
  23. {
  24.     epoll_event eve;
  25.     eve.data.fd=fd;
  26.     eve.events=EPOLLIN|EPOLLET;//ET
  27.     epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&eve);
  28.     setnonblocking(fd);
  29. }

  30. int main(int argc,char *argv[])
  31. {
  32.     const char *ip=argv[1];
  33.     int port=atoi(argv[2]);

  34.     int ret=0;
  35.     struct sockaddr_in address;
  36.     bzero(&address,sizeof(address));
  37.     address.sin_family=AF_INET;
  38.     inet_pton(AF_INET,ip,&address.sin_addr);
  39.     address.sin_port=htons(port);
  40.     int listenfd=socket(PF_INET,SOCK_STREAM,0);

  41.     ret=bind(listenfd,(struct sockaddr*)&address,sizeof(address));
  42.     ret=listen(listenfd,5);

  43.     epoll_event events[MAX_EVENT_NUM];
  44.     int epollfd=epoll_create(5);
  45.     addfd(epollfd,listenfd);

  46.     while(1)
  47.     {
  48.         int num=epoll_wait(epollfd,events,MAX_EVENT_NUM,-1);
  49.         if(num<0)
  50.         {
  51.             printf("epoll falied\n");
  52.             break;
  53.         }

  54.         for(int i=0;i<num;i++)
  55.         {
  56.             int sockfd=events[i].data.fd;
  57.             if(sockfd==listenfd)
  58.             {
  59.                 struct sockaddr_in client_addr;
  60.                 socklen_t client_addrlen=sizeof(client_addr);
  61.                 int connfd=accept(listenfd,(struct sockaddr*)&client_addr,&client_addrlen);
  62.                 addfd(epollfd,connfd);
  63.             }
  64.             else if(events[i].events&EPOLLIN)
  65.             {
  66.                 char buf[TCP_BUFFER_SIZE];
  67.                 while(1)
  68.                 {
  69.                     memset(buf,0,TCP_BUFFER_SIZE);
  70.                     ret=recv(sockfd,buf,TCP_BUFFER_SIZE-1,0);
  71.                     if(ret<0)
  72.                     {
  73.                         if((errno==EAGAIN)||(errno==EWOULDBLOCK))//表示数据已经全部读取完毕,此后epoll就能再次触发该socket上的EPOLLIN事件;
  74.                         {
  75.                             break;
  76.                         }
  77.                         close(sockfd);
  78.                         break;
  79.                     }
  80.                     else if(ret==0)//客户端关闭连接;
  81.                     {
  82.                         close(sockfd);
  83.                     }
  84.                     else
  85.                         send(sockfd,buf,ret,0);
  86.                 }
  87.             }
  88.             else
  89.             {
  90.                 printf("somethins else happen\n");
  91.             }
  92.         }
  93.     }
  94.     close(listenfd);
  95.     return 0;
  96. }

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