登录  /  注册
自制Ping(3) IcmpSendEcho2Ex Demo
php中文网
发布: 2016-06-07 15:42:44
原创
985人浏览过

IP Helper 可能需要WDK支持, 我本机装了WDK, 没试过不用会怎样. 由于IcmpSendEcho2Ex()回调的形式乎与我的WDK有所冲突, 所以这里只演示阻塞模式. Event应该不受影响. Demo下载地址: http://download.csdn.net/detail/ren0065/8388105 主要代码: void CPingTe

IP Helper 可能需要WDK支持, 我本机装了WDK, 没试过不用会怎样.

由于IcmpSendEcho2Ex()回调的形式似乎与我的WDK有所冲突, 所以这里只演示阻塞模式. Event应该不受影响.

Demo下载地址: http://download.csdn.net/detail/ren0065/8388105

主要代码:

void CPingTest1Dlg::OnBnClickedBtnPing()
{
	// TODO:  在此添加控件通知处理程序代码
	UpdateData(TRUE);

	HANDLE h_icmp = IcmpCreateFile();
	IPAddr ip_source = inet_addr(cs_source_ip_);
	IPAddr ip_destination = inet_addr(cs_destination_ip_);
	WORD w_request_data_size = atoi(cs_request_size_);
	LPVOID p_request_data = calloc(1, w_request_data_size);
	PIP_OPTION_INFORMATION p_option_info = NULL;
	LPVOID p_reply_data = calloc(1, sizeof(ICMP_ECHO_REPLY) + 8 + w_request_data_size);
	DWORD dw_reply_data_size = sizeof(ICMP_ECHO_REPLY) + 8 + w_request_data_size;
	DWORD dw_timeout = atoi(cs_timeout_);

	DWORD dw_result = IcmpSendEcho2Ex(
		h_icmp
		, NULL
		, NULL
		, NULL
		, ip_source
		, ip_destination
		, p_request_data
		, w_request_data_size
		, p_option_info
		, p_reply_data
		, dw_reply_data_size
		, dw_timeout);

	free(p_request_data);

	cs_output_.Empty();
	if (dw_result == 0)
	{
		DWORD dw_error = GetLastError();
		CString cs_error_msg;
		switch (dw_error)
		{
		case ERROR_INVALID_PARAMETER:
			cs_error_msg = "无效参数. 当IcmpHandle是一个无效handle或ReplySize的值小于ICMP_ECHO_REPLY或ICMP_ECHO_REPLY32时会返回该错误.";
			break;
		case ERROR_IO_PENDING:
			cs_error_msg = "异步处理正在进行中.调用IcmpSendEcho2Ex异步模式成功时会返回该值, 不是错误.";
			break;
		case ERROR_NOT_ENOUGH_MEMORY:
			cs_error_msg = "内存不足";
			break;
		case ERROR_NOT_SUPPORTED:
			cs_error_msg = "不支持该请求.如果本地计算机没有IPv4协议栈将返回该错误.";
			break;
		case IP_BUF_TOO_SMALL:
			cs_error_msg = "ReplySize指定的太小.";
			break;
		default:
		{
			HLOCAL hLocal = NULL;
			DWORD dwErr = GetLastError();
			DWORD systemLocale = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
			DWORD fOK = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
				NULL, dwErr, systemLocale, (LPTSTR)&hLocal, 0, NULL);
			cs_error_msg = (LPCTSTR)LocalLock(hLocal);
			break;
		}
		}
		cs_output_ = cs_error_msg;
	}
	else
	{
		CString cs_buffer;
		PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)p_reply_data;
		struct in_addr ReplyAddr;
		ReplyAddr.S_un.S_addr = pEchoReply->Address;
		cs_buffer.Format("Sent icmp message from %s to %s\r\n", cs_source_ip_, cs_destination_ip_);
		cs_output_ += cs_buffer;
		if (dw_result > 1)
		{
			cs_buffer.Format("Received %ld icmp message responses\r\n", dw_result);
			cs_output_ += cs_buffer;
			cs_buffer.Format("Information from the first response:\r\n");
			cs_output_ += cs_buffer;
		}
		else
		{
			cs_buffer.Format("Received %ld icmp message response\r\n", dw_result);
			cs_output_ += cs_buffer;
			cs_buffer.Format("Information from this response:\r\n");
			cs_output_ += cs_buffer;
		}
		cs_buffer.Format("Received from %s\r\n", inet_ntoa(ReplyAddr));
		cs_output_ += cs_buffer;
		cs_buffer.Format("Status = %ld ", pEchoReply->Status);
		cs_output_ += cs_buffer;

		if (pEchoReply->Status != IP_SUCCESS)
		{
			DWORD dw_status_err_msg_size = 1024;
			PWSTR cw_str_error = (PWSTR)calloc(1, dw_status_err_msg_size);
			GetIpErrorString(pEchoReply->Status, cw_str_error, &dw_status_err_msg_size);
			cs_output_ = WS2S(cw_str_error).c_str();
			free(cw_str_error);
		}
		else
		{
			cs_buffer.Format("(Successful)\r\n");
			cs_output_ += cs_buffer;
			cs_buffer.Format("RTT: %d\r\n", pEchoReply->RoundTripTime);
			cs_output_ += cs_buffer;
			cs_buffer.Format("SIZE: %d\r\n", pEchoReply->DataSize);
			cs_output_ += cs_buffer;
			cs_buffer.Format("TTL: %d\r\n", pEchoReply->Options.Ttl);
			cs_output_ += cs_buffer;
		}
	}

	UpdateData(FALSE);
	free(p_reply_data);
	IcmpCloseHandle(h_icmp);
}
登录后复制


相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学