epoll

 

一、epoll server端

  1 #include <iostream>
  2 #include <sys/socket.h>
  3 #include <sys/epoll.h>
  4 #include <netinet/in.h>
  5 #include <arpa/inet.h>
  6 #include <fcntl.h>
  7 #include <unistd.h>
  8 #include <stdio.h>
  9 #include <string.h>
 10 #include <errno.h>
 11 #include <strings.h>
 12 
 13 using namespace std;
 14 
 15 const int EPOLLSIZE = 256;
 16 const int PORTNUMBER = 2555;
 17 const int LISTENNUMBER = 20;
 18 const int EVENTSIZE = 20;
 19 const int EPOLLWAITTIMEOUT = 500;
 20 const int BUFFERSIZE = 200;
 21 
 22 int main(int argc, char * argv[])
 23 {
 24     int iRet;
 25     char acBuffer[BUFFERSIZE] = {0};
 26 
 27     int iEpFd = epoll_create(EPOLLSIZE);
 28     int iListenFd = socket(AF_INET, SOCK_STREAM, 0);
 29     struct epoll_event stEv;
 30     stEv.data.fd = iListenFd;
 31     stEv.events = EPOLLIN|EPOLLET;
 32     epoll_ctl(iEpFd, EPOLL_CTL_ADD, iListenFd, &stEv);
 33 
 34     struct sockaddr_in stServeraddr;
 35     bzero(&stServeraddr, sizeof(stServeraddr));
 36     stServeraddr.sin_family = AF_INET;
 37     const char * pcLocalIp = "127.0.0.1";
 38     inet_aton(pcLocalIp, &(stServeraddr.sin_addr));
 39     //stServeraddr.sin_addr.s_addr = htonl(INADDR_ANY);
 40     stServeraddr.sin_port = htons(PORTNUMBER);
 41 
 42     bind(iListenFd, (sockaddr*)&stServeraddr, sizeof(stServeraddr));
 43     listen(iListenFd, LISTENNUMBER);
 44     
 45     struct epoll_event astEvents[EVENTSIZE];
 46     int iNfds;
 47     while (true) 
 48     {
 49         iNfds = epoll_wait(iEpFd, astEvents, EVENTSIZE, EPOLLWAITTIMEOUT);
 50         for (int i=0; i<iNfds; i++)
 51         {
 52             if(astEvents[i].data.fd == iListenFd)
 53             {
 54                 struct sockaddr_in stClientaddr;
 55                 socklen_t iClientLen = sizeof(struct sockaddr);
 56                 int iConnFd = accept(iListenFd, (struct sockaddr*)&stClientaddr, &iClientLen);
 57                 if(iConnFd < 0)
 58                 {
 59                     cout<<"errno : "<<strerror(errno)<<endl;
 60                     cout<<"accept error. iConnFd = "<<iConnFd<<endl;
 61                 }
 62                 stEv.data.fd = iConnFd;
 63                 stEv.events = EPOLLIN|EPOLLET;
 64                 iRet = epoll_ctl(iEpFd, EPOLL_CTL_ADD, iConnFd, &stEv);
 65                 if(iRet != 0)
 66                 {
 67                     cout<<"errno : "<<strerror(errno)<<endl;
 68                     cout<<"new socket epoll_ctl iRet = "<<iRet<<endl;
 69                 }
 70             }
 71             else if(astEvents[i].events & EPOLLIN)
 72             {
 73                 int iReadSockFd = astEvents[i].data.fd;
 74                 int iNum = read(iReadSockFd, acBuffer, BUFFERSIZE); 
 75                 if(iNum < 0)
 76                 {
 77                     cout<<"iNum < 0"<<endl;
 78                 }
 79                 else if(iNum == 0)
 80                 {
 81                     cout<<"iNum == 0"<<endl;
 82                 }
 83                 acBuffer[iNum] = '\0';
 84                 cout<<"acBuffer : "<<acBuffer<<endl;
 85                 stEv.data.fd = iReadSockFd;
 86                 stEv.events = EPOLLOUT|EPOLLET;
 87                 epoll_ctl(iEpFd, EPOLL_CTL_MOD, iReadSockFd, &stEv);
 88             }
 89             else if(astEvents[i].events & EPOLLOUT)
 90             {
 91                 int iWriteSockFd = astEvents[i].data.fd;
 92                 write(iWriteSockFd, acBuffer, BUFFERSIZE); 
 93 
 94                 stEv.data.fd = iWriteSockFd;
 95                 stEv.events = EPOLLIN|EPOLLET;
 96                 epoll_ctl(iEpFd, EPOLL_CTL_MOD, iWriteSockFd, &stEv);
 97             }
 98         }
 99     }
100     return 0;
101 }

 

 

二、tcp client端

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <errno.h>
 5 #include <sys/types.h>
 6 #include <sys/socket.h>
 7 #include <arpa/inet.h>
 8 #include <netinet/in.h>
 9 const int SERVERPORT = 2555;
10 const char * pcLocalIp = "127.0.0.1";
11 const int BUFFERSIZE = 1024;
12 using namespace std;
13 int main(int argc, char ** argv)
14 {
15     int iRet;
16     int iSockFd = socket(AF_INET, SOCK_STREAM, 0);
17     struct sockaddr_in stServeraddr;
18     memset(&stServeraddr, 0, sizeof(stServeraddr));
19     stServeraddr.sin_family = AF_INET;
20     stServeraddr.sin_port = htons(SERVERPORT);
21     inet_pton(AF_INET, pcLocalIp, &stServeraddr.sin_addr);
22     iRet = connect(iSockFd, (struct sockaddr*)&stServeraddr, sizeof(stServeraddr));
23     if(iRet < 0)
24     {
25         cout<<"connect error"<<endl;
26     }
27     cout<<"send msg to server: "<<endl;
28     char acBuffer[BUFFERSIZE] = {0};
29     fgets(acBuffer, BUFFERSIZE, stdin);
30     iRet = send(iSockFd, acBuffer, strlen(acBuffer), 0);
31     if(iRet < 0)
32     {
33         cout<<"send error"<<endl;
34     }
35     sleep(10);
36     close(iSockFd);
37     return 0;
38 }

 

posted @ 2014-12-02 18:45  MDGSF  阅读(132)  评论(0)    收藏  举报