⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ewdpingdlg.cpp

📁 VC通信程序源代码.TCP/UDP高级编程,TCP,IP终端程序源代码等.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//////////////////////////////////////////
void CEWDPingDlg::StartTimer()
{

	TimerActive = FALSE;
	
	icmpIntervalCount = 0;
	icmpMaxIntervals = (PingSocket.icmpPingTimer / MAX_INTERVAL_TIME);

	TimerNumber = SetTimer(IDT_TIMER1, MAX_INTERVAL_TIME, NULL);
	if (TimerNumber != 0)
		TimerActive = TRUE;
	
	return;

}

//////////////////////////////////////////////
//											//
//				Stop the Timer				//
//											//
//	Issue KillTimer and reset TimerActive	//
//											//
//////////////////////////////////////////////
void CEWDPingDlg::StopTimer()
{

	if (TimerActive)
		KillTimer (TimerNumber);
	TimerActive = FALSE;
}

//////////////////////////////////////////////////
//												//
//				Change Icon State				//
//												//
//		Select the next icon image to display	//
//		increment the IconState and invoke		//
//		icon display							//
//												//
//////////////////////////////////////////////////

void CEWDPingDlg::ChangeIconState(void)
{
	int			IconId;
	CStatic *	pDlgWnd;

	switch (IconState % 4)
	{
	case 0:
		IconId = IDI_CommLink2;
		break;
	case 1:
		IconId = IDI_CommLink3;
		break;
	case 2:
		IconId = IDI_CommLink4;
		break;
	case 3:
		IconId = IDI_CommLink3;
		break;
	default:
		IconId = IDI_CommLink1;
		break;
	}
	
	IconState++;

	m_hIcon = AfxGetApp()->LoadIcon(IconId);		

	pDlgWnd=(CStatic*)GetDlgItem(IDC_CommLink);
	pDlgWnd->SetIcon(m_hIcon);						

	if(IsIconic())
	{
		InvalidateRect(NULL,TRUE);
	}

}

//////////////////////////////////////////////////////////////////////////
//																		//
//							BUTTON HANDLING SECTION						//
//																		//
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////
//											//
//			Issue PING Request				//
//											//
//	Set IconState to default, Set icmpTTL	//
//	to MaxHops, reset icmpTracing, and		//
//	issue IcmpPing request.					//
//											//
//////////////////////////////////////////////
//PING按钮处理程序
void CEWDPingDlg::OnPingButton() 
{
	IconState = 0;
	//列表框插入一条空记录
	DisplayBlankLine ();
	
	icmpTracing = FALSE;
	icmpPingTTL = PingSocket.icmpMaxHops;

	PingSocket.icmpCurSeq = 0;
	//调用SENDPING函数发送PING 命令
	SendPing ( );
}

//////////////////////////////////////////
//										//
//			Trace Route Request			//
//										//
//	Set icmpPingTTL to 0, icmpHops to	//
//	0, set default icon and output		//
//	message about the target, then		//
//	issue a PING with icmpTracing set	//
//	to TRUE to start tracing.			//
//										//
//////////////////////////////////////////

//单击"trace route"按钮处理程序
void CEWDPingDlg::OnTraceButton() 
{
	//列表框插入一个空白项
	DisplayBlankLine ();
	//设定TTL为0
	icmpPingTTL = 0;
	PingSocket.icmpHops = 0;
	//图标状态为0
	IconState = 0;

	PingSocket.icmpCurSeq = 0;
	//设定地址
	TraceTarget.s_addr = HostIPAddress();
	CString TComment;
	TComment.Format("Trace Target = %s",
				    inet_ntoa(TraceTarget));
	//活动TRACE的信息
	TraceComment (TComment);

	icmpTracing = TRUE;
	//调用SendPing函数
	SendPing ( );
}

//////////////////////////////////////////
//										//
//			Stop Current Ping			//
//										//
//////////////////////////////////////////
void CEWDPingDlg::OnStop() 
{

	if (!PingSent)
		return;
	
	PingSent = FALSE;

	StopTimer();
	EndTrace();

	m_TraceList.InsertItem (m_TraceList.GetItemCount(), 
							"", 
							Icon_BlueArrow);
	SetTraceFocus (m_TraceList.GetItemCount()-1,
				   0);
	DisplayTrace ("","","Operation CANCELLED");

}

//////////////////////////////////////////
//										//
//			Clear Display List			//
//										//
//	Delete all items from ListCtrl box	//
//										//
//////////////////////////////////////////
void CEWDPingDlg::OnClearButton() 
{
	if (!PingSent)
 		m_TraceList.DeleteAllItems();
	
}

//////////////////////////////////////////
//										//
//			Set operating options		//
//										//
//	Copy current option settings to		//
//	interchange variables, invoke		//
//  options dialog.						//
//										//
//	Exit dialog if option dialog was	//
//	cancelled.							//
//										//
//	Update current option variables and //
//	save in the Registry.				//
//										//
//////////////////////////////////////////
void CEWDPingDlg::OnOptionsButton() 
{

	CPingOptionsDlg OptionsDlg;

	OptionsDlg.m_BufferSize = icmpDataLen;
	OptionsDlg.m_DefaultHost = DefHost;
	OptionsDlg.m_PingTimeout = PingSocket.icmpPingTimer;
	OptionsDlg.m_MaxHops = PingSocket.icmpMaxHops;

	int Result = OptionsDlg.DoModal();

	if (Result != IDOK)
		return;

	icmpDataLen = OptionsDlg.m_BufferSize;
	DefHost = OptionsDlg.m_DefaultHost;
	PingSocket.icmpPingTimer = OptionsDlg.m_PingTimeout;
	PingSocket.icmpMaxHops = OptionsDlg.m_MaxHops;

	SaveRegValues();
	
}

