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

📄 testnetneidlg.cpp

📁 本代码是测试网络链接
💻 CPP
字号:
// testNetNeiDlg.cpp : implementation file
//

#include "stdafx.h"
#include "testNetNei.h"
#include "testNetNeiDlg.h"

#include <afxsock.h>
#include "iphlpapi.h"

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

//=================================定义全局变量
typedef  struct  ipmac  
{  
	char  szIp[16];  
	char  szMac[6];  
}IPMAC;  
IPMAC  g_Host[255];  
int  g_K=0;  
CRITICAL_SECTION  cs;  

/////////////////////////////////////////////////////////////////////////////
// CTestNetNeiDlg dialog

CTestNetNeiDlg::CTestNetNeiDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestNetNeiDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestNetNeiDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestNetNeiDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestNetNeiDlg)
	DDX_Control(pDX, IDC_LIST_LOCAL_HOST, m_ipList);
	DDX_Control(pDX, IDC_IPADDRESS1, m_ctrlIP);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestNetNeiDlg, CDialog)
	//{{AFX_MSG_MAP(CTestNetNeiDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestNetNeiDlg message handlers

BOOL CTestNetNeiDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CTestNetNeiDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestNetNeiDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTestNetNeiDlg::OnButtonStart() 
{
	HANDLE  t_hthread[254];  
	CString t_IpSuffix,t_sIP;
	m_ctrlIP.GetWindowText(t_sIP);
	t_IpSuffix = t_sIP.Left(t_sIP.ReverseFind('.')+1);
	CString  t_strIp[254];    
	InitializeCriticalSection(&cs);
	for(int i=0;i<254;i++)
	{
		t_strIp[i].Format("%d",i+1);  
		t_strIp[i]=t_IpSuffix+t_strIp[i];  
		t_hthread[i]=CreateThread(NULL,0,ArpThread,t_strIp[i].GetBuffer(0),0,NULL);    
	}
	/*呵呵,因为一次只能等待  64个内核对象,所以只有分几次了*/  
	/*当然也可以用循环了*/  
	WaitForMultipleObjects(64,t_hthread,TRUE,INFINITE);  
	WaitForMultipleObjects(64,&t_hthread[64],TRUE,INFINITE);  
	WaitForMultipleObjects(64,&t_hthread[128],TRUE,INFINITE);  
	WaitForMultipleObjects(62,&t_hthread[192],TRUE,INFINITE);  
	DeleteCriticalSection(&cs);  
	CString  t_sTest;  
	for(i=0;i<g_K;i++)  
	{  
		PBYTE  t_pmac=(PBYTE)g_Host[i].szMac;  
		t_sTest.Format("%s(%02x-%02x-%02x-%02x-%02x-%02x)",g_Host[i].szIp,t_pmac[0],t_pmac[1],t_pmac[2],t_pmac[3],t_pmac[4],t_pmac[5]);  
		m_ipList.AddString(t_sTest);
	}
}

/*线程函数,用来查询IP对应的MAC地址*/  
DWORD  WINAPI  ArpThread(LPVOID  lParam)  
{  
	char * szIp=(char  *)lParam;  
	ULONG  pMac[2];  
	ULONG  pulen=6;  
	int  ret;  
	TRACE(szIp);  
	if  ((ret=SendARP(inet_addr(szIp),0,pMac,&pulen))==0)  
	{  
		EnterCriticalSection(&cs);  //多线程同步,呵呵:0  
		strcpy(g_Host[g_K].szIp,szIp);  
		PBYTE  pbyte=(PBYTE)pMac;  
		for  (int  i=0;i<5;i++)  
		{  
			g_Host[g_K].szMac[i]=pbyte[i];  
			TRACE("%02X-",pbyte[i]);  
		}  
		TRACE("%02X",pbyte[5]);  
		g_Host[g_K].szMac[5]=pbyte[5];  
		g_K++;  
		LeaveCriticalSection(&cs);  
		
	}  
	else  
	{  
		CString t_s;    t_s.Format("SendARP errorCode = %d",ret);
		::OutputDebugString(t_s.LockBuffer());
	}  
	return  0;  
}

⌨️ 快捷键说明

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