libevent简单服务端和客户端实现

本文介绍了如何使用libevent编译生成动态库,并展示了基于libevent的简单服务端和客户端代码实现,用于创建一个基础的聊天应用。在编译客户端和服务端程序时,需要链接libevent_core库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.生成动态库说明

在开始使用之前,我们需要先搞清楚libevent编译生成的各个动态库的作用。
在下载libevent源码包进行编译以后,当前目录生成.libs目录,该目录下是所有的目标文件,这里我们只说明个动态库so文件的作用,通过makefile我们可以知道各个动态库包含内容:

动态库名称作用
libevent_core.so这个库包含了所有核心的事件和缓存功能
libevent_extra.so这个库包含外围的dns、rpc、http等协议使用
libevent.so这个库包含了libevent_core和libevent_extra的内容
libevent_openssl.so需要进行加密通信时可以使用这个
libevent_pthreads.so看名字就知道如果要用多线程的方式使用libevent,就需要用到这个库

下面使用libevent实现了一个很简单的服务端和客户端程序。

2. 服务端代码实现

//server-event.cpp
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/listener.h>
#include <event2/util.h>
#include <event2/event.h>

static const int PORT = 9995;

static char g_szWriteMsg[256] = {0};
static char g_szReadMsg[256] = {0};
static int g_iCnt = 0;

static void listener_cb(struct evconnlistener *, evutil_socket_t,
    struct sockaddr *, int socklen, void *);
static void conn_writecb(struct bufferevent *, void *);
static void conn_readcb(struct bufferevent *, void *);
static void conn_eventcb(struct bufferevent *, short, void *);

int
main(int argc, char **argv)
{
 struct event_base *base;
 struct evconnlistener *listener;
 struct event *signal_event;

 struct sockaddr_in sin;

 base = event_base_new();
 if (!base) {
  fprintf(stderr, "Could not initialize libevent!\n");
  return 1;
 }

 memset(&sin, 0, sizeof(sin));
 sin.sin_family = AF_INET;
 sin.sin_port = htons(PORT);//固定一个端口号

 //创建、绑定、监听socket
 listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
     LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1,
     (struct sockaddr*)&sin,
     sizeof(sin));

 if (!listener) {
  fprintf(stderr, "Could not create a listener!\n");
  return 1;
 } 

 event_base_dispatch(base);

 evconnlistener_free(listener);
 //event_free(signal_event);
 event_base_free(base);

 printf("done\n");
 return 0;
}

//有连接来时调用
static void
listener_cb(struct evconnlistener *listener, evutil_socket_t fd,
    struct sockaddr *sa, int socklen, void *user_data)
{
 struct event_base *base = (struct event_base*)user_data;
 struct bufferevent *bev;

 //构造一个bufferevent
 bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
 if (!bev) {
  fprintf(stderr, "Error constructing bufferevent!");
  event_base_loopbreak(base);
  return;
 }

 //绑定读事件回调函数、写事件回调函数、错误事件回调函数
 bufferevent_setcb(bev, conn_readcb, conn_writecb, conn_eventcb, NULL);

 bufferevent_enable(bev, EV_WRITE);
 bufferevent_enable(bev, EV_READ);

 const char *szMsg = "hi client!";
 bufferevent_write(bev, szMsg, strlen(szMsg));
}

static void
conn_writecb(struct bufferevent *bev, void *user_data)
{
 //printf("touch conn_writecb\n");
	
//	if ( strlen(g_szWriteMsg) > 0 )
//	{
//	bufferevent_write(bev, g_szWriteMsg, strlen(g_szWriteMsg));
//	memset(g_szWriteMsg, 0x00, sizeof(g_szWriteMsg));
//	}
}

static void
conn_readcb(struct bufferevent *bev, void *user_data)
{
 //printf("touch conn_readcb\n");
 memset(g_szReadMsg, 0x00, sizeof(g_szReadMsg));
 struct evbuffer *input = bufferevent_get_input(bev);
 size_t sz = evbuffer_get_length(input);
 if (sz > 0)
 {
  bufferevent_read(bev, g_szReadMsg, sz);
  printf("cli:>>%s\n", g_szReadMsg);
  memset(g_szWriteMsg, 0x00, sizeof(g_szWriteMsg));
  snprintf(g_szWriteMsg, sizeof(g_szWriteMsg)-1, "hi client, this count is %d", g_iCnt);
  g_iCnt++;
  //printf("ser:>>");
  //gets(g_szWriteMsg);
  //scanf("%s", g_szWriteMsg);
  
  bufferevent_write(bev, g_szWriteMsg, strlen(g_szWriteMsg));
 }
}

