ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

使用Win32控制台实现libevent通信

2020-07-05 09:38:38  阅读:280  来源: 互联网

标签:szWriteMsg struct bufferevent void Win32 base libevent 控制台 bev


libevent版本:libevent-2.0.22-stable

服务端:

#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <signal.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;

	WSADATA wsa_data;
	WSAStartup(MAKEWORD(2, 2), &wsa_data);

	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);
}

客户端

#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <signal.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;

	WSADATA wsa_data;
	WSAStartup(MAKEWORD(2, 2), &wsa_data);

	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("127.0.0.1");
	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);
}

  

标签:szWriteMsg,struct,bufferevent,void,Win32,base,libevent,控制台,bev
来源: https://www.cnblogs.com/rcg714786690/p/13237655.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有