关于网友提出的“ 编写的端口扫描工具,扫非本机ip,一直超时??怎么回事啊”问题疑问,本网通过在网上对“ 编写的端口扫描工具,扫非本机ip,一直超时??怎么回事啊”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 编写的端口扫描工具,扫非本机ip,一直超时??怎么回事啊
描述: 代码如下
// PortOpenTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#if defined(_WIN32) || defined(_WIN64)
#include
#include
#endif
#include
#include
#include
#include "mysql.h"
#pragma comment(lib, "Ws2_32.lib")
BOOL IsPortOpen(char * address, int port)
{
int recv = 1;
WSADATA wsadata;
int fd;
struct sockaddr_in clientaddress;
struct hostent * host1;
BOOL Result = FALSE;
struct timeval timer4;
fd_set writefd;
ULONG value = 1;
WSAStartup(MAKEWORD(1,1), &wsadata); //套接字库请求
fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); //返回套接字描述符
ioctlsocket(fd,FIONBIO,&value); //控制套接口模式,允许非阻塞
if (!(host1 = gethostbyname(address))){
printf("地址无效!\n");
return(0);
}
memset(&clientaddress, 0, sizeof(struct sockaddr));
clientaddress.sin_family =AF_INET;
clientaddress.sin_port = htons((unsigned short)port);
clientaddress.sin_addr = *((struct in_addr *)host1->h_addr);
timer4.tv_sec = 0;
timer4.tv_usec = 50;
FD_ZERO(&writefd); //清零,使集合中不包涵任何fd。
FD_SET(fd, &writefd);//把fd加入到wirtefd集合
connect(fd, (struct sockaddr *)&clientaddress, sizeof(struct sockaddr));//成功返回0,失败返回-1
if( FD_ISSET(fd, &writefd))
{
recv = select(fd+1, NULL, &writefd,NULL, &timer4); // 返回值>0, -1:出错 0 :超时
if( recv > 0 ) Result = TRUE;
printf("%d",recv);
}
closesocket(fd);
WSACleanup();
return Result;
}
int main(int argc, char* argv[])
{
int PortNum=1000;
int i=0;
int k=1;
printf("Hello World!\n");
printf("参数一共有%d个\n",argc);
if( argv[1]="-t" )
{
printf("-t\n");
}
while(k<100)
{
if( IsPortOpen("127.0.0.1", k) ) //atoi把字符串转换成整型数. argv[3]
{
printf("%d is open\n", k);
// exit(0);
}
else
{
printf("%d is close\n", k);
}
k++;
}
return 0;
}
以上介绍了“ 编写的端口扫描工具,扫非本机ip,一直超时??怎么回事啊”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2979580.html