谁能帮我改一下 这个 简单的端口扫描器
这几天要做网络课设,但是自己又不会编程 网上下了个代码 自己研究了半天 看不懂
不明白为什么这程序只输出前面的部分,后面的都没执行
#include <winsock.h> 这个也没看懂
课设的题目是:
简单的端口扫描器
实现一个端口扫描程序,判断指定的主机有哪些端口可以提供服务, 并把扫描得知的结果记录下来
下面是代码 各位热心的帮忙看看 改改
#include <string.h>
#include <winsock.h>
#include <windows.h>
#include <iostream.h>
#pragma comment (lib,"ws2_32.lib")
int main (int argc, char *argv[])
{
int iportFrom,iportTo;
int testsocket;
int iopenedport = 0;
struct sockaddr_in target_addr;
WSADATA wsaData;
WORD wVersionRequested=MAKEWORD(1,1);
if (argc <= 3)
{
cout << "使用格式 : " << argv[0] << " 主机IP地址 开始端口号 结束端口号\n" << endl;
exit(1);
}
if (atoi (argv[2]) > atoi (argv[3]))
{
cout << "错误!开始端口号必须小于结束端口号" << endl;
exit(1);
}
else
{
if (WSAStartup (wVersionRequested , &wsaData) )
{
cout << "连接socket库失败,请检查版本号是否为1.1\n" << endl;
exit(1);
}
iportFrom=atoi (argv[2]);
iportTo=atoi (argv[3]);
for (int i=iportFrom; i <= iportTo; i++)
{
cout << "正在建立socket................................" << endl;
if ((testsocket=socket (AF_INET,SOCK_STREAM,0) ) == INVALID_SOCKET)
{
cout << "Socket建立失败!" << endl;
exit(0);
}
target_addr.sin_family = AF_INET;
target_addr.sin_port = htons(i);
target_addr.sin_addr.s_addr = inet_addr (argv[1]);
cout << "正在扫描端口:" << i << endl;
if (connect (testsocket, (struct sockaddr *) &target_addr, sizeof(struct sockaddr)) == SOCKET_ERROR)
cout << "端口" << i << "关闭!" << endl;
else
{
iopenedport++;
cout << "端口" << i << "开放\n" << endl;
}
}
cout << "目标主机" << argv[1] << "从" << iportFrom << "--" << iportTo << "共有" << iopenedport << "个端口开放" << endl;
closesocket (testsocket);
WSACleanup();
}
return 0;
}