libevent网络框架学习
作者:互联网
//client.c
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<event2/event.h>
#include<event2/bufferevent.h>
char buf[1024];
void read_cb(struct bufferevent *bev, void *ctx)
{
memset(buf, 0, sizeof(buf));
bufferevent_read(bev, buf, sizeof(buf));
printf("client read data = %s", buf);
}
void write_cb(struct bufferevent *bev, void *ctx)
{
memset(buf, 0, sizeof(buf));
bufferevent_read(bev, buf, sizeof(buf));
printf("client read data = %s", buf);
}
void event_cb(struct bufferevent* bev, short event, void*ctx)
{
struct event_base* base = (struct event_base*)ctx;
if ( event & BEV_EVENT_CONNECTED )
{
bufferevent_enable(bev, EV_READ | EV_WRITE);
printf("connect sucess BEV_EVENT_CONNECTED\n");
}
if (event & BEV_EVENT_EOF )
{
printf("old client go away BEV_EVENT_EOF\n");
//stop eventloop
event_base_loopbreak(base);
}
if ( event & BEV_EVENT_ERROR )
{
printf("BEV_EVENT_ERROR\n");
}
}
void send_cb(evutil_socket_t fd, short evnet, void * agr)
{
//from stdinfileno read buf
char buf[1024] = {0};
read(fd, buf, sizeof(buf));
//send data
struct bufferevent* bev = (struct bufferevent*)agr;
bufferevent_write(bev, buf, strlen(buf)+1);
}
int main(int agrc, char* agrv)
{
//create event fream instance
struct event_base* base = event_base_new();
//create sock and bufferevent instance
struct bufferevent* bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
//sock connect
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8989);
evutil_inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr);
// bind ip and prot adn client connect
bufferevent_socket_connect(bev, (struct sockaddr*)&addr, sizeof(addr));
//set event read/write event callback
bufferevent_setcb(bev, read_cb, write_cb, event_cb, base);
//write cmd addevent
struct event* ev = event_new(base, STDIN_FILENO, EV_READ | EV_PERSIST, send_cb, bev);
//add event
event_add(ev, NULL);
//start eventloop
event_base_dispatch(base);
//free bufferevent
bufferevent_free(bev);
//free event fream
event_base_free(base);
return 0;
}
//server.c
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<event2/event.h>
#include<event2/bufferevent.h>
#include<event2/listener.h>
char buf[1024];
void read_cb(struct bufferevent *bev, void *ctx)
{
memset(buf, 0, sizeof(buf));
int len = bufferevent_read(bev, buf, sizeof(buf));
printf("client read data = %s", buf);
bufferevent_write(bev, buf, len);
}
void write_cb(struct bufferevent *bev, void *ctx)
{
// printf("server kerner can write\n");
}
void event_cb(struct bufferevent* bev, short event, void*ctx)
{
struct event_base* base = (struct event_base*)ctx;
if (event & BEV_EVENT_EOF )
{
printf("old client go away BEV_EVENT_EOF\n");
//stop eventloop
event_base_loopbreak(base);
}
if ( event & BEV_EVENT_ERROR )
{
printf("BEV_EVENT_ERROR\n");
}
}
void listener_cb(struct evconnlistener * lev, evutil_socket_t fd, struct sockaddr * addr, int socklen, void * ctx)
{
//as accept new clinet conmutication fds
struct event_base* base = (struct event_base*)ctx;
//create
struct bufferevent* bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev, read_cb, write_cb, event_cb, base);
bufferevent_enable(bev, EV_READ | EV_WRITE);
}
int main(int agrc, char* agrv)
{
//create event fream instance
struct event_base* base = event_base_new();
//sock connect
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8989);
evutil_inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr);
struct evconnlistener* lev = evconnlistener_new_bind(base, listener_cb, base,
LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1,
(struct sockaddr*)&addr, sizeof(addr));
//set evconnlistener able
evconnlistener_enable(lev);
//start eventloop
printf("start event loop \n");
event_base_dispatch(base);
event_base_free(base);
return 0;
}
标签:bev,struct,框架,bufferevent,网络,base,libevent,buf,event 来源: https://blog.csdn.net/qq_22203741/article/details/117851775