📄 netdlg.cpp
字号:
// NetDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "PortMon.h"
#include "ping.h"
#include "whois.h"
#include ".\netdlg.h"
UINT ThreadProc(LPVOID pParam)
{
CNetDlg *pDlg = reinterpret_cast<CNetDlg *>(pParam);
if( !pDlg )
{
return -1;
}
switch(pDlg->m_nNetOper)
{
case NET_OPER_PING:
pDlg->PingProc();
break;
case NET_OPER_TRACERT:
pDlg->TracertProc();
break;
case NET_OPER_WHOIS:
pDlg->WhoisProc();
break;
default:
break;
}
return 0;
}
// CNetDlg 对话框
IMPLEMENT_DYNAMIC(CNetDlg, CBCGPDialog)
CNetDlg::CNetDlg(CWnd* pParent /*=NULL*/)
: CBCGPDialog(CNetDlg::IDD, pParent)
, m_pNetThread(NULL)
, m_nNetOper(NET_OPER_PING)
, m_sHostName(_T(""))
{
}
CNetDlg::~CNetDlg()
{
if( m_pNetThread )
{
delete m_pNetThread;
}
}
void CNetDlg::DoDataExchange(CDataExchange* pDX)
{
CBCGPDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LSTREMOTEHOST, m_lstReply);
}
BEGIN_MESSAGE_MAP(CNetDlg, CBCGPDialog)
ON_BN_CLICKED(IDC_BTNSTOP, OnBnClickedBtnStop)
ON_BN_CLICKED(IDC_EXIT, OnBnClickedExit)
ON_MESSAGE(WM_USER_REPLY, OnNetReply)
ON_WM_CLOSE()
END_MESSAGE_MAP()
// CNetDlg 消息处理程序
BOOL CNetDlg::OnInitDialog()
{
CBCGPDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
switch( m_nNetOper )
{
case NET_OPER_PING:
{
m_lstReply.InsertColumn(0, "#", LVCFMT_LEFT, 40);
m_lstReply.InsertColumn(1, "目的IP", LVCFMT_LEFT, 100);
m_lstReply.InsertColumn(2, "响应时间", LVCFMT_LEFT, 80);
}
break;
case NET_OPER_TRACERT:
{
m_lstReply.InsertColumn(0, "#", LVCFMT_LEFT, 40);
m_lstReply.InsertColumn(1, "主机IP", LVCFMT_LEFT, 100);
m_lstReply.InsertColumn(2, "主机名", LVCFMT_LEFT, 80);
m_lstReply.InsertColumn(3, "平均响应", LVCFMT_LEFT, 80);
m_lstReply.InsertColumn(4, "最快响应", LVCFMT_LEFT, 80);
m_lstReply.InsertColumn(5, "最慢响应", LVCFMT_LEFT, 80);
}
break;
case NET_OPER_WHOIS:
{
m_lstReply.InsertColumn(0, "主机信息", LVCFMT_LEFT, 200);
m_lstReply.InsertItem(0, "请稍候...");
}
break;
default:
break;
}
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CNetDlg::PostNcDestroy()
{
// TODO: 在此添加专用代码和/或调用基类
delete this;
CBCGPDialog::PostNcDestroy();
}
void CNetDlg::OnBnClickedBtnStop()
{
// TODO: 在此添加控件通知处理程序代码
m_evtStop.SetEvent();
}
void CNetDlg::OnBnClickedExit()
{
// TODO: 在此添加控件通知处理程序代码
SendMessage(WM_CLOSE);
}
void CNetDlg::OnClose()
{
DWORD dwCode = 0;
// notify thread to close
m_evtStop.SetEvent();
m_pNetThread->SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL);
Sleep(0); // let thread transfering take place
// TODO: 在此添加消息处理程序代码和/或调用默认值
if( ::GetExitCodeThread(m_pNetThread->m_hThread, &dwCode) )
{
if( dwCode == STILL_ACTIVE )
{
PostMessage(WM_CLOSE); // PostMessage give other messages have a chance
return;
}
}
CBCGPDialog::OnClose();
}
int CNetDlg::PingProc(void)
{
PINGREPLY_S reply;
CPing pinger;
int nTimes = 0;
while( (nTimes < DEFAULT_PING_TIMES) && (WaitForSingleObject(m_evtStop, 0) != WAIT_OBJECT_0) )
{
ZeroMemory( &reply, sizeof(PINGREPLY_S) );
if( pinger.Ping(m_sHostName, &reply) != -1 )
{
SendMessage( WM_USER_REPLY, NET_OPER_PING, (LPARAM)&reply );
}
else
{
SendMessage( WM_USER_REPLY, NET_OPER_PING, (LPARAM)NULL );
}
Sleep(1000);
nTimes++;
}
return 0;
}
int CNetDlg::TracertProc(void)
{
CPing pinger;
int nError, nTimes = 0;
while( (nTimes < DEFAULT_TRACERT_HOPS) && (WaitForSingleObject(m_evtStop, 0) != WAIT_OBJECT_0) )
{
// this memory should free by OnNetReply()
PINGREPLY_S *pReply = new PINGREPLY_S;
ZeroMemory( pReply, sizeof(PINGREPLY_S) );
pinger.Ping(m_sHostName, pReply, DEFAULT_TRACERT_PINGS, nTimes+1);
nError = pReply->nError;
if( nError != -1 )
{
PostMessage( WM_USER_REPLY, NET_OPER_TRACERT, (LPARAM)pReply );
if( nError == 0 ) // reach the target now, stop try
{
break;
}
}
else
{
delete pReply; // ping error message, just free it
}
// pReply may be freed by OnNetReply() here after, do not use it
nTimes++;
}
return 0;
}
int CNetDlg::WhoisProc(void)
{
CWhoIs whois;
CString answer = whois.GetWhoIs(m_sHostName);
SendMessage( WM_USER_REPLY, NET_OPER_WHOIS, (LPARAM)answer.GetBuffer() );
answer.ReleaseBuffer();
return 0;
}
LRESULT CNetDlg::OnNetReply( WPARAM wParam, LPARAM lParam )
{
CString sTemp;
int nItem;
DWORD dwIP;
switch( wParam )
{
case NET_OPER_PING:
{
PINGREPLY_S *pstReply = (PINGREPLY_S *)lParam;
nItem = m_lstReply.GetItemCount();
sTemp.Format( "%d", nItem+1 );
m_lstReply.InsertItem( nItem, sTemp );
if( pstReply )
{
dwIP = pstReply->Address.S_un.S_addr;
sTemp.Format( "%d.%d.%d.%d", dwIP&0xFF, (dwIP>>8)&0xFF, (dwIP>>16)&0xFF, (dwIP >> 24)&0xFF );
m_lstReply.SetItemText( nItem, 1, sTemp );
sTemp.Format( "%d ms", pstReply->avgRTT );
m_lstReply.SetItemText( nItem, 2, sTemp );
}
else
{
sTemp = _T("未知主机");
m_lstReply.SetItemText( nItem, 1, sTemp );
sTemp = _T("超时");
m_lstReply.SetItemText( nItem, 2, sTemp );
}
if( (nItem + 1) == DEFAULT_PING_TIMES )
{
m_lstReply.InsertItem( nItem+1, _T("*") );
}
AutoSizeColumns();
}
break;
case NET_OPER_TRACERT:
{
PINGREPLY_S *pstReply = (PINGREPLY_S *)lParam;
nItem = m_lstReply.GetItemCount();
sTemp.Format( "%d", nItem+1 );
m_lstReply.InsertItem( nItem, sTemp );
dwIP = pstReply->Address.S_un.S_addr;
sTemp.Format( "%d.%d.%d.%d", dwIP&0xFF, (dwIP>>8)&0xFF, (dwIP>>16)&0xFF, (dwIP >> 24)&0xFF );
m_lstReply.SetItemText( nItem, 1, sTemp );
hostent* hp = gethostbyaddr((const char *)&dwIP, 4, AF_INET);
if (!hp)
{
sTemp = "未知";
}
else
{
sTemp = hp->h_name;
}
m_lstReply.SetItemText( nItem, 2, sTemp );
sTemp.Format( "%d ms", pstReply->avgRTT );
m_lstReply.SetItemText( nItem, 3, sTemp );
ULONG nMinRTT = pstReply->minRTT;
if( nMinRTT == ULONG_MAX )
{
nMinRTT = 0;
}
sTemp.Format( "%d ms", nMinRTT );
m_lstReply.SetItemText( nItem, 4, sTemp );
sTemp.Format( "%d ms", pstReply->maxRTT );
m_lstReply.SetItemText( nItem, 5, sTemp );
if( pstReply->nError == 0 )
{
m_lstReply.InsertItem( nItem+1, _T("*") );
}
delete pstReply; // destroy the memory allocated by TracertProc()
AutoSizeColumns();
}
break;
case NET_OPER_WHOIS:
{
int nStart = 0;
int nRow = 0;
CString answer( (LPCTSTR)lParam );
if( answer.IsEmpty() )
{
m_lstReply.SetItemText(0, 0, _T("抱歉,暂时得不到对方信息"));
}
else
{
CString sTemp;
m_lstReply.DeleteItem(0); // remove "wait..." information
while (answer.Find('\n', nStart) != -1)
{
sTemp = answer.Mid( nStart, answer.Find('\n', nStart) - nStart );
m_lstReply.InsertItem( nRow, sTemp );
nRow++;
nStart = answer.Find('\n', nStart) + 1;
}
}
AutoSizeColumns();
}
break;
default:
break;
}
return 0;
}
void CNetDlg::AutoSizeColumns(void)
{
int maxcol = (m_lstReply.GetHeaderCtrl())->GetItemCount() - 1;
m_lstReply.SetRedraw(FALSE);
for (int col = 0; col <= maxcol; col++)
{
m_lstReply.SetColumnWidth(col, LVSCW_AUTOSIZE);
int wc1 = m_lstReply.GetColumnWidth(col);
m_lstReply.SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER);
int wc2 = m_lstReply.GetColumnWidth(col);
int wc = max(wc1, wc2);
m_lstReply.SetColumnWidth(col, wc);
}
m_lstReply.SetRedraw(TRUE);
m_lstReply.Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -