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

📄 ewdpingdlg.cpp

📁 VC通信程序源代码.TCP/UDP高级编程,TCP,IP终端程序源代码等.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// EWDPingDlg.cpp : implementation file
//

//////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EWDPing.h"
#include "EWDPingDlg.h"
#include "PingOptionsDlg.h"

//////////////////////////////////////////////////////////////////////////////

#include "RegKey.h"
#include "SysTCPIP.h"

//////////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEWDPingDlg dialog

CEWDPingDlg::CEWDPingDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEWDPingDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEWDPingDlg)
	m_Host = _T("");
	m_LocalHost = _T("");
	m_NameServer = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEWDPingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEWDPingDlg)
	DDX_Control(pDX, IDC_TraceList, m_TraceList);
	DDX_Text(pDX, IDC_DEST, m_Host);
	DDX_Text(pDX, IDC_LocalHost, m_LocalHost);
	DDX_Text(pDX, IDC_NameServer, m_NameServer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEWDPingDlg, CDialog)
	//{{AFX_MSG_MAP(CEWDPingDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDQUIT, OnQuit)
	ON_BN_CLICKED(IDC_PingButton, OnPingButton)
	ON_BN_CLICKED(IDC_TraceButton, OnTraceButton)
	ON_BN_CLICKED(IDC_OptionsButton, OnOptionsButton)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_ClearButton, OnClearButton)
	ON_MESSAGE(WSA_PING_ASYNC, OnPINGAsynch)
	ON_BN_CLICKED(IDC_Stop, OnStop)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEWDPingDlg message handlers

//////////////////////////////////////////////////////////////////////////
//																		//
//						MESSAGE INTERRUPT PROCESSORS					//
//																		//
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////
//										//
//		Initialize main dialog			//
//										//
//////////////////////////////////////////

//对话框初始化
BOOL CEWDPingDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	CString strAboutMenu;
	strAboutMenu.LoadString(IDS_ABOUTBOX);
	if (!strAboutMenu.IsEmpty())
	{
		pSysMenu->AppendMenu(MF_SEPARATOR);
		pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
	}

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	PingSocket.icmpSocket = INVALID_SOCKET;
	PingSent = FALSE;

	FetchWinsockSettings();			// 获得机器名、域名的属性.

	if (!InitSockets())				// 如果初始化失败则退出
		CDialog::OnCancel();

	InitImageList();				// 设定列表框图标

	pIcmpBuffer = (LPSTR)icmpBuffer;// 设定缓冲区

	return TRUE; 
}

//////////////////////////////////////////////////
//												//
//		Control menu and max/min processor		//
//												//
//////////////////////////////////////////////////
void CEWDPingDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

