Skip to content

Instantly share code, notes, and snippets.

@baixiangcpp
Created December 6, 2018 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baixiangcpp/f2d3feea38c81f50d02f33483587d2f2 to your computer and use it in GitHub Desktop.
Save baixiangcpp/f2d3feea38c81f50d02f33483587d2f2 to your computer and use it in GitHub Desktop.
A libevent helloworld not using changelist
#include <event2/event.h>
#define BUF_SIZE 1024
void onError(const char *reason)
{
perror(reason);
exit(EXIT_FAILURE);
}
void read_cb(int fd,short events,void* arg)
{
char buf[BUF_SIZE] = {0};
int ret = read(fd,buf,sizeof(buf)-1);
if(ret < 0 )
onError("read()");
buf[ret] = 0;
printf("What you type is : %s \n",buf);
}
int main()
{
struct event_base* base = event_base_new();
if(!base)
onError("event_base_new()");
struct event* ev = event_new(base,STDIN_FILENO,EV_READ | EV_PERSIST ,read_cb,NULL);
if(!ev)
onError("event_new()");
event_add(ev,NULL);
event_base_dispatch(base);
event_free(ev);
event_base_free(base);
}
@dongziqi
Copy link

大牛,拿走用用哈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment