// pingDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ping.h"
#include "pingDlg.h"
#include <winsock.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern SOCKET m_socket;
BOOL ping(const char *ipaddr, char *resp);
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CPingDlg dialog
CPingDlg::CPingDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPingDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPingDlg)
m_Ctx = _T("");
m_Num = 0;
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPingDlg)
DDX_Control(pDX, IDC_IPADDRESS1, m_IP);
DDX_Text(pDX, IDC_EDIT1, m_Ctx);
DDX_Text(pDX, IDC_EDIT_NUM, m_Num);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPingDlg, CDialog)
//{{AFX_MSG_MAP(CPingDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OK, OnOk)
ON_BN_CLICKED(IDC_START, OnStart)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPingDlg message handlers
BOOL CPingDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_IP.SetWindowText("127.0.0.1");
//////////////////////////////////////////////////////
int res;
int timeout = 100;
WSADATA wsaData;
//启动WINDOWS SOCK,版本1.0
if(WSAStartup( MAKEWORD( 1, 0 ), &wsaData ) != 0)
{
MessageBox("Window Socket 初始化失败!");
return FALSE;
}
//创建SOCKET
// AF_INET:协议族为TCP/IP
//SOCK_RAW:原始方式(直接与IP层交换数据)
//IPPROTO_ICMP:子协议类型
m_socket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if(m_socket == INVALID_SOCKET)
{
MessageBox("创建 Socket 失败!");
return FALSE;
}
// 设置选项:接受超时为100ms
res = setsockopt(m_socket,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,sizeof(timeout));
if(res == SOCKET_ERROR)
{
MessageBox("设置接受超时为100ms 失败!");
}
timeout = 1000;
// 设置发送超时为1000ms
res = setsockopt(m_socket,SOL_SOCKET,SO_SNDTIMEO,(char*)&timeout,sizeof(timeout));
if(res == SOCKET_ERROR)
{
MessageBox("设置发送超时为100ms 失败!");
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CPingDlg::OnOK()
{
}
void CPingDlg::OnCancel()
{
}
void CPingDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CPingDlg::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();
}
}
HCURSOR CPingDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPingDlg::OnOk()
{
CDialog::OnOK();
}
BOOL CPingDlg::DestroyWindow()
{
closesocket(m_socket);
WSACleanup ();
return CDialog::DestroyWindow();
}
///////////////////////////////////////////////////////////
void CPingDlg::OnStart()
{
char ipaddr[32];
char resp[4096];
UINT n,ok = 0;
float loss;
UpdateData(TRUE);
if(m_Num <= 0)
return;
m_IP.GetWindowText(ipaddr,30);
sprintf(resp,"\r\nping %s...\r\n",ipaddr);
AppendMsg(resp);
for(n = 0; n < m_Num; n++)
{
if(ping(ipaddr,resp))
ok++;
AppendMsg(resp);
Sleep(1000);
}
if(ok == 0) loss = 100.0f;
else loss = 100.0f - float(n * 100.0f / ok);
sprintf(resp,"\r\nPing %s 的统计结果:\r\n\t发送 = %d 接收 = %d 丢失 = %.2f%",ipaddr,n,ok,loss);
AppendMsg(resp);
}
void CPingDlg::AppendMsg(const char *msg)
{
CEdit *pEdit;
if(m_Ctx.GetLength() > 2048)
m_Ctx.Empty();
m_Ctx += msg;
m_Ctx += "\r\n";
UpdateData(FALSE);
pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);
pEdit->LineScroll(100,0);
}