?? ewdpingdlg.cpp
字號:
//////////////////////////////////////////
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;
//調(diào)用SENDPING函數(shù)發(fā)送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 ();
//設(shè)定TTL為0
icmpPingTTL = 0;
PingSocket.icmpHops = 0;
//圖標(biāo)狀態(tài)為0
IconState = 0;
PingSocket.icmpCurSeq = 0;
//設(shè)定地址
TraceTarget.s_addr = HostIPAddress();
CString TComment;
TComment.Format("Trace Target = %s",
inet_ntoa(TraceTarget));
//活動TRACE的信息
TraceComment (TComment);
icmpTracing = TRUE;
//調(diào)用SendPing函數(shù)
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 //
// //
//////////////////////////////////////////////////
//發(fā)送PING消息包到目的主機(jī)
void CEWDPingDlg::SendPing(void)
{
PingSent = TRUE;
PingSocket.icmpCurSeq++;
PingSocket.icmpCurId = (USHORT)GetCurrentProcessId();
PingSocket.icmpHostAddress = HostIPAddress();
//如果目標(biāo)地址為空,則返回
if (PingSocket.icmpHostAddress == NULL)
return;
//如果icmpTracing為真,則必須將
if (icmpTracing)
{
icmpPingTTL++;
}
//設(shè)定TTL
if (PingSocket.SetTTL (icmpPingTTL) == SOCKET_ERROR)
{
PingSocket.DisplayError ("setsocket(TTL)",
"CEWDPingDlg::SendPing");
return;
}
//要設(shè)定一定的時間讓TTL設(shè)定完成
Sleep (100);
//顯示序列號,同時用藍(lán)色尖頭表示出來
SetTraceSequence (PingSocket.icmpCurSeq,
m_TraceList.GetItemCount(),
Icon_BlueArrow);
//設(shè)定地址
PingSocket.icmpSockAddr.sin_family = PF_INET;
PingSocket.icmpSockAddr.sin_addr.s_addr = PingSocket.icmpHostAddress;
PingSocket.icmpSockAddr.sin_port = 0;
//開始計時
StartTimer();
//調(diào)用PING函數(shù)發(fā)出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)
{
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -