首页 > 代码库 > libevent源码分析-TCP服务端代码

libevent源码分析-TCP服务端代码

先贴一段代码再说,Linux下使用g++ -g-o server server.c -levent

可以直接使用gdb调试,而且可以跟踪到libevent的库里。

技术分享
  1 #include <stdio.h>    
  2 #include <string.h>    
  3 #include <iostream>    
  4 #include <sys/socket.h>        
  5 #include <netinet/in.h>        
  6 #include <arpa/inet.h>        
  7 #include <netdb.h>    
  8     
  9 #include <event.h>    
 10 using namespace std;    
 11     
 12 // 事件base    
 13 struct event_base* base;    
 14     
 15 // 读事件回调函数    
 16 void onRead(int iCliFd, short iEvent, void *arg)    
 17 {    
 18     int iLen;    
 19     char buf[1500];    
 20     
 21     iLen = recv(iCliFd, buf, 1500, 0);    
 22     
 23     if (iLen <= 0) {    
 24         cout << "Client Close" << endl;    
 25     
 26         // 连接结束(=0)或连接错误(<0),将事件删除并释放内存空间    
 27         struct event *pEvRead = (struct event*)arg;    
 28         event_del(pEvRead);    
 29         delete pEvRead;    
 30     
 31         close(iCliFd);    
 32         return;    
 33     }    
 34     
 35     buf[iLen] = 0;    
 36     cout << "Client Info:" << buf << endl;    
 37   
 38   
 39   
 40     struct bufferevent* buf_ev;  
 41     buf_ev = bufferevent_new(iCliFd, NULL, NULL, NULL, NULL);  
 42     buf_ev->wm_read.high = 4096;  
 43     char MESSAGE[]="welcome to server..";  
 44     bufferevent_write(buf_ev, MESSAGE, strlen(MESSAGE));  
 45 }    
 46     
 47 // 连接请求事件回调函数    
 48 void onAccept(int iSvrFd, short iEvent, void *arg)    
 49 {    
 50     int iCliFd;    
 51     struct sockaddr_in sCliAddr;    
 52     
 53     socklen_t iSinSize = sizeof(sCliAddr);    
 54     iCliFd = accept(iSvrFd, (struct sockaddr*)&sCliAddr, &iSinSize);    
 55     
 56     // 连接注册为新事件 (EV_PERSIST为事件触发后不默认删除)    
 57     struct event *pEvRead = new event;    
 58     event_set(pEvRead, iCliFd, EV_READ|EV_PERSIST, onRead, pEvRead);    
 59     event_base_set(base, pEvRead);    
 60     event_add(pEvRead, NULL);   
 61   
 62   
 63     struct bufferevent* buf_ev;    
 64      buf_ev = bufferevent_new(iCliFd, NULL, NULL, NULL, NULL);      
 65      buf_ev->wm_read.high = 4096;  
 66      char MESSAGE[]="welcome to server..";  
 67     bufferevent_write(buf_ev, MESSAGE, strlen(MESSAGE));   
 68   
 69     cout<<"a client connect:"<<iCliFd<<endl;  
 70 }    
 71     
 72 int main()    
 73 {    
 74     
 75     int iSvrFd;      
 76     struct sockaddr_in sSvrAddr;    
 77                   
 78     memset(&sSvrAddr, 0, sizeof(sSvrAddr));      
 79     sSvrAddr.sin_family = AF_INET;      
 80     sSvrAddr.sin_addr.s_addr = inet_addr("127.0.0.1");        
 81     sSvrAddr.sin_port = htons(8888);       
 82                                 
 83     // 创建tcpSocket(iSvrFd),监听本机8888端口      
 84     iSvrFd = socket(AF_INET, SOCK_STREAM, 0);      
 85     bind(iSvrFd, (struct sockaddr*)&sSvrAddr, sizeof(sSvrAddr));      
 86     listen(iSvrFd, 10);    
 87     
 88     // 初始化base    
 89     base = (struct event_base*)event_init();    
 90         
 91     struct event evListen;    
 92     // 设置事件    
 93     event_set(&evListen, iSvrFd, EV_READ|EV_PERSIST, onAccept, NULL);    
 94     // 设置为base事件    
 95     event_base_set(base, &evListen);    
 96     // 添加事件    
 97     event_add(&evListen, NULL);    
 98         
 99     // 事件循环    
100     event_base_dispatch(base);    
101     
102     return 0;    
103 }    
View Code

 

libevent源码分析-TCP服务端代码