📄 findipdlg.cpp
字号:
// FindIpDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SnmpManager.h"
#include "FindIpDlg.h"
#include "icmp.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFindIpDlg dialog
CFindIpDlg::CFindIpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFindIpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFindIpDlg)
//}}AFX_DATA_INIT
}
void CFindIpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFindIpDlg)
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Control(pDX, IDC_IPADDRESS2, m_IP2);
DDX_Control(pDX, IDC_IPADDRESS1, m_IP1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFindIpDlg, CDialog)
//{{AFX_MSG_MAP(CFindIpDlg)
ON_BN_CLICKED(IDC_OK, OnOk)
ON_BN_CLICKED(IDC_CANCAL, OnCancal)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFindIpDlg message handlers
void CFindIpDlg::OnOk()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
WSADATA wsaData;
SOCKET sockRaw = INVALID_SOCKET;
int b = m_list.GetCount();
for (int i = 0; i < b; i++)
{
int a = 0;
m_list.DeleteString(a);
}
// TODO: Add your control notification handler code here
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
//printf("WSAStartup() failed: %d\n", GetLastError());
AfxMessageBox("Cannt open TCP/IP!!!");
return;
}
sockRaw = WSASocket (AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0,
WSA_FLAG_OVERLAPPED);
if (sockRaw == INVALID_SOCKET)
{
AfxMessageBox("Cannt open TCP/IP!!!");
//printf("WSASocket() failed: %d\n", WSAGetLastError());
return ;
}
//m_list.AddString("正在运行.....");
sockaddr_in from;
DWORD ip, ipmask;
if( m_IP1.IsBlank() || m_IP2.IsBlank())
AfxMessageBox("Please input IP Address or Mask!!");
else
{
m_IP1.GetAddress(ip);
m_IP2.GetAddress(ipmask);
int i=0;
while(ipmask!=0)
{
ipmask = ipmask<<1;
i++;
}
i = 32 - i;
int count = 0;
double a = 2.0;
double b = i;
int start = 1;
if(i == 0)
start = 0;
else
{
i = pow(a, b);
i -= 2;
}
while(start <= i)
{
DWORD address = ip + start;
if(ping(sockRaw, address, &from) == 0)
{
u_char a, b, c, d;
count++;
char* s1, *s2, *s3;
s1 = new char[4];
s2 = new char [16];
s3 = new char[2];
s3[0] = '.';
s3[1] = '\0';
a=from.sin_addr.S_un.S_un_b.s_b1;
b=from.sin_addr.S_un.S_un_b.s_b2;
c=from.sin_addr.S_un.S_un_b.s_b3;
d=from.sin_addr.S_un.S_un_b.s_b4;
sprintf(s1, "%d", a);
strcpy(s2, s1);
strcat(s2, s3);
sprintf(s1, "%d", b);
strcat(s2, s1);
strcat(s2, s3);
sprintf(s1, "%d", c);
strcat(s2, s1);
strcat(s2, s3);
sprintf(s1, "%d", d);
strcat(s2, s1);
m_list.AddString(s2);
delete s1;
delete s2;
delete s3;
}
start++;
}
if(count==0)
m_list.AddString("未发现设备!");
}
//ping(sockRaw,3544634602,&from);
if (sockRaw != INVALID_SOCKET)
closesocket(sockRaw);
WSACleanup();
}
void CFindIpDlg::OnCancal()
{
// TODO: Add your control notification handler code here
EndDialog(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -