[C++] 纯文本查看 复制代码 int CConnectScan::scan(const char *IP,int start,int stop)
{
WORD wVersion;
WSADATA WsaData;
int time;
struct timeval timeout;
struct fd_set fdset;
wVersion = MAKEWORD(2, 2);
if (WSAStartup(wVersion, &WsaData) != 0) //初始化Winsock DLL
{
printf("未找到可用winsock.dll! \n");
return -1;
}
SOCKET PortScanSock = socket(AF_INET, SOCK_STREAM, 0);
unsigned long ul = 1;
if (ioctlsocket(PortScanSock, FIONBIO, (unsigned long*)&ul) == SOCKET_ERROR)
return 0;
SOCKADDR_IN ADDR;
ADDR.sin_family = AF_INET;
ADDR.sin_addr.S_un.S_addr = inet_addr(IP);
for (int Port = start; Port <= stop; Port++)
{
if (PortScanSock == NULL)
PortScanSock = socket(AF_INET, SOCK_STREAM, 0);
ADDR.sin_port = (Port);
connect(PortScanSock, (sockaddr*)&ADDR, sizeof(ADDR));
FD_ZERO(&fdset);
FD_SET(PortScanSock, &fdset);
timeout.tv_sec = 0;
timeout.tv_usec = 500 * 1000;
time = select(PortScanSock + 1, 0, &fdset, 0, &timeout);
PortDetil result;
result.PortNum = Port;
if (time <= 0)
{
result.OpenFlag = 0;
list.push_back(result);
}
else
{
result.OpenFlag = 1;
list.push_back(result);
closesocket(PortScanSock);
PortScanSock = NULL;
}
}
}
刚学网络编程不是很懂,select返回值一直为0,百度了一下没改过来==!好悲剧 |