libevent使用event_new和不使用的两种方法
写两个简单的demo,对照一下各自的方法
#include <sys/types.h> #include <event2/event-config.h> #include <sys/stat.h>
#ifndef _WIN32
#include <sys/queue.h>
#include <unistd.h>
#include <sys/time.h>
#else
#include <winsock2.h>
#include <windows.h>
#endif
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <event.h>
#include <event2/event.h> int called = ; static void
signal_cb(evutil_socket_t fd, short event, void *arg)
{
struct event *signal = arg; printf("signal_cb: got signal %d\n", event_get_signal(signal)); if (called >= )
event_del(signal); called++;
} int
main(int argc, char **argv)
{
struct event signal_int;
struct event_base* base;
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData; wVersionRequested = MAKEWORD(, ); (void) WSAStartup(wVersionRequested, &wsaData);
#endif /* Initalize the event library */
// base = event_base_new();
//
// /* Initalize one event */
// signal_int = evsignal_new(base, SIGINT, signal_cb, event_self_cbarg());
event_init();
event_set(&signal_int, SIGINT, EV_SIGNAL, signal_cb, (void *)&signal_int);
event_add(&signal_int, NULL); event_dispatch();
//event_free(signal_int);
//event_base_free(base); return ();
}
#include <sys/types.h> #include <event2/event-config.h> #include <sys/stat.h>
#ifndef _WIN32
#include <sys/queue.h>
#include <unistd.h>
#include <sys/time.h>
#else
#include <winsock2.h>
#include <windows.h>
#endif
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h> #include <event2/event.h> int called = 0; static void
signal_cb(evutil_socket_t fd, short event, void *arg)
{
struct event *signal = arg; printf("signal_cb: got signal %d\n", event_get_signal(signal)); if (called >= 2)
event_del(signal); called++;
} int
main(int argc, char **argv)
{
struct event *signal_int;
struct event_base* base;
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData; wVersionRequested = MAKEWORD(2, 2); (void) WSAStartup(wVersionRequested, &wsaData);
#endif /* Initalize the event library */
base = event_base_new(); /* Initalize one event */
signal_int = evsignal_new(base, SIGINT, signal_cb, event_self_cbarg()); //evsignal_new内部调用了event_new
event_add(signal_int, NULL);
event_base_dispatch(base);
event_free(signal_int);
event_base_free(base); return (0);
}
libevent使用event_new和不使用的两种方法的更多相关文章
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- android 之 启动画面的两种方法
现在,当我们打开任意的一个app时,其中的大部分都会显示一个启动界面,展示本公司的logo和当前的版本,有的则直接把广告放到了上面.启动画面的可以分为两种设置方式:一种是两个Activity实现,和一 ...
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- php如何防止图片盗用/盗链的两种方法(转)
图片防盗链有什么用? 防止其它网站盗用你的图片,浪费你宝贵的流量.本文章向大家介绍php防止图片盗用/盗链的两种方法 Apache图片重定向方法 设置images目录不充许http访问 Apache服 ...
- WPF程序将DLL嵌入到EXE的两种方法
WPF程序将DLL嵌入到EXE的两种方法 这一篇可以看作是<Visual Studio 版本转换工具WPF版开源了>的续,关于<Visual Studio 版本转换工具WPF版开源了 ...
- MongoDB实现分页(两种方法)
1.插入实验数据 偷懒用下samus,100条. ; i < ; i++) { Document doc = new Document(); doc["ID"] = i; d ...
- css:图标与文字对齐的两种方法
(好久没写博客了,这几个月的积累比较零碎,记在本子上,现在开始整理归类) 在平时写页面的过程中,常遇到要把小图标与文字对齐的情况.比如: 总结了两种方法,代码量都比较少. 第一种 对img设置竖直方向 ...
- .net中创建xml文件的两种方法
.net中创建xml文件的两种方法 方法1:根据xml结构一步一步构建xml文档,保存文件(动态方式) 方法2:直接加载xml结构,保存文件(固定方式) 方法1:动态创建xml文档 根据传递的值,构建 ...
- 两种方法设置disabled属性
//两种方法设置disabled属性 $('#fileup').attr("disabled",true); $('#fileup').attr("disabled&qu ...
随机推荐
- C 标准库 - string.h之memchr使用
memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...
- Mybatis 关联查询(三)
多对多的管理查询结果映射 1. 需求: 查询用户购买的商品信息 2. 分析: (1)用户和商品没有直接关联 (2)用户和订单进行了关联,订单和订单明细进行了关联,订单明细和商品进行了关联,因此 ...
- leetcode简单题目两道(1)
Problem: You are playing the following Nim Game with your friend: There is a heap of stones on the t ...
- 如何在不接入微信API的情况下自定义分享内容(图片、链接、标题)
方法如下: 1.设置分享title:动态改变document.title值即可: document.title = 'test' 2.设置分享图片:在页面隐藏一张尺寸大于290*290的图(图片需要容 ...
- C#把大写英文变成小写英文,把小写英文变成大写英文
static void Main(string[] args) { string s; // 声明一个变量,来接受用户输入的值. Console.WriteLine("请输入一个字符串:& ...
- ios下虚拟键盘出现"搜索"字样
最近在开发过程中,发现用户输入想要检索的内容,弹出虚拟键盘,在安卓机上虚拟键盘最右下角会有‘搜索’字样,而ios上虚拟键盘最右下角只有‘换行’字样, 这样用户体验就会大打折扣. 安卓机上虚拟键盘 io ...
- Newtonsoft.Json 全部配置
需要在序列化时候,忽略掉某些字段,对Newtonsoft.Json进行全局配置,如下: 1. 自定 ContractResolver public class MyContractResolver : ...
- 简单的CRUD(二)
一.重构简单的CRUD 1.JDBC工具类 1.因为在crud中都包含一些相同的代码所以可以提取出来,抽取代码重构为工具类. 2.将工具类设置为static静态类,方便调用,不需要new对象. pub ...
- 使用js获取URL地址栏里面的参数, 获取请求链接参数,函数定义如下
function getUrlRequestParam(name) { var paramUrl = window.location.search.substr(1); var paramStrs = ...
- 深入理解jQuery插件开发总结(三)
容器:一个即时执行函数 根本上来说,每个插件的代码是被包含在一个即时执行的函数当中,如下: (function(arg1, arg2) { // 代码 })(arg1, arg2); 即时执行函数,顾 ...