服务端代码:
/*************************************************************************
> File Name: server.c
> Author: chencj
> Mail: 1378755306@qq.com
> Created Time: 2020年08月17日 星期一 17时50分07秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/listener.h>
#include <event2/bufferevent.h>
//读回调函数
void read_cb(struct bufferevent *bev,void *arg)
{
char buf[1024];
bzero(buf,sizeof(buf));
//读数据用bufferevent_read
bufferevent_read(bev,buf,sizeof(buf));
if(buf[0] != '\0')
{
printf("recv from client data is %s \n",buf);
char *p = "成功接受数据";
bufferevent_write(bev,p,strlen(p) + 1);
}
}
//写回调函数,在bufferevent_write执行结束调用,没有多少意义
void write_cb(struct bufferevent *bev,void *arg)
{
printf("成功发送数据...\n");
}
//错误处理回调函数
void event_cb(struct bufferevent *bev,short events,void *arg)
{
if(events & BEV_EVENT_EOF)
{
printf("connect end...\n");
}
if(events * BEV_EVENT_ERROR)
{
printf("some other error\n");
}
//释放资源
bufferevent_free(bev);
printf("bufferevent 已被释放\n");
}
//回调函数,有客户端连接并且发送数据的时候会调用 fd是有客户端连接进来创建的cfd
void listen_cb(struct evconnlistener *listener,int fd,struct sockaddr *addr,int len,void *ptr)
{
struct event_base *base = (struct event_base *)ptr;
//添加事件 cfd和bev绑定在一起
struct bufferevent *bev;
bev = bufferevent_socket_new(base,fd,BEV_OPT_CLOSE_ON_FREE);
//设置bev回调函数
bufferevent_setcb(bev,read_cb,write_cb,event_cb,NULL);
//读缓冲默认是不能使用的,打开读缓冲
bufferevent_enable(bev,EV_READ);
}
int main()
{
//地址结构
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(6666);
server_addr.sin_addr.s_addr = INADDR_ANY;
//建立base;
struct event_base *base = NULL;
base = event_base_new();
//建立监听器 listen_cb是回调函数,有客户端连接会调用
struct evconnlistener *listener;
listener = evconnlistener_new_bind(base,listen_cb,base,LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE,66,
(struct sockaddr*)&server_addr,sizeof(server_addr));
//设置循环
event_base_dispatch(base);
//释放资源
evconnlistener_free(listener);
event_base_free(base);
return 0;
}
客户端代码:
/*************************************************************************
> File Name: client.c
> Author: chencj
> Mail: 1378755306@qq.com
> Created Time: 2020年08月18日 星期二 10时45分13秒
************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
void sys_err(const char *msg)
{
perror(msg);
exit(1);
}
void read_cb(struct bufferevent *bev,void *arg)
{
char buf[1024] = {0};
bufferevent_read(bev,buf,sizeof(buf));
printf("fwq say : %s \n",buf);
//bufferevent_write(bev,buf,strlen(buf)+1);
sleep(1);
}
void write_cb(struct bufferevent *bev,void *arg)
{
printf("客户端成功发送数据...\n");
}
void event_cb(struct bufferevent *bev,short what,void *arg)
{
if(what & BEV_EVENT_EOF)
{
printf("connect close...\n");
}
if(what & BEV_ERROR)
{
printf("some error\n");
}
if(what & BEV_EVENT_CONNECTED)
{
printf("已连接服务器...\n");
return ;
}
//释放资源
bufferevent_free(bev);
}
void read_terminal(int fd,short what,void *arg)
{
char buf[1024] = {0};
read(fd,buf,sizeof(buf));
struct bufferevent *bev = (struct bufferevent*)arg;
//写给服务器
bufferevent_write(bev,buf,strlen(buf)+1);
}
int main()
{
int fd;
fd = socket(AF_INET,SOCK_STREAM,0);
if(fd == -1)
sys_err("socket error\n");
//创建事件底座
struct event_base * base = NULL;
base = event_base_new();
//创建bufferevent,把创建的fd放到bufferevent中
struct bufferevent *bev = NULL;
bev = bufferevent_socket_new(base,fd,BEV_OPT_CLOSE_ON_FREE);
//创建地址结构
struct sockaddr_in srv_addr;
srv_addr.sin_family = AF_INET;
srv_addr.sin_port = htons(6666);
srv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//连接服务器
if(bufferevent_socket_connect(bev,(struct sockaddr*)&srv_addr,sizeof(srv_addr)) != 0)
sys_err("connect error\n");
//设置回调函数
bufferevent_setcb(bev,read_cb,write_cb,NULL,NULL);
bufferevent_enable(bev,EV_READ | EV_WRITE);
//创建事件监听终端是否有数据输入
struct event *ev = NULL;
ev = event_new(base,STDIN_FILENO,EV_PERSIST | EV_READ,read_terminal,bev);
event_add(ev,NULL);
//循环
event_base_dispatch(base);
//释放资源
event_free(ev);
event_base_free(base);
return 0;
}