static void
conn_eventcb(struct bufferevent *bev, short events, void *user_data)
{
 if (events & BEV_EVENT_EOF) {
  printf("Connection closed.\n");
 } else if (events & BEV_EVENT_ERROR) {
  printf("Got an error on the connection: %s\n",
      strerror(errno));/*XXX win32*/
 }
 /* None of the other events can happen here, since we haven't enabled
  * timeouts */
 bufferevent_free(bev);
}

3. 客户端代码实现

//client-event.cpp
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/listener.h>
#include <event2/util.h>
#include <event2/event.h>

static const int PORT = 9995;
static char g_szWriteMsg[256] = {0};
static char g_szReadMsg[256] = {0};
static int g_iCnt = 0;
static void conn_writecb(struct bufferevent *, void *);
static void conn_readcb(struct bufferevent *, void *);
static void conn_eventcb(struct bufferevent *, short, void *);

int
main(int argc, char **argv)
{
 struct event_base *base;

 struct sockaddr_in sin;

 base = event_base_new();
 if (!base) {
  fprintf(stderr, "Could not initialize libevent!\n");
  return 1;
 }

 memset(&sin, 0, sizeof(sin));
 sin.sin_addr.s_addr = inet_addr("192.168.233.250");
 sin.sin_family = AF_INET;
 sin.sin_port = htons(PORT);
	
 struct bufferevent* bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
 if (bev == NULL )
 {
  fprintf(stderr, "socket init failed\n");
  return 1;
 }
 bufferevent_setcb(bev, conn_readcb, conn_writecb, conn_eventcb, NULL);

 //连接服务端
 int flag = bufferevent_socket_connect(bev, (struct sockaddr*)&sin, sizeof(sin));
 if (-1 == flag )
 {
  fprintf(stderr, "connect failed\n");
  return 1;
 }
 bufferevent_enable(bev, EV_READ | EV_WRITE);

 event_base_dispatch(base);
 event_base_free(base);

 printf("done\n");
 return 0;
}

static void
conn_writecb(struct bufferevent *bev, void *user_data)
{
 //printf("touch conn_writecb\n");
	
//	if ( strlen(g_szWriteMsg) > 0 )
//	{
//	bufferevent_write(bev, g_szWriteMsg, strlen(g_szWriteMsg));
//	memset(g_szWriteMsg, 0x00, sizeof(g_szWriteMsg));
//	}
}

static void
conn_readcb(struct bufferevent *bev, void *user_data)
{
 //printf("touch conn_readcb\n");
 memset(g_szReadMsg, 0x00, sizeof(g_szReadMsg));
 struct evbuffer *input = bufferevent_get_input(bev);
 size_t sz = evbuffer_get_length(input);
 if (sz > 0)
 {
  bufferevent_read(bev, g_szReadMsg, sz);
  printf("ser:>>%s\n", g_szReadMsg);
  memset(g_szWriteMsg, 0, sizeof(g_szWriteMsg));
  snprintf(g_szWriteMsg, sizeof(g_szWriteMsg)-1, "hi server,this count is %d", g_iCnt);
  g_iCnt++;
  //printf("cli:>>");
  //gets(g_szWriteMsg);
  //scanf("%s", g_szWriteMsg);
  bufferevent_write(bev, g_szWriteMsg, strlen(g_szWriteMsg));
 }
}

static void
conn_eventcb(struct bufferevent *bev, short events, void *user_data)
{
 if (events & BEV_EVENT_EOF) {
  printf("Connection closed.\n");
 } else if (events & BEV_EVENT_ERROR) {
  printf("Got an error on the connection: %s\n",
      strerror(errno));/*XXX win32*/
 }
 else if(events & BEV_EVENT_CONNECTED)
 {
  //连接成功时走这里,并且要客户端第一次触发读事件后连接才真正建立起来
  printf("connect success\n");
  const char* msg = "hi server,hao are you";
  bufferevent_write(bev, msg, strlen(msg));
  return;
 }
 bufferevent_free(bev);
}

上面服务端和客户端代码使用libevent建立了一个简单的聊天应用,编译时需链接-levent_core。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cpp加油站

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值