Created
December 6, 2018 16:22
-
-
Save baixiangcpp/f2d3feea38c81f50d02f33483587d2f2 to your computer and use it in GitHub Desktop.
A libevent helloworld not using changelist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
大牛,拿走用用哈