64,646
社区成员
发帖
与我相关
我的任务
分享
//main.c
#include <winsock2.h>
#include <stdio.h>
#include <process.h>
#include <errno.h>
#pragma comment(lib,"ws2_32.lib")
#include <stdlib.h>
#include <string.h>
#define PORT 80
int InitSock(void)
{
int err = 0;
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup(wVersionRequested, &wsaData);
if ( err != 0 )
{
return -1;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup();
return -1;
}
return 0;
}
int Hi_Setup_OpenTcp(struct sockaddr *SockAddr, int NameLen, int nTimeout)
{
int f = -1;
struct timeval tv;
fd_set writefds;
int len, error, s32Ret;
unsigned long non_blocking = 1;
unsigned long blocking = 0;
unsigned long reuse = 1;
if ((f = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
return -1;
}
tv.tv_sec = nTimeout / 1000;
tv.tv_usec = nTimeout % 1000;
ioctlsocket(f, FIONBIO, &non_blocking);
s32Ret = connect(f, SockAddr, NameLen);
if (s32Ret < 0)
{
FD_ZERO(&writefds);
FD_SET((unsigned int)f, &writefds);
if(select(f+1, NULL, &writefds, NULL, &tv) != 0)
{
if(FD_ISSET(f, &writefds))
{
len = sizeof(error);
//下面的一句一定要,主要针对防火墙
if(getsockopt(f, SOL_SOCKET, SO_ERROR, (char *)&error, &len) < 0)
goto error_ret;
if(error != 0)
goto error_ret;
}
else
goto error_ret; //timeout or error happen
}
else
goto error_ret;
ioctlsocket(f, FIONBIO, &blocking);
}
else
{
error_ret:
closesocket(f);
return -1;
}
return f;
}
/******************************************
* connect server
******************************************/
int HI_Setup_Connect(const char* pServer, int nPort, int nTimeout,
int *pSessSock)
{
int GetHost = -1;
struct sockaddr_in SockAddr;
unsigned int net_addr = 0;
struct hostent *Hostent = NULL;
Hostent = gethostbyname(pServer);
if (NULL != Hostent)
{
GetHost = 0;
}
else
{
net_addr = inet_addr(pServer);
if(INADDR_NONE == net_addr)
{
GetHost = -1;
}
else
{
Hostent = gethostbyaddr((const char*) &net_addr, sizeof(unsigned int), PF_INET);
if (Hostent)
{
GetHost = 0;
}
}
}
if (0 == GetHost)
{
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *(unsigned int *) Hostent->h_addr;
SockAddr.sin_port = htons(PORT);
*pSessSock = Hi_Setup_OpenTcp((struct sockaddr *) & SockAddr,
sizeof(SockAddr), nTimeout);
//if(strcmp(pServer, "192.168.1.22") == 0)
// printf("%s\n", pServer);
if (*pSessSock < 0)
{
GetHost = -1;
}
}
return GetHost;
}
char sHost[20][32] = {"192.168.1.22", "192.168.1.200",
"192.168.1.201", "192.168.1.202",
"192.168.1.203", "192.168.1.204",
"192.168.1.205", "192.168.1.206",
"192.168.1.207", "192.168.1.208",
"192.168.1.209", "192.168.1.210",
"192.168.1.211", "192.168.1.212",
"192.168.1.213", "192.168.1.214",
"192.168.1.215", "192.168.1.216",
"192.168.1.217", "192.168.1.218"};
int ConnectPross(int index)
{
printf("%s\n", sHost[index]);
while(1)
{
int nSock = -1;
int ret = HI_Setup_Connect(sHost[index], PORT, 5000, &nSock);
if(ret == -1)
{
if(index == 0)
{
printf("connect Fail:%s\n", sHost[index]);
}
}
else
{
closesocket(nSock);
if(index == 0)
{
printf("connected\n");
}
}
Sleep(5000);
}
return 0;
}
DWORD WINAPI ConnectThreadPross(LPVOID lpParameter)
{
int index = (int)lpParameter;
ConnectPross(index);
return 0;
}
int main(int argc, char* argv[])
{
int i = 0;
DWORD dwThreadID = 0;
HANDLE hThread = NULL;
InitSock();
for(i=0; i<20; i++)
{
dwThreadID = 0;
hThread = CreateThread(NULL, 0, ConnectThreadPross, (LPVOID)i, 0, &dwThreadID);
CloseHandle(hThread);
Sleep(10);
}
while(1)
{
Sleep(1000);
}
WSACleanup();
return 0;
}