//////////////////////////////////////////
//										//
//			Repaint window				//
//										//
//////////////////////////////////////////
void CEWDPingDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//////////////////////////////////////////
//										//
//		Called when icon is moving		//
//										//
//////////////////////////////////////////
HCURSOR CEWDPingDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//////////////////////////////////////////////////////
//													//
//		PING Asynchronous Message Processor			//
//													//
//	Responds to received ICMP messages				//
//		Updates the Trace window with the round		//
//		trip time and sequence,						//
//		Stops the timer,							//
//		If not TRACING, exits.						//
//													//
//		If rcvSockAddr = TraceTarget, displays		//
//			host reached and exits.					//
//		If icmpMaxHops exceeded, displays host not	//
//			reached and exits. (icmpMaxHops is set	//
//			in EWDPing Options Dialog).				//
//		Displays next animated icon picture,		//
//			increments icmpHops and requests next	//
//			PING									//
//													//
//////////////////////////////////////////////////////
LRESULT CEWDPingDlg::OnPINGAsynch(WPARAM wParam, LPARAM lParam)
{

	int WSAEvent = WSAGETSELECTEVENT(lParam);
	int WSAError = WSAGETSELECTERROR(lParam);

	if (WSAError)
	{
		CString ErrorMessage;
		ErrorMessage.Format("WSAAsynch Event # %u, Error: %u", 
							WSAEvent, 
							WSAError);
		MessageBox (ErrorMessage, 
				    "CEWDPingDlg::OnPINGAsynch", 
					MB_OK|MB_SYSTEMMODAL);
	}
	
	StopTimer();

	switch (WSAEvent)
	{
	case FD_READ:

		if (PingSocket.Receive(pIcmpBuffer, sizeof icmpBuffer) == SOCKET_ERROR)
		{
			if (!PingSent)
				return 0;

			PingSocket.DisplayError ("FD_READ", 
									 "CEWDPingDlg::OnWSAAsynch");
		}

		else
		{
			DWORD	PingRoundTrip = PingSocket.icmpPingReceivedAt - PingSocket.icmpPingSentAt;

			if (PingSocket.icmpRoundTripTime > PingRoundTrip)
				PingSocket.icmpRoundTripTime = PingRoundTrip;
			
			if (PingSent)
			{
				UpdateTrace();
			
				if (!icmpTracing)
				{
					m_TraceList.InsertItem (m_TraceList.GetItemCount(), 
											"", 
											Icon_BlueArrow);
					SetTraceFocus (m_TraceList.GetItemCount()-1,
								   0);
					DisplayTrace ("","","Host Reached");

				}
			}
		}

		PingSent = FALSE;
		
		if (icmpTracing)
		{
			PingSocket.icmpHops++;
			if ( (PingSocket.icmpHops >= PingSocket.icmpMaxHops) ||
				 (TraceTarget.s_addr == PingSocket.rcvSockAddr.sin_addr.s_addr ))
			{
				EndTrace();
				m_TraceList.InsertItem (m_TraceList.GetItemCount(), 
										"", 
										Icon_BlueArrow);
				SetTraceFocus (m_TraceList.GetItemCount()-1,
							   0);
				DisplayTrace ("","","HOST Reached");
			}
			else
			{
				ChangeIconState();
				SendPing();
			}
		}

		break;

	case FD_WRITE:
		break;

	default:
		break;

	}

	return 0;
}

//////////////////////////////////////////////////////
//													//
//					End Trace Operation				//
//													//
//		Set ICON to default, and force screen to	//
//		be updated.  Reset icmpTracing so next		//
//		operation will be PING.						//
//													//
//////////////////////////////////////////////////////
void CEWDPingDlg::EndTrace(void)
{
	CStatic *	pDlgWnd;

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

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

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

	icmpTracing = FALSE;

}

//////////////////////////////////////////////////////////////////////////
//																		//
//								TIMER SECTION							//
//																		//
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
//															//
//				Process Timer Interrupt						//
//															//
//		If time-out period has not elapsed, increment		//
//		icmpIntervalCount, change the animated icon's state	//
//		and exit.											//
//															//
//		Otherwise, stop the timer, reset icon to default,	//
//		set receive address to null, and update the trace	//
//		display to show that the timer has expired.			//
//															//
//////////////////////////////////////////////////////////////

//计时器事件
void CEWDPingDlg::OnTimer(UINT id)
{

	CString		Message;
	CStatic *	pDlgWnd;
	//如果没有超时则不断改变图标状态,并将计时变量增加
	if (icmpIntervalCount < icmpMaxIntervals)
	{
		ChangeIconState ();
		icmpIntervalCount++;
		return;
	}

	StopTimer();
	PingSent = FALSE;

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

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

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

	PingSocket.rcvSockAddr.sin_addr.s_addr = NULL;
	
	PingSocket.icmpRoundTripTime = PingSocket.icmpPingTimer * icmpMaxIntervals;
	UpdateTrace ();

	if (icmpTracing)
		SendPing();
}

//////////////////////////////////////////
//										//
//			Start the Timer				//
//										//
//	Start the timer and set TimerActive	//
//										//

⌨️ 快捷键说明

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