欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Linux 初识Libevent网络库

程序员文章站 2022-04-15 14:52:22
初识Libevent libevent是用c写的高并发网络io库,只要有文件描述符,就都可使用libevent。 libevent使用回调函数(callback) 。 有了libevent,网络编程我有 1, FIFO的进程间通信。 利用FIFO的进程间通信read端: 利用FIFO的进程间通信wr ......

初识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);

}

int main(int argc, char** argv){

  int port = atoi(argv[1]);
  struct event_base* base;
  base = event_base_new();
  if(!base){
    perror("event_base_new");
    exit(1);
  }

  struct sockaddr_in s;
  s.sin_family = af_inet;
  s.sin_port = htons(port);
  s.sin_addr.s_addr = htonl(inaddr_any);
  struct evconnlistener* listener =
    evconnlistener_new_bind(base, listencb, null, lev_opt_close_on_free,
                -1, (struct sockaddr*)&s, sizeof(s));
  if(!listener){
    perror("bind");
    exit(1);
  }
  event_base_dispatch(base);
  event_base_free(base);
}

client端:

#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>
#include <event2/dns.h>
#include <stdlib.h>
#include <event2/util.h>

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

}

//event callback
void eventcb(struct bufferevent *bev, short what, void *ctx){
  if(what & bev_event_connected){
    printf("connect okay\n");  
  }
  else if(what & bev_event_error){
    struct event_base* base = ctx;
    int err = bufferevent_socket_get_dns_error(bev);
    if(err){
      printf("dns error:%s\n", evutil_gai_strerror(err));
    }
    printf("closing\n");
    bufferevent_free(bev);
    event_base_loopexit(base, null);
  }
}

//terminal read callback
void termicb(evutil_socket_t fd, short what, void* ptr){
  char buf[64] = {0};
  int len = read(fd, buf, sizeof(buf));
  buf[len] = '\0';
  printf("in termicb\n");
  struct bufferevent* bev = ptr;
  bufferevent_write(bev, buf, len);
}
int main(int artc, char** argv){

  struct event_base* base;
  base = event_base_new();
 
  struct evdns_base* dns_base;
  dns_base = evdns_base_new(base, 1);

  struct bufferevent* bev;
  bev = bufferevent_socket_new(base, -1, bev_opt_close_on_free);

  int port = atoi(argv[2]);
  bufferevent_setcb(bev, readcb, null, eventcb, base);
  bufferevent_enable(bev, ev_read);
  bufferevent_socket_connect_hostname(bev, dns_base, af_inet, argv[1], port);

  struct event* ev = event_new(base, stdin_fileno, ev_read | ev_persist, termicb, bev);
  event_add(ev, null);
  event_base_dispatch(base);
  event_base_free(base);
}

c/c++ 学习互助qq群:877684253

Linux 初识Libevent网络库

本人微信:xiaoshitou5854