From 4ce669343567fdde2a305f8e262408d08b698ccc Mon Sep 17 00:00:00 2001 From: adminPyf <3043130461@qq.com> Date: Thu, 10 Jun 2021 23:17:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'test=5Ffile'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 提交监听文件改变事件demo --- test_file/makefile | 2 ++ test_file/test_file.cpp | 68 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 test_file/makefile create mode 100644 test_file/test_file.cpp diff --git a/test_file/makefile b/test_file/makefile new file mode 100644 index 0000000..7fd3540 --- /dev/null +++ b/test_file/makefile @@ -0,0 +1,2 @@ +test_file:test_file.cpp + g++ $^ -o $@ -levent \ No newline at end of file diff --git a/test_file/test_file.cpp b/test_file/test_file.cpp new file mode 100644 index 0000000..9c6ce95 --- /dev/null +++ b/test_file/test_file.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +#ifdef _WIN32 + +#else + #include + + #include + #include + #include + #include +#endif // !_WIN32 + +using namespace std; + +void read_file(evutil_socket_t fd,short event,void *arg){ + char buf[1024] = {0}; + int len = read(fd,buf,sizeof(buf) - 1); + if(len > 0){ + cout << buf << endl; // 当有用户登录就会把 该信息打印出来 + }else { + cout << "." << endl; + this_thread::sleep_for(500ms); + } + +} + +int main(int agrc,char** agrv){ +#ifdef _WIN32 + //初始化socket库 + WSADATA wsa; + WSAStartup(MAKEWORD(2, 2), &wsa); +#else + if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { //忽略管道信号,发送数据给已关闭的socket,会飞掉! + return 1; + } +#endif + + event_config* conf = event_config_new(); + event_config_require_features(conf,EV_FEATURE_FDS); + event_base *base = event_base_new_with_config(conf); + event_config_free(conf); + + if(!base){ + cerr << "event_base_new_with_config failed" <