//////////////////////////////////////////
//										//
//		Shut down pinger and exit		//
//										//
//////////////////////////////////////////
void CEWDPingDlg::OnQuit() 
{

	StopTimer ();
	PingSocket.CloseIcmpSocket();
	CDialog::OnOK();

}

//////////////////////////////////////////////////////////////////////////
//																		//
//							SOCKET SUPPORT SECTION						//
//																		//
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////
//												//
//			Send a PING Message					//
//												//
//		Sends a PING message to the currently	//
//		selected host.							//
//												//
//		If icmpTracing is TRUE,					//
//			increment and set icmpPingTTL to	//
//			reach the next node in the Internet //
//			tree. Note: must timeout for a small//
//			period of time to allow TTL to		//
//			complete.							//
//												//
//		Start the timer and issue the Icmp Ping	//
//												//
//////////////////////////////////////////////////

//发送PING消息包到目的主机
void CEWDPingDlg::SendPing(void)
{

	PingSent = TRUE;
	PingSocket.icmpCurSeq++;
	PingSocket.icmpCurId = (USHORT)GetCurrentProcessId();

	PingSocket.icmpHostAddress = HostIPAddress();
	
	//如果目标地址为空,则返回
	if (PingSocket.icmpHostAddress == NULL)
		return;
	//如果icmpTracing为真,则必须将
	if (icmpTracing)
	{
		icmpPingTTL++;
	
    }
	//设定TTL
	if (PingSocket.SetTTL (icmpPingTTL) == SOCKET_ERROR)
	{
		PingSocket.DisplayError ("setsocket(TTL)",
								 "CEWDPingDlg::SendPing");
		return;
	}
	//要设定一定的时间让TTL设定完成
	Sleep (100);
	
	//显示序列号,同时用蓝色尖头表示出来
	SetTraceSequence (PingSocket.icmpCurSeq, 
					  m_TraceList.GetItemCount(), 
					  Icon_BlueArrow);

	//设定地址
	PingSocket.icmpSockAddr.sin_family = PF_INET;
	PingSocket.icmpSockAddr.sin_addr.s_addr = PingSocket.icmpHostAddress;
	PingSocket.icmpSockAddr.sin_port = 0;
	//开始计时
	StartTimer();
	//调用PING函数发出PING命令
	if (PingSocket.Ping (pIcmpBuffer, icmpDataLen) == SOCKET_ERROR)
		PingSocket.DisplayError("Ping", "CEWDPingDlg::SendPing");

}

//////////////////////////////////////////////////////
//													//
//					HostIPAddress					//
//													//
//		Return the IP Address for the curently		//
//		selected host in Internet byte order.		//
//													//
//		GetIPAddress leaves a copy of the address	//
//		in icmpSockAddr and sets HostName to its	//
//		DNS official name.							//
//													//
//////////////////////////////////////////////////////
unsigned long CEWDPingDlg::HostIPAddress()
{

	unsigned long		iHostAddress;

	GetDlgItemText (IDC_DEST, HostName, MAXHOSTNAME);

	iHostAddress = PingSocket.GetIPAddress ((LPSTR)&HostName);

	if ((iHostAddress == INADDR_ANY) || (iHostAddress == NULL))
	{
		PingSocket.DisplayError ("gethostbyname", 
								 "CEWDPingDlg::HostIPAddress");
		iHostAddress = NULL;
	}
	
	else
		
		SetDlgItemText (IDC_DEST, HostName);

	return iHostAddress;
}


//////////////////////////////////////////////////////////////////////////
//																		//
//					CListCtrl and Image List Sections					//
//																		//
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////
//										//
//		Initialize Image Lists			//
//										//
//	The small image list is used, the	//
//	large is not, but is initialized	//
//	anyhow since they exist.			//
//										//
//////////////////////////////////////////
void CEWDPingDlg::InitImageList()
{

	//
	//		Create the image lists and add the icons
	//

	smallImageList.Create(16,16,FALSE,1,1);
	largeImageList.Create(32,32,FALSE,1,1);

	HICON TIcon;
	
	TIcon = LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_Blank));
	smallImageList.Add (TIcon);
	largeImageList.Add (TIcon);

	TIcon = LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_BlueArrow));
	smallImageList.Add (TIcon);
	largeImageList.Add (TIcon);

	//
	//		Assign the image lists to the list view
	//
	m_TraceList.SetImageList (&largeImageList, LVSIL_NORMAL);
	m_TraceList.SetImageList (&smallImageList, LVSIL_SMALL);
	
	//
	//		Initialize the columns and headings
	//
	AddListColumn (0, 
				   LVCFMT_RIGHT, 
				   m_TraceList.GetStringWidth("SEQXX")+5, 
				   "SEQ",
				   0);
	AddListColumn (1, 
				   LVCFMT_RIGHT, 
				   m_TraceList.GetStringWidth("TIMEXX")+5, 
				   "时间",
				   1);
	AddListColumn (2, 
				   LVCFMT_LEFT, 
				   m_TraceList.GetStringWidth("255.255.255.255.1")+10, 
				   "IP地址",
				   2);
	AddListColumn (3, 
				   LVCFMT_LEFT, 
				   m_TraceList.GetStringWidth("XXXXXXXXEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"), 
				   "信息", 
				   3);

}

//////////////////////////////////////////////
//											//
//		Create Column Headings/Widths		//
//											//
//////////////////////////////////////////////
int CEWDPingDlg::AddListColumn(int lcolumn, int lfmt, int lmaxwidth, LPSTR ltext, int lsubitem)
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -