// MyScanPortDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyScanPort.h"
#include "MyScanPortDlg.h"
#include "afxmt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BOOL REPORT_SUCCEED,REPORT_FAILED;
typedef struct ThreadParam
{
int StartPort;
int EndPort;
HWND hwnd;
byte a,b,c,d;
}THREADPARAM;
typedef struct PortParam
{
int iPort;
HWND hwnd;
byte a,b,c,d;
}PORTPARAM;
THREADPARAM threadparam1;
bool IsStop;
HANDLE g_Busy;
CString g_message;
bool ReleaseSmp;
CCriticalSection criticalSection;
int NowWhere;
UINT GetPortMessage(LPVOID pParam);
UINT MyThread(LPVOID pParam)
{
THREADPARAM* threadparam=(THREADPARAM*)pParam;
sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.S_un.S_un_b.s_b1=threadparam->a;
addr.sin_addr.S_un.S_un_b.s_b2=threadparam->b;
addr.sin_addr.S_un.S_un_b.s_b3=threadparam->c;
addr.sin_addr.S_un.S_un_b.s_b4=threadparam->d;
for(int i=threadparam->StartPort;(i<threadparam->EndPort)&&(!IsStop)&&(NowWhere<=threadparam->EndPort);i++)
{
SOCKET sck_Conn;
sck_Conn=socket(AF_INET,SOCK_STREAM,0);
if(sck_Conn==INVALID_SOCKET)
{
return 0;
}
criticalSection.Lock();
i=NowWhere;
NowWhere++;
criticalSection.Unlock();
addr.sin_port=htons(i);
sck_Conn=socket(AF_INET,SOCK_STREAM,0);
if(sck_Conn==INVALID_SOCKET)
{
return 0;
}
int ddd=connect(sck_Conn,(sockaddr*)&addr,sizeof(addr));
CString aa="";
char buffer[80];
strcpy(buffer,"");
if(ddd==SOCKET_ERROR)
{
if(REPORT_FAILED)
aa.Format("failed! port:%d\r\n",i);
}
else
{
if(REPORT_SUCCEED)
aa.Format("succeed! port:%d\r\n",i);
}
PORTPARAM *portparam=new PORTPARAM;
portparam->a=threadparam->a;
portparam->b=threadparam->b;
portparam->c=threadparam->c;
portparam->d=threadparam->d;
portparam->hwnd=threadparam->hwnd;
portparam->iPort=i;
AfxBeginThread(GetPortMessage,portparam,THREAD_PRIORITY_IDLE);
closesocket(sck_Conn);
if(g_message.GetLength()>30000)
g_message="";
g_message=aa+g_message;
if(i==threadparam->EndPort)
{
g_message="scan port complete!\r\n\r\n"+g_message;
}
::SetWindowText(threadparam->hwnd,g_message);
}
if(!ReleaseSmp)
{
if(ReleaseSemaphore(g_Busy,1,NULL))
ReleaseSmp=true;
}
return 0;
}
UINT GetPortMessage(LPVOID pParam)
{
PORTPARAM* portparam1=(PORTPARAM*)pParam;
sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.S_un.S_un_b.s_b1=portparam1->a;
addr.sin_addr.S_un.S_un_b.s_b2=portparam1->b;
addr.sin_addr.S_un.S_un_b.s_b3=portparam1->c;
addr.sin_addr.S_un.S_un_b.s_b4=portparam1->d;
addr.sin_port=htons(portparam1->iPort);
SOCKET sck_Conn;
sck_Conn=socket(AF_INET,SOCK_STREAM,0);
if(sck_Conn==INVALID_SOCKET)
{
delete pParam;
return 0;
}
sck_Conn=socket(AF_INET,SOCK_STREAM,0);
int ddd=connect(sck_Conn,(sockaddr*)&addr,sizeof(addr));
if(ddd==SOCKET_ERROR)
{
closesocket(sck_Conn);
delete pParam;
return 0;
}
char buffer[256];
strcpy(buffer,"");
ddd=recv(sck_Conn,buffer,256,0);
if(ddd==SOCKET_ERROR)
{
closesocket(sck_Conn);
delete pParam;
return 0;
}
if(strcmp(buffer,"")!=0)
{
CString aa="";
for(int i=0;i<256;i++)
{
if(buffer[i]>128||buffer[i]<0)
{
buffer[i]='\0';
break;
}
}
aa.Format("port:%d is %s\r\n",portparam1->iPort,buffer);
closesocket(sck_Conn);
if(g_message.GetLength()>30000)
{
g_message="";
}
g_message=g_message+aa;
::SetWindowText(portparam1->hwnd,g_message);
}
delete pParam;
return 0;return 0;
}
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CMyScanPortDlg dialog
CMyScanPortDlg::CMyScanPortDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyScanPortDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyScanPortDlg)
m_endport = 65536;
m_startport = 0;
m_message = _T("");
m_threadnum = 1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
IsStop=false;
g_Busy=CreateSemaphore(NULL,1,1,NULL);
g_message="";
NowWhere=0;
ReleaseSmp=false;
REPORT_SUCCEED=true;
REPORT_FAILED=false;
}
void CMyScanPortDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyScanPortDlg)
DDX_Control(pDX, IDC_IPADDRESS, m_ipaddress);
DDX_Text(pDX, IDC_ENDPORT, m_endport);
DDX_Text(pDX, IDC_STARTPORT, m_startport);
DDX_Text(pDX, IDC_MESSAGE, m_message);
DDX_Text(pDX, IDC_THREADNUM, m_threadnum);
//}}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_START, OnStart)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_BN_CLICKED(IDC_CLOSE, OnClose)
//}}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
GetDlgItem(IDC_STOP)->EnableWindow(false);
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
vc++编的多线程扫描端口
版权申诉
150 浏览量
2022-07-01
22:59:56
上传
评论
收藏 29KB RAR 举报
N201871643
- 粉丝: 625
- 资源: 2241
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