在 acl 的软件包中,lib_acl 是一个基础的库,另外,还有另外一个库 lib_protocol,这个库中不仅包含了 HTTP 协议的实现,而且还有一个 ICMP PING 协议的实现。前些日子,看到开源中国的微博中提到 fping 的新版本发布了,这是一个可以在一个线程里同时 PING 多个 IP 的程序,心中不尽暗痒,想到自己曾经还专门实现了一个类似的库,并且通用性可能更强,何不写出来给大家分享一下?因此,本篇主要是先以一个简单的小例子入手,演示如何使用 acl 中的这个 ICMP 协议包实现一个可以同时支持 WIN32 和 LINUX 的多目标 PING 程序。
部分 API 接口说明:
/**
* 创建ICMP会话对象
* @param aio {ACL_AIO*} 如果该项不为空,则内部在通信过程中采用非阻塞模式,
* 否则采用阻塞模式
* @param check_tid {int} 是否在校验响应包时检查数据中的线程号字段
* @return {ICMP_CHAT*} ICMP会话对象句柄
*/
ICMP_API ICMP_CHAT *icmp_chat_create(ACL_AIO *aio, int check_tid);
/**
* 释放ICMP会话对象
* @param chat {ICMP_CHAT*} ICMP会话对象句柄
*/
ICMP_API void icmp_chat_free(ICMP_CHAT *chat);
/**
* 判断当前的ICMP会话对象中所有探测任务是否已经完成
* @param chat {ICMP_CHAT*} 会话对象句柄
* @return {int} != 0: 表示完成; 0: 表示未完成
*/
ICMP_API int icmp_chat_finish(ICMP_CHAT *chat);
/**
* ping 一台主机(内部默认每个探测包长度为64个字节)
* @param chat {ICMP_CHAT*} 会话对象句柄
* @param domain {const char*} 域名标识字符串,可以为空
* @param ip {const char*} 主机IP地址,不能为空
* @param npkt {size_t} 对该主机发送的数据包个数
* @param delay {int} 发送探测数据包的时间间隔(秒)
* @param timeout {int} 被探测主机的响应包超时时间(秒)
*/
ICMP_API void icmp_ping_one(ICMP_CHAT *chat, const char *domain,
const char *ip, size_t npkt, int delay, int timeout);
/**
* 输出当前ICMP的会话状态
* @param chat {ICMP_CHAT*} 会话对象句柄
*/
ICMP_API void icmp_stat(ICMP_CHAT *chat);
/**
* 取得当前ICMP会话对象中的当前会话序列号值
* @param chat {ICMP_CHAT*} 会话对象句柄
* @return {unsigned short} 会话序列号值
*/
ICMP_API unsigned short icmp_chat_seqno(ICMP_CHAT *chat);
下面一个简单的小例子:
#include "lib_acl.h"
#include "lib_protocol.h"
static void ping_main_async(void)
{
int delay = 1; /* 发送 PING 数据包的时间间隔(秒) */
int npkt = 10; /* 发送的 PING 数据包个数 */
ACL_AIO *aio;
ICMP_CHAT *icmp;
/* 创建非阻塞异步通信句柄 */
aio = acl_aio_create(ACL_EVENT_SELECT);
acl_aio_set_keep_read(aio, 0);
/* 创建 ICMP 对象 */
icmp = icmp_chat_create(aio, 1);
/* PING www.baidu.com 的一个 IP 地址*/
icmp_ping_one(icmp, NULL, 61.135.169.115, npkt, delay, 1);
/* PING www.sina.com.cn 的一个 IP 地址 */
icmp_ping_one(icmp, NULL, 202.108.33.60, npkt, delay, 1);
/* PING www.hexun.com 的一个 IP 地址 */
icmp_ping_one(icmp, NULL, 202.99.16.169, npkt, delay, 1);
/* PING www.qq.com 的一个 IP 地址 */
icmp_ping_one(icmp, NULL, 61.135.167.36, npkt, delay, 1);
while (1) {
/* 如果 PING 结束,则退出循环 */
if (icmp_chat_finish(icmp)) {
printf("over now!, hosts' size=%d, count=%d\r\n",
icmp_chat_size(icmp), icmp_chat_count(icmp));
break;
}
/* 异步事件循环过程 */
acl_aio_loop(aio);
}
/* 显示 PING 结果 */
icmp_stat(icmp);;
/* 释放 ICMP 对象 */
icmp_chat_free(icmp);
/* 销毁非阻塞句柄 */
acl_aio_free(aio);
}
可以看出,该例子还是非常简单的,在 acl/samples/ping 下有该例子的完整实现,编译后,运行下面命令:
./ping -n 10 www.baidu.com www.sina.com.cn www.hexun.com www.qq.com
得到如下的输出结果
Reply from 202.108.33.60: bytes=56 time=5.427ms TTL=202 icmp_seq=2 status=0
Reply from 61.135.169.105: bytes=64 time=5.975ms TTL=61 icmp_seq=1 status=0
Reply from 61.135.169.125: bytes=64 time=6.394ms TTL=61 icmp_seq=0 status=0
Reply from 61.135.167.36: bytes=64 time=8.147ms TTL=61 icmp_seq=4 status=0
Reply from 202.99.16.169: bytes=64 time=8.532ms TTL=202 icmp_seq=3 status=0
Reply from 61.135.169.105: bytes=64 time=4.879ms TTL=61 icmp_seq=6 status=0
Reply from 202.108.33.60: bytes=56 time=5.313ms TTL=202 icmp_seq=5 status=0
Reply from 61.135.169.125: bytes=64 time=6.695ms TTL=61 icmp_seq=7 status=0
Reply from 61.135.167.36: bytes=64 time=5.963ms TTL=61 icmp_seq=8 status=0
。。。
Ping statistics for 61.135.169.125: www.baidu.com
Packets: Sent = 10, Received = 10, Lost = 0 (0.00% loss),
Approximate round trip times in milli-seconds:
Minimum = 5.187 ms, Maximum = 7.143 ms, Average = 6.307 ms
Ping statistics for 61.135.169.105: www.baidu.com
Packets: Sent = 10, Received = 10, Lost = 0 (0.00% loss),
Approximate round trip times in milli-seconds:
Minimum = 4.123 ms, Maximum = 8.807 ms, Average = 5.556 ms
Ping statistics for 202.108.33.60: www.sina.com.cn
Packets: Sent = 10, Received = 10, Lost = 0 (0.00% loss),
Approximate round trip times in milli-seconds:
Minimum = 4.319 ms, Maximum = 8.073 ms, Average = 5.855 ms
Ping statistics for 202.99.16.169: www.hexun.com
Packets: Sent = 10, Received = 10, Lost = 0 (0.00% loss),
Approximate round trip times in milli-seconds:
Minimum = 5.376 ms, Maximum = 8.532 ms, Average = 6.453 ms
Ping statistics for 61.135.167.36: www.qq.com
Packets: Sent = 10, Received = 10, Lost = 0 (0.00% loss),
Approximate round trip times in milli-seconds:
Minimum = 4.292 ms, Maximum = 8.147 ms, Average = 5.948 ms
>>>max pkts: 50
个人微博:http://weibo.com/zsxxsz
acl 库的下载地址:http://http://sourceforge.net/projects/acl/?source=directory
分享到:
相关推荐
【标题】:“acl.rar”指的是一个名为“acl”的压缩包文件,后缀为“.rar”,通常用于存储多个文件和文件夹。这里的“_epoll server”可能是对压缩包内容的补充说明,暗示其中包含了与epoll服务器相关的源代码或文档...
lwIP(Lightweight TCP/IP)是一个开放源代码的TCP/IP协议栈,专为嵌入式系统设计,由瑞典厄勒布鲁大学(Linköping University)的Adiscon公司开发。这个小巧而强大的网络协议栈被广泛应用于各种资源有限的设备,如...
2. **IP地址和子网掩码**:192.168.19.255/20表示这是一个使用CIDR(无类别域间路由)表示法的IP地址,/20代表子网掩码的前20位是网络部分。所以这是一个网络地址,不在192.168.19.0网段上,而在192.168.16.0网段上...
例如,使用NetworkInterface.GetIsNetworkAvailable()方法可以判断计算机是否已连接到网络,而Ping类则可以发送ICMP回显请求,检查与特定主机的连通性。 接下来,**下载文件**是Web编程中常见的需求。VB.NET的...
- **ping**: 向目标发送ICMP Echo请求报文,用于测试连通性和延迟。 - **ppp**: 启动PPP(Point-to-Point Protocol)协议,用于建立点对点连接。 - **reload**: 关闭路由器并执行冷启动,通常用于更新软件或解决某些...