TOP

Linux 初识Libevent网络库(一)
2019-07-05 18:10:29 】 浏览:154
Tags:Linux 初识 Libevent 网络

初识Libevent

libevent是用c写的高并发网络io库,只要有文件描述符,就都可使用libevent。

libevent使用回调函数(callback) 。

有了libevent,网络编程我有

1, FIFO的进程间通信。

利用FIFO的进程间通信read端:

#include <event2/event.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define MYFIFO "myfifo"

//call back
void readcb(evutil_socket_t fd, short what, void* arg){
  //read fifo
  char buf[24] = {0};
  int len = read(fd, buf, sizeof(buf));
  buf[len] = '\0';
  printf("data len = %d, buf = %s\n", len, buf);
  printf("read event:%s\n", what & EV_READ ? "Yes" : "No");
}

int main(){
  unlink(MYFIFO);
  mkfifo(MYFIFO, 0664);

  int fd = open(MYFIFO, O_RDONLY | O_NONBLOCK);
  //int fd = open(MYFIFO, O_RDONLY);

  struct event_base* base;
  base = event_base_new();

  //create event
  struct event* ev = NULL;
  ev = event_new(base, fd, EV_READ | EV_PERSIST | EV_ET, readcb, NULL);

  //add event
  event_add(ev, NULL);

  //start
  event_base_dispatch(base);

  //free event
  event_free(ev);
  event_base_free(base);
  close(fd);

  return 0;
}

利用FIFO的进程间通信write端:

#include <event2/event.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#define MYFIFO "myfifo"

//call back
void writecb(evutil_socket_t fd, short what, void* arg){
  //write fifo
  char buf[24] = {0};
  static int num = 0;
  sprintf(buf, "num = %d", num++);
  write(fd, buf, strlen(buf) + 1);
}

int main(){

  int fd = open(MYFIFO, O_WRONLY | O_NONBLOCK);

  struct event_base* base;
  base = event_base_new();

  //create event
  struct event* ev = NULL;
  ev = event_new(base, fd, EV_WRITE | EV_PERSIST | EV_ET, writecb, NULL);

  //add event
  event_add(ev, NULL);

  //start
  event_base_dispatch(base);

  //free event
  event_free(ev);
  event_base_free(base);
  close(fd);

  return 0;
}

2, socket通信。

server端:

#include <event2/event.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <event2/bufferevent.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <event2/listener.h>

//write callback
void write_cb(struct bufferevent* bev, void* ctx){
  printf("all is sent\n");
}

//read callback
void read_cb(struct bufferevent *bev, void *ctx){
  char buf[64];
  size_t ret = bufferevent_read(bev, buf, sizeof(buf));
  buf[ret] = '\0';
  printf("server recf:%s\n", buf);

  bufferevent_write(bev, "hahaha", 6);
}

//event callback
void event_cb(struct bufferevent *bev, short what, void *ctx){

  if(what & BEV_EVENT_EOF){
    printf("EOF\n");
  }
  if(what & BEV_EVENT_CONNECTED){
    printf("connected\n");
  }
}

//listener call back
void listencb(struct evconnlistener* listener, evutil_socket_t fd,
          struct sockaddr* cli, int len, void* ptr){

  struct event_base* base = evconnlistener_get_base(listener);
  struct bufferevent* bev =
    bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);

  //set callback function
  bufferevent_setcb(bev, read_cb, write_cb, event_cb, NULL);
  bufferevent_enable(bev, EV_READ | EV_WRITE);

  
  //set water
  bufferevent_setwatermark(bev, EV_READ, 10, 0);
  bufferevent_setwatermark(bev, EV_WRITE, 1, 2);  
		
Linux 初识Libevent网络库(一) https://www.cppentry.com/bencandy.php?fid=45&id=227270

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言:正负数之间取模运算(转载.. 下一篇用GDB调试程序(一)