Windows下實(shí)現(xiàn)簡單的libevent服務(wù)器
最近再學(xué)習(xí)Libevent由于自己使用的是windows系統(tǒng),遺憾的是有關(guān)在vs下可以參考的程序少之又少。在參考了許多的博客文章后。自己摸索寫了一個(gè)簡單的Libevent Server程序。并且在網(wǎng)上找了一個(gè)簡單的客戶端程序,測試該代碼成功。今天在此做一個(gè)記錄。
Libevent的確是一個(gè)非常好用的東西,還在繼續(xù)學(xué)習(xí)中,后續(xù)還要在windows下實(shí)現(xiàn)Libevent的多線程使用。今天先把自己搞出來的東西貼上來,僅供學(xué)習(xí)參考。在vs2015上編譯通過。
默認(rèn)情況下是單線程的(可以配置成多線程,如果有需要的話),每個(gè)線程有且只有一event base,對應(yīng)一個(gè)struct event_base結(jié)構(gòu)體(以及附于其上的事件管理器),用來schedule托管給它的一系列event,可以和操作系統(tǒng)的進(jìn)程管理類比,當(dāng)然,要更簡單一點(diǎn)。當(dāng)一個(gè)事件發(fā)生后,event_base會(huì)在合適的時(shí)間(不一定是立即)去調(diào)用綁定在這個(gè)事件上的函數(shù)(傳入一些預(yù)定義的參數(shù),以及在綁定時(shí)指定的一個(gè)參數(shù)),直到這個(gè)函數(shù)執(zhí)行完,再返回schedule其他事件。
//創(chuàng)建一個(gè)event_base struct event_base *base = event_base_new(); assert(base != NULL);
event_base內(nèi)部有一個(gè)循環(huán),循環(huán)阻塞在epoll / kqueue等系統(tǒng)調(diào)用上,直到有一個(gè) / 一些事件發(fā)生,然后去處理這些事件。當(dāng)然,這些事件要被綁定在這個(gè)event_base上。每個(gè)事件對應(yīng)一個(gè)struct event,可以是監(jiān)聽一個(gè)fd或者POSIX信號量之類(這里只講fd了,其他的看manual吧)。struct event使用event_new來創(chuàng)建和綁定,使用event_add來啟用:
//創(chuàng)建并綁定一個(gè)event struct event *listen_event; //參數(shù):event_base, 監(jiān)聽的fd,事件類型及屬性,綁定的回調(diào)函數(shù),給回調(diào)函數(shù)的參數(shù) listen_event = event_new(base, listener, EV_READ | EV_PERSIST, callback_func, (void*)base); //參數(shù):event,超時(shí)時(shí)間(struct timeval *類型的,NULL表示無超時(shí)設(shè)置) event_add(listen_event, NULL);
注:libevent支持的事件及屬性包括(使用bitfield實(shí)現(xiàn),所以要用 | 來讓它們合體)
(a) EV_TIMEOUT: 超時(shí)
(b) EV_READ : 只要網(wǎng)絡(luò)緩沖中還有數(shù)據(jù),回調(diào)函數(shù)就會(huì)被觸發(fā)
(c) EV_WRITE : 只要塞給網(wǎng)絡(luò)緩沖的數(shù)據(jù)被寫完,回調(diào)函數(shù)就會(huì)被觸發(fā)
(d) EV_SIGNAL : POSIX信號量,參考manual吧
(e) EV_PERSIST : 不指定這個(gè)屬性的話,回調(diào)函數(shù)被觸發(fā)后事件會(huì)被刪除
(f) EV_ET : Edge - Trigger邊緣觸發(fā),參考EPOLL_ET
然后需要啟動(dòng)event_base的循環(huán),這樣才能開始處理發(fā)生的事件。循環(huán)的啟動(dòng)event base dispatch,循環(huán)將一直持續(xù),直到不再有需要關(guān)注的事件,或者是遇到event_loopbreak() / event_loopexit()函數(shù)。
//啟動(dòng)事件循環(huán)
event_base_dispatch(base);
接下來關(guān)注下綁定到event的回調(diào)函數(shù)callback_func:傳遞給它的是一個(gè)socket fd、一個(gè)event類型及屬性bit_field、以及傳遞給event_new的最后一個(gè)參數(shù)(去上面幾行回顧一下,把event_base給傳進(jìn)來了,實(shí)際上更多地是分配一個(gè)結(jié)構(gòu)體,把相關(guān)的數(shù)據(jù)都撂進(jìn)去,然后丟給event_new,在這里就能取得到了)。其原型是:
typedef void(*event_callback_fn)(evutil_socket_t sockfd, short event_type, void *arg)
對于一個(gè)服務(wù)器而言,上面的流程大概是這樣組合的:
1. listener = socket(),bind(),listen(),設(shè)置nonblocking(POSIX系統(tǒng)中可使用fcntl設(shè)置,windows不需要設(shè)置,
實(shí)際上libevent提供了統(tǒng)一的包裝evutil_make_socket_nonblocking)
2. 創(chuàng)建一個(gè)event_base
3. 創(chuàng)建一個(gè)event,將該socket托管給event_base,指定要監(jiān)聽的事件類型,并綁定上相應(yīng)的回調(diào)函數(shù)(及需要給它的參數(shù))
。對于listener socket來說,只需要監(jiān)聽EV_READ | EV_PERSIST
4. 啟用該事件
5. 進(jìn)入事件循環(huán)
-------------- -
6. (異步)當(dāng)有client發(fā)起請求的時(shí)候,調(diào)用該回調(diào)函數(shù),進(jìn)行處理。
/*接下來關(guān)注下綁定到event的回調(diào)函數(shù)callback_func:傳遞給它的是一個(gè)socket fd、一個(gè)event類型及屬性bit_field、以及傳遞給event_new的最后一個(gè)參數(shù)(去上面幾行回顧一下,把event_base給傳進(jìn)來了,實(shí)際上更多地是分配一個(gè)結(jié)構(gòu)體,把相關(guān)的數(shù)據(jù)都撂進(jìn)去,然后丟給event_new,在這里就能取得到了)。*/
服務(wù)器端代碼:Server.cpp
#include <WinSock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include<iostream>
#include<cassert>
#pragma comment (lib,"ws2_32.lib")
#include<ws2tcpip.h>
#define LISTEN_PORT 9999
#define LIATEN_BACKLOG 32
using namespace std;
/*********************************************************************************
* 函數(shù)聲明
**********************************************************************************/
//accept回掉函數(shù)
void do_accept_cb(evutil_socket_t listener, short event, void *arg);
//read 回調(diào)函數(shù)
void read_cb(struct bufferevent *bev, void *arg);
//error回調(diào)函數(shù)
void error_cb(struct bufferevent *bev, short event, void *arg);
//write 回調(diào)函數(shù)
void write_cb(struct bufferevent *bev, void *arg);
/*********************************************************************************
* 函數(shù)體
**********************************************************************************/
//accept回掉函數(shù)
void do_accept_cb(evutil_socket_t listener, short event, void *arg)
{
//傳入的event_base指針
struct event_base *base = (struct event_base*)arg;
//socket描述符
evutil_socket_t fd;
//聲明地址
struct sockaddr_in sin;
//地址長度聲明
socklen_t slen = sizeof(sin);
//接收客戶端
fd = accept(listener, (struct sockaddr *)&sin, &slen);
if (fd < 0)
{
perror("error accept");
return;
}
printf("ACCEPT: fd = %u\n", fd);
////注冊一個(gè)bufferevent_socket_new事件
struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
////設(shè)置回掉函數(shù)
bufferevent_setcb(bev, read_cb, NULL, error_cb, arg);
////設(shè)置該事件的屬性
bufferevent_enable(bev, EV_READ | EV_WRITE | EV_PERSIST);
}
////read 回調(diào)函數(shù)
void read_cb(struct bufferevent *bev, void *arg)
{
#define MAX_LINE 256
char line[MAX_LINE + 1];
int n;
//通過傳入?yún)?shù)bev找到socket fd
evutil_socket_t fd = bufferevent_getfd(bev);
//
while (n = bufferevent_read(bev, line, MAX_LINE))
{
line[n] = '\0';
printf("fd=%u, read line: %s\n", fd, line);
//將獲取的數(shù)據(jù)返回給客戶端
bufferevent_write(bev, line, n);
}
}
////error回調(diào)函數(shù)
void error_cb(struct bufferevent *bev, short event, void *arg)
{
//通過傳入?yún)?shù)bev找到socket fd
evutil_socket_t fd = bufferevent_getfd(bev);
//cout << "fd = " << fd << endl;
if (event & BEV_EVENT_TIMEOUT)
{
printf("Timed out\n"); //if bufferevent_set_timeouts() called
}
else if (event & BEV_EVENT_EOF)
{
printf("connection closed\n");
}
else if (event & BEV_EVENT_ERROR)
{
printf("some other error\n");
}
bufferevent_free(bev);
}
////write 回調(diào)函數(shù)
void write_cb(struct bufferevent *bev, void *arg)
{
char str[50];
//通過傳入?yún)?shù)bev找到socket fd
evutil_socket_t fd = bufferevent_getfd(bev);
//cin >> str;
printf("輸入數(shù)據(jù)!");
scanf_s("%d", &str);
bufferevent_write(bev, &str, sizeof(str));
}
int main()
{
int ret;
evutil_socket_t listener;
WSADATA Ws;
//Init Windows Socket
if (WSAStartup(MAKEWORD(2, 2), &Ws) != 0)
{
return -1;
}
listener = socket(AF_INET, SOCK_STREAM, 0);
assert(listener > 0);
evutil_make_listen_socket_reuseable(listener);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(LISTEN_PORT);
if (bind(listener, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("bind");
return 1;
}
if (listen(listener, 1000) < 0) {
perror("listen");
return 1;
}
printf("Listening...\n");
evutil_make_socket_nonblocking(listener);
struct event_base *base = event_base_new();
assert(base != NULL);
struct event *listen_event;
listen_event = event_new(base, listener, EV_READ | EV_PERSIST, do_accept_cb, (void*)base);
event_add(listen_event, NULL);
event_base_dispatch(base);
printf("The End.");
return 0;
}
客戶端代碼:Client.cpp
/******* 客戶端程序 client.c ************/
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include<winsock2.h>
#include<ws2tcpip.h>
#include<iostream>
#pragma comment (lib,"ws2_32.lib")
int main(int argc, char *argv[])
{
WSADATA Ws;
//Init Windows Socket
if (WSAStartup(MAKEWORD(2, 2), &Ws) != 0)
{
return 0;
}
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber, nbytes;
if ((host = gethostbyname("127.0.0.1")) == NULL)
{
fprintf(stderr, "Gethostname error\n");
exit(1);
}
if ((portnumber = atoi("9999"))<0)
{
fprintf(stderr, "Usage:%s hostname portnumber\a\n", argv[0]);
exit(1);
}
/* 客戶程序開始建立 sockfd描述符 */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "Socket Error:%s\a\n", strerror(errno));
exit(1);
}
/* 客戶程序填充服務(wù)端的資料 */
memset(&server_addr,0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(portnumber);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
/* 客戶程序發(fā)起連接請求 */
if (connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1)
{
fprintf(stderr, "Connect Error:%s\a\n", strerror(errno));
exit(1);
}
while (true)
{
char MESSAGE[] = "hello server..\n";
//bufferevent_write(buf_ev,MESSAGE,strlen(MESSAGE));
//
if (-1 == (::send(sockfd, MESSAGE, strlen(MESSAGE), 0)))
{
printf("the net has a error occured..");
break;
}
if ((nbytes = recv(sockfd, buffer, 1024,0)) == -1)
{
fprintf(stderr, "read error:%s\n", strerror(errno));
exit(1);
}
buffer[nbytes] = '\0';
printf("I have received:%s\n", buffer);
memset(buffer, 0, 1024);
Sleep(2);
}
/* 結(jié)束通訊 */
closesocket(sockfd);
exit(0);
return 0;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Linux下編譯安裝php libevent擴(kuò)展實(shí)例
- libevent庫的使用方法實(shí)例
- libevent庫的使用--定時(shí)器的使用實(shí)例
- PHP 使用pcntl和libevent 實(shí)現(xiàn)Timer功能
- 使用 libevent 和 libev 提高網(wǎng)絡(luò)應(yīng)用性能的方法
- windows下部署免費(fèi)ssl證書(letsencrypt)的方法
- 阿里云服務(wù)器ping不通解決辦法(云服務(wù)器搭建完環(huán)境訪問不了ip解決辦法)
- windows服務(wù)器您試圖從目錄中執(zhí)行CGI、ISAPI 或其他可執(zhí)行程序,但該目錄不允許執(zhí)行程序
- Windows下SVN服務(wù)器搭建方法整理(apache)
- WINDOWS下搭建SVN服務(wù)器端的步驟分享(Subversion)
相關(guān)文章
windows系統(tǒng)搭建WEB服務(wù)器詳細(xì)教程
這篇文章主要為大家詳細(xì)介紹了windows系統(tǒng)搭建WEB服務(wù)器詳細(xì)教程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
HTTP常見的狀態(tài)碼HTTP Status Code
這篇文章主要介紹了HTTP常見的狀態(tài)碼HTTP Status Code2017-01-01
CentOS配置虛擬主機(jī)virtualhost使服務(wù)器支持多網(wǎng)站多域名的方法
這篇文章主要介紹了CentOS配置虛擬主機(jī)virtualhost使服務(wù)器支持多網(wǎng)站多域名的方法,涉及CentOS環(huán)境下Apache服務(wù)器虛擬主機(jī)設(shè)置技巧,需要的朋友可以參考下2016-10-10
Valheim服務(wù)器?Mod修改安裝教程?【ValheimPlus】
這篇文章主要介紹了Valheim服務(wù)器?Mod修改安裝?【ValheimPlus】,本教程只提供Windows版本服務(wù)器的mod安裝過程,Linux的安裝教程過程是一樣的本人懶得寫了,此mod采用c配置,需要的朋友可以參考下2022-12-12
解決IntelliJ IDEA maven庫下載依賴包速度慢的問題
下面小編就為大家分享一篇解決IntelliJ IDEA maven庫下載依賴包速度慢的問題,具有很好的參考價(jià)值。希望對大家有所幫助2017-11-11
網(wǎng)站解決和優(yōu)化Server is too busy的一些方法
有時(shí)候我們在訪問網(wǎng)站的時(shí)候提示Server is too busy,一般情況都是因?yàn)榉?wù)器iis或web服務(wù)器資源不足引起的,一般情況下都有限制2014-04-04
k8s監(jiān)控?cái)?shù)據(jù)組件Pod自動(dòng)化進(jìn)行擴(kuò)縮容HPA
這篇文章主要為大家介紹了監(jiān)控?cái)?shù)據(jù)組件Pod自動(dòng)化進(jìn)行擴(kuò)縮容-HPA的全面講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
使用idea搭建springboot initializer服務(wù)器的問題分析
這篇文章主要介紹了使用idea搭建springboot initializer服務(wù)器的問題分析,本文給大家分享大家過程通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

