使用 acl 異步庫及ICMP協議庫編寫了一個同時PING多個目標IP的程序

      在 acl 的軟件包中,lib_acl 是一個基礎的庫,另外,還有另一個庫 lib_protocol,這個庫中不只包含了 HTTP 協議的實現,並且還有一個 ICMP PING 協議的實現。前些日子,看到開源中國的微博中提到 fping 的新版本發佈了,這是一個能夠在一個線程裏同時 PING 多個 IP 的程序,心中不盡暗癢,想到本身曾經還專門實現了一個相似的庫,而且通用性可能更強,何不寫出來給你們分享一下?所以,本篇主要是先以一個簡單的小例子入手,演示如何使用 acl 中的這個 ICMP 協議包實現一個能夠同時支持 WIN32 和 LINUX 的多目標 PING 程序。dom

   部分 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);

 

      下面一個簡單的小例子:async

 

 

#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 下有該例子的完整實現,編譯後,運行下面命令:oop

      ./ping -n 10 www.baidu.com www.sina.com.cn www.hexun.com www.qq.com.net

      獲得以下的輸出結果線程

 

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/zsxxszcode

    acl 庫的下載地址:http://http://sourceforge.net/projects/acl/?source=directory對象

相關文章
相關標籤/搜索