优点简单
缺点速度慢,一般防火墙会过滤掉该数据包,且发起扫描的源地址容易被察觉
#include <stdio.h>
#include <winsock2.h>#pragma comment(lib,"ws2_32")
#define START_PROT 80 //扫描起始端口
#define END_PROT 1025 //扫描结束端口
int main(int argc, char *argv[])
{
if(argc != 2) //检查是否有IP地址参数
{
printf("need a IP");
return 1;
}
int i;
WSADATA ws;
SOCKET sockfd;
struct sockaddr_in target_addr;
WSAStartup(MAKEWORD(2,2),&ws);
//初始化地址结构体
target_addr.sin_family = AF_INET;
target_addr.sin_addr.S_un.S_addr = inet_addr(argv[1]);
for(i=START_PROT; i<END_PROT; i++)
{
sockfd = socket(AF_INET,SOCK_STREAM,0);//建立套接字
target_addr.sin_port = htons(i);//初始化地址端口
if(connect(sockfd,(struct sockaddr *)&target_addr,
sizeof(struct sockaddr)) == SOCKET_ERROR)//尝试连接
{printf("PORT %d close\n",i);//连接失败,端口未打开
}
else
{
printf("PORT %d open\n",i);//连接成功,端口已打开
}
}
closesocket(sockfd);
WSACleanup();
return 0;
}