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

📄 myscanportdlg.cpp

📁 扫描目标主机的各个端口
💻 CPP
字号:
// myscanportDlg.cpp : implementation file
//

#include "stdafx.h"
#include "myscanport.h"
#include "myscanportDlg.h"
#include "afxsock.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
short g_nThreadCount = 0;


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()

/////////////////////////////////////////////////////////////////////////////
// CMyscanportDlg dialog

CMyscanportDlg::CMyscanportDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyscanportDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyscanportDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyscanportDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyscanportDlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Control(pDX, IDC_START, m_start);
	DDX_Control(pDX, IDC_END, m_end);
	DDX_Control(pDX, IDC_IP, m_ip);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyscanportDlg, CDialog)
	//{{AFX_MSG_MAP(CMyscanportDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SCAN, OnScan)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyscanportDlg message handlers






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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;	
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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

	WSADATA wsaData;	
	BOOL ret = WSAStartup(MAKEWORD(2,2), &wsaData);
	if(ret != 0)
	{
		MessageBox("初始化网络协议失败!");
		return -1;
	}	
	m_ip.SetWindowText("127.0.0.1");
	m_start.SetWindowText("1");
    m_end.SetWindowText("1024");
    m_thinfo1=new threadinfo;

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyscanportDlg::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 CMyscanportDlg::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 CMyscanportDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyscanportDlg::OnScan() 
{

	// TODO: Add your control notification handler code here
	//获取IP
	m_list.ResetContent();
	CString  scanip,startport,endport;
	m_ip.GetWindowText(scanip);
	m_start.GetWindowText(startport);
	m_end.GetWindowText(endport);
	if(scanip.IsEmpty()||startport.IsEmpty()||endport.IsEmpty())
	{
		MessageBox("请输入完整信息!!!!");
		return;
	}
	int  startp =atoi(startport);
	int endp =atoi(endport);
	m_thinfo1->start= startp;
	m_thinfo1->end=endp;
	m_thinfo1->ip=scanip;
	m_thinfo1->list=(CListBox*)GetDlgItem(IDC_LIST1);
	DWORD ThreadId;
    HANDLE  hThread1=CreateThread( NULL, 0, ThreadProc1, (LPVOID)m_thinfo1, 0, &ThreadId);
	CloseHandle(hThread1);	
}

DWORD WINAPI CMyscanportDlg::ThreadProc(LPVOID lParam)
{
	g_nThreadCount++;
	threadinfo*pt=(threadinfo*)lParam;
	char address[200];
	SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
	
	if(sock == INVALID_SOCKET)
	{
		AfxMessageBox("创建套接字失败!");
		return 0;
	}
	
	strcpy(address,pt->ip);
	sockaddr_in severAddr;
	severAddr.sin_family = AF_INET;
	severAddr.sin_port = htons(pt->port); 		
	severAddr.sin_addr.S_un.S_addr =inet_addr(address);
    int res=connect(sock, (sockaddr*)&severAddr, sizeof(severAddr));
	if(res!=SOCKET_ERROR)
	{
		CString temp;
		temp.Format("%d",pt->port);
		pt->list->InsertString(-1,temp);
		
	}
	closesocket(sock);
	g_nThreadCount--;
	return 1;
}

DWORD WINAPI CMyscanportDlg::ThreadProc1(LPVOID lparam)
{
	
	threadinfo*	p=(threadinfo*)lparam;
	for(int i=p->start;i<=p->end;i++)
	{
		threadinfo* m_thinfo=new threadinfo;
		m_thinfo->ip=p->ip;
		m_thinfo->list=p->list;
		m_thinfo->port=i;
		DWORD ThreadId;
		if(g_nThreadCount>200)
		{
			Sleep(10);
		}
		HANDLE  m_hThread=CreateThread( NULL, 0, ThreadProc, (LPVOID)m_thinfo, 0, &ThreadId);
        CloseHandle(m_hThread);	
	
	}
	AfxMessageBox("扫描结束!");	
	return 1;
	
}

void CMyscanportDlg::OnDestroy() 
{
	delete m_thinfo1;
	WSACleanup();
	CDialog::OnDestroy();
}

⌨️ 快捷键说明

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