多线程connect探测FTP失败,请帮忙!
DWORD __stdcall CConnectDlg::ThreadProc(PVOID lParam)
{
CConnectDlg* pWnd=(CConnectDlg*)AfxGetApp()-> m_pMainWnd;
char lpBuf[8192];
CString strTmp,strBuf;
sockaddr_in name;
SOCKET s;
int dwIndex,dwLine,iPos,dwRet,i;
u_long ul;
fd_set writefds;
TIMEVAL timeout;
while (!bStop)
{
EnterCriticalSection(&lpCriticalSection1);
if (iCount==MaxCount)
{ //用临界区,保证读取IP文本不重复
LeaveCriticalSection(&lpCriticalSection1);
break;
}
dwIndex=iCount;
iCount++;
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
LeaveCriticalSection(&lpCriticalSection1);
name.sin_family=AF_INET;
name.sin_port=htons(Port);
//从RichEdit文本框中读取指定行的IP
dwLine=pWnd-> m_RichRecord.GetLine(dwIndex,strTmp.GetBuffer(0),10000);
pWnd-> m_RichRecord.GetLine(dwIndex,strTmp.GetBuffer(dwLine),dwLine);
strTmp.ReleaseBuffer(dwLine);
iPos=strTmp.Find( "\r\n ");
if (-1==iPos)
{
name.sin_addr.S_un.S_addr=inet_addr(strTmp);
}
else
{
name.sin_addr.S_un.S_addr=inet_addr(strTmp.Mid(0,iPos));
}
ul=1;
ioctlsocket(s,FIONBIO,&ul);
connect(s,(sockaddr*)&name,sizeof(name));
FD_ZERO(&writefds);
FD_SET(s,&writefds);
timeout.tv_sec=3; //设置connect 3秒超时
timeout.tv_usec=0;
dwRet=select(0,NULL,&writefds,NULL,&timeout);
if (0> =dwRet)
{
closesocket(s);
Sleep(300);
continue;
}
ul=0;
ioctlsocket(s,FIONBIO,&ul);
setsockopt(s,SOL_SOCKET,SO_RCVTIMEO,(char*)&tRecv,sizeof(tRecv));
if (recv(s,lpBuf,sizeof(lpBuf),0)> 0)
{
strBuf=(CString)lpBuf;
strBuf.MakeLower();
if (-1!=strBuf.Find( "ftp "))
{
//
}
}
closesocket(s);
Sleep(300);
}
}
当RichEdit里面有几万个IP的时候,创建50个线程探测,
for (int i=0;i <50;i++)
{
CloseHandle(CreateThread(NULL,0,ThreadProc,NULL,0,NULL));
}
和用单线程, 返回的结果有非常大的区别? 请问代码哪里有问题?
[解决办法]
關注...