几何尺寸与公差论坛

 找回密码
 注册
查看: 1870|回复: 0

单线程tcp扫描器

[复制链接]
发表于 2007-8-1 13:29:46 | 显示全部楼层 |阅读模式
// tcpscan.cpp
#include<winsock2.h>
#include<stdio.h>
#include <time.h>//计时需要用到的头文件
#pragma comment(lib,"ws2_32.lib")

clock_t start,end;//程序运行的起始和结束时间
float costtime;//程序耗时
void usage(void)
{
printf("\tusage: tcpscan RemoteIP StartPort-EndPort\n");
printf("\tExample: tcpscan 192.168.1.1 80-100\n");
}
int main(int argc,char **argv)
{
char *host;
int startport,endport;
char *p;
if(argc!=3)
{
usage();
return 0;
}
p=argv[2];//处理端口参数
if(strstr(argv[2],"-"))
{ startport=atoi(argv[2]);
for(;*p;)
if(*(p++)=='-')break;
endport=atoi(p);

if(startport<1 || endport>65535)
{ printf("ort Error!\n");
return 0;
}
}

host=argv[1];

WSADATA ws;
SOCKET s;
struct sockaddr_in addr;
int result;
long lresult;
lresult=WSAStartup(MAKEWORD(1,1), &ws);
addr.sin_family =AF_INET;
addr.sin_addr.s_addr =inet_addr(host);
start=clock();//开始计时
for (int i=startport;i<endport;i++)
{
s=socket(AF_INET, SOCK_STREAM, 0);
addr.sin_port = htons(i);
if(s==INVALID_SOCKET)break;
result=connect(s, (struct sockaddr*)&addr,sizeof(addr));
if(result==0)
{
printf("%s %d\n",host,i);
closesocket(s);

}

}
end=clock();//计时结束
costtime= (float)(end - start) / CLOCKS_PER_SEC; //转换时间格式
printf("Cost time:%f second",costtime);//显示耗时
WSACleanup();
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 12:50 , Processed in 0.035221 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表