代码是这样:
UINT CScanDlg::PortScan()
{
int port[60] = {0};
PortArrSet(port);
DWORD dwThrdParam;
CWinThread *pA = NULL;
HANDLE handle[60] = {0};
for(int i = 0; i < 60; i++)
{
dwThrdParam = (DWORD)port[i];
pA = AfxBeginThread(ThreadPortScan, &dwThrdParam);
handle[i] = pA->m_hThread;
Sleep(1);
}
WaitForMultipleObjects(60, handle, TRUE, INFINITE);
Sleep(1);
return 0;
}
11 个解决方案
#1
在线程入口函数中使用啊~你这个是什么函数
#2
这个是个端口扫描函数,针对每个不同的端口我都开了个线程……
#3
我使用了这个函数来等待线程,可是每次线程都没有结束完该函数就执行了,苦恼了一天啊!
#4
#define MAX_THREAD_COUNT (60)
HANDLE hThread[MAX_THREAD_COUNT ] = {0};
CWinThread* pThread[MAX_THREAD_COUNT] = {0};
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
pThread[i] = AfxBeginThread(ThreadPortScan, ... , CREATE_SUSPENDED, ...);
pThread->m_bAutoDelete = FALSE;
hThread[i] = pThread[i]->m_hThread;
pThread[i]->ResumeThread();
}
WaitForMultipleObjects(MAX_THREAD_COUNT, hThread, TRUE, INFINITE);
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
CloseHandle(hThread[i]);
hThread[i] = NULL;
delete pThread[i];
pThread[i] = NULL;
}
#5
这样写后程序直接锁死了~怎么回事?
#6
那的看你的线程函数中写了些什么?
#7
#define MAX_THREAD_COUNT (60)
HANDLE hThread[MAX_THREAD_COUNT ] = {0};
CWinThread* pThread[MAX_THREAD_COUNT] = {0};
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
pThread[i] = AfxBeginThread(ThreadPortScan, ... , CREATE_SUSPENDED, ...);
pThread->m_bAutoDelete = FALSE;
hThread[i] = pThread[i]->m_hThread;
pThread[i]->ResumeThread();
}
WaitForMultipleObjects(MAX_THREAD_COUNT, hThread, TRUE, INFINITE);
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
CloseHandle(hThread[i]);
hThread[i] = NULL;
delete pThread[i];
pThread[i] = NULL;
}
这样写后程序直接锁死了~怎么回事?
那的看你的线程函数中写了些什么?
函数是这个
UINT ThreadPortScan(LPVOID lp)
{
timeout.tv_sec = 1; //定义超时时间
int retal = 0;
SOCKET sockfd;
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == INVALID_SOCKET)
{
return 0;
}
unsigned long arg = 1;
retal = ioctlsocket(sockfd, FIONBIO, &arg);
sockaddr_in des;
des.sin_family = AF_INET;
des.sin_addr.s_addr =inet_addr(HostAddr);
int Port = *(int*)lp; //传递的是端口号
des.sin_port = htons(Port);
connect(sockfd, (struct sockaddr *)&des, sizeof(des));
FD_ZERO(&mask);
FD_SET(sockfd,&mask);
retal = select(0, NULL, &mask, NULL, &timeout);
if((retal == 0) || (retal == -1))
{
closesocket(sockfd);
}
else
{
closesocket(sockfd);
char buf[100];
sprintf(buf, "端口%d为开放状态", Port);
m_pDlg->PrintInfo(buf);//打印信息
}
return 0;
}
我用VC调的··
#8
照我自己之前的写法··我使用getlasterror后~得出的错误是句柄无效,可是不懂怎么解决啊…………
#9
你Debug调试一下你的线程函数,看看卡在哪里,为啥没有返回?
#10
你Debug调试一下你的线程函数,看看卡在哪里,为啥没有返回?
新建的线程有点多,单步调试搞得好乱,能加你QQ和你讨论一下么?
#11
http://blog.csdn.net/lyd_253261362/article/details/4450202
希望对你有帮助。
希望对你有帮助。
#1
在线程入口函数中使用啊~你这个是什么函数
#2
这个是个端口扫描函数,针对每个不同的端口我都开了个线程……
#3
我使用了这个函数来等待线程,可是每次线程都没有结束完该函数就执行了,苦恼了一天啊!
#4
#define MAX_THREAD_COUNT (60)
HANDLE hThread[MAX_THREAD_COUNT ] = {0};
CWinThread* pThread[MAX_THREAD_COUNT] = {0};
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
pThread[i] = AfxBeginThread(ThreadPortScan, ... , CREATE_SUSPENDED, ...);
pThread->m_bAutoDelete = FALSE;
hThread[i] = pThread[i]->m_hThread;
pThread[i]->ResumeThread();
}
WaitForMultipleObjects(MAX_THREAD_COUNT, hThread, TRUE, INFINITE);
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
CloseHandle(hThread[i]);
hThread[i] = NULL;
delete pThread[i];
pThread[i] = NULL;
}
#5
#define MAX_THREAD_COUNT (60)
HANDLE hThread[MAX_THREAD_COUNT ] = {0};
CWinThread* pThread[MAX_THREAD_COUNT] = {0};
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
pThread[i] = AfxBeginThread(ThreadPortScan, ... , CREATE_SUSPENDED, ...);
pThread->m_bAutoDelete = FALSE;
hThread[i] = pThread[i]->m_hThread;
pThread[i]->ResumeThread();
}
WaitForMultipleObjects(MAX_THREAD_COUNT, hThread, TRUE, INFINITE);
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
CloseHandle(hThread[i]);
hThread[i] = NULL;
delete pThread[i];
pThread[i] = NULL;
}
这样写后程序直接锁死了~怎么回事?
#6
#define MAX_THREAD_COUNT (60)
HANDLE hThread[MAX_THREAD_COUNT ] = {0};
CWinThread* pThread[MAX_THREAD_COUNT] = {0};
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
pThread[i] = AfxBeginThread(ThreadPortScan, ... , CREATE_SUSPENDED, ...);
pThread->m_bAutoDelete = FALSE;
hThread[i] = pThread[i]->m_hThread;
pThread[i]->ResumeThread();
}
WaitForMultipleObjects(MAX_THREAD_COUNT, hThread, TRUE, INFINITE);
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
CloseHandle(hThread[i]);
hThread[i] = NULL;
delete pThread[i];
pThread[i] = NULL;
}
这样写后程序直接锁死了~怎么回事?
那的看你的线程函数中写了些什么?
#7
#define MAX_THREAD_COUNT (60)
HANDLE hThread[MAX_THREAD_COUNT ] = {0};
CWinThread* pThread[MAX_THREAD_COUNT] = {0};
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
pThread[i] = AfxBeginThread(ThreadPortScan, ... , CREATE_SUSPENDED, ...);
pThread->m_bAutoDelete = FALSE;
hThread[i] = pThread[i]->m_hThread;
pThread[i]->ResumeThread();
}
WaitForMultipleObjects(MAX_THREAD_COUNT, hThread, TRUE, INFINITE);
for(size_t i = 0; i != MAX_THREAD_COUNT; ++i)
{
CloseHandle(hThread[i]);
hThread[i] = NULL;
delete pThread[i];
pThread[i] = NULL;
}
这样写后程序直接锁死了~怎么回事?
那的看你的线程函数中写了些什么?
函数是这个
UINT ThreadPortScan(LPVOID lp)
{
timeout.tv_sec = 1; //定义超时时间
int retal = 0;
SOCKET sockfd;
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == INVALID_SOCKET)
{
return 0;
}
unsigned long arg = 1;
retal = ioctlsocket(sockfd, FIONBIO, &arg);
sockaddr_in des;
des.sin_family = AF_INET;
des.sin_addr.s_addr =inet_addr(HostAddr);
int Port = *(int*)lp; //传递的是端口号
des.sin_port = htons(Port);
connect(sockfd, (struct sockaddr *)&des, sizeof(des));
FD_ZERO(&mask);
FD_SET(sockfd,&mask);
retal = select(0, NULL, &mask, NULL, &timeout);
if((retal == 0) || (retal == -1))
{
closesocket(sockfd);
}
else
{
closesocket(sockfd);
char buf[100];
sprintf(buf, "端口%d为开放状态", Port);
m_pDlg->PrintInfo(buf);//打印信息
}
return 0;
}
我用VC调的··
#8
照我自己之前的写法··我使用getlasterror后~得出的错误是句柄无效,可是不懂怎么解决啊…………
#9
你Debug调试一下你的线程函数,看看卡在哪里,为啥没有返回?
#10
你Debug调试一下你的线程函数,看看卡在哪里,为啥没有返回?
新建的线程有点多,单步调试搞得好乱,能加你QQ和你讨论一下么?
#11
http://blog.csdn.net/lyd_253261362/article/details/4450202
希望对你有帮助。
希望对你有帮助。