网页素材: 导航菜单选项卡滑动门焦点图图片切换下拉菜单弹出层Tabs链接提示

您的当前位置: 源码爱好者 » 网页制作教程(编程教程) » VC++技术教程

vc++检测端口的小程序,扫描端口

  • 标签:扫描端口 更新时间:2014-07-31
  • VC++扫描端口,以下是主要的程序代码,扫描到的端口将列出端口号、端口状态等信息:

    BOOL CPortScanDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    	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);
    		}
    	}
    	SetIcon(m_hIcon, TRUE);
    	SetIcon(m_hIcon, FALSE);
    	m_List.SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_GRIDLINES|LVS_EX_TRACKSELECT);
    	m_List.InsertColumn(0,"端口号",LVCFMT_LEFT,120);
    	m_List.InsertColumn(1,"端口状态",LVCFMT_LEFT,120);
    	return TRUE; 
    }
    void CPortScanDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    	{
    		CAboutDlg dlgAbout;
    		dlgAbout.DoModal();
    	}
    	else
    	{
    		CDialog::OnSysCommand(nID, lParam);
    	}
    }
    void CPortScanDlg::OnPaint()
    {
    	if (IsIconic())
    	{
    		CPaintDC dc(this);
    		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
    		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;
    		dc.DrawIcon(x, y, m_hIcon);
    	}
    	else
    	{
    		CDialog::OnPaint();
    	}
    }
    HCURSOR CPortScanDlg::OnQueryDragIcon()
    {
    	return (HCURSOR) m_hIcon;
    }
    void CPortScanDlg::OnOK()
    {
    }
    DWORD WINAPI ThreadProc( LPVOID lpParameter )
    {
    	CPortScanDlg* pDlg = (CPortScanDlg* )lpParameter;
    	memset(&pDlg->addr,0,sizeof(SOCKADDR_IN));
    	pDlg->addr.sin_addr.S_un.S_addr = inet_addr(pDlg->IP.GetBuffer(0));
    	pDlg->addr.sin_family =AF_INET ;
    	int from = atoi(pDlg->m_From);
    	int end  = atoi(pDlg->m_EndPort);
    	for (int port = from; port <end; port++)
    	{
    		pDlg->m_Progress.SetPos(port);
    		pDlg->sock =  socket(AF_INET,SOCK_STREAM,0);
    		pDlg->addr.sin_port = htons(port) ;
    		pDlg->str.Format("%d",port);
    		if (connect(pDlg->sock,(sockaddr*)&pDlg->addr,sizeof(SOCKADDR_IN))==0)
    		{
    			pDlg->m_List.InsertItem(0,"");
    			pDlg->m_List.SetItemText(0,0,pDlg->str);
    			pDlg->m_List.SetItemText(0,1,"占用");
    		}
    		else
    		{
    			pDlg->m_List.InsertItem(0,"");
    			pDlg->m_List.SetItemText(0,0,pDlg->str);
    			pDlg->m_List.SetItemText(0,1,"未用");
    		}
    		closesocket(pDlg->sock);
    		if (pDlg->m_ThreadExit)
    		{
    			ExitThread(0);
    		}
    	}
    	AfxMessageBox("扫描完成");
    	return 0;
    }
    void CPortScanDlg::OnStart()
    {
    	UpdateData();
    	m_IP.GetWindowText(IP);
    	m_Progress.SetRange(0,80);
    	m_Progress.SetPos(0);
    	DWORD ThreadID;
    	m_ThreadExit = FALSE;
    	m_List.DeleteAllItems();
    	m_hTread = CreateThread(NULL,0,ThreadProc,(LPVOID)this,0,&ThreadID);
    }
    void CPortScanDlg::OnStop()
    {
    	m_Progress.SetPos(0);
    	m_ThreadExit = TRUE;
    	closesocket(sock);
    }
    void CPortScanDlg::OnCancel()
    {
    	CDialog::OnCancel();
    }

    VC++端口扫描完整源码在此下载:PortScan VC端口扫描

  • 已读: 次 收藏本文关闭本文打印本文复制链接
处理 SSI 文件时出错