网络扫描问题急求...
如题,最近研究网络编程,打算写一个局域网扫描程序,但是我写的扫描程序速度非常之慢,从0~255一个网段我能扫半个小时,我的具体做法就是向每个机器的80端口发出连接请求,根据应答来看是否存活。有没有高效的扫描办法,求指教!就这些分了,都散了...
[解决办法]
之前写过一个,但是速度一样不快,想快的话就改成多线程吧楼主,另外你连接80端口的做法不是很可取....
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <iostream>
#include "Iphlpapi.h"
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"Iphlpapi.lib")
using namespace std;
void main(){
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions later */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2
[解决办法]
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
char name[200];
memset(name,0,200);
::gethostname(name,strlen(name));
hostent * host=::gethostbyname(name);
char pIp[15];
memset(pIp,0,15);
//memset(pIp, 0, host-> h_length+1);
//memcpy(pIp,*(host-> h_addr_list),2000);
//sprintf(pIp,"%s",*(host->h_addr_list));
strcpy(pIp,*(host-> h_addr_list));
char sip[100];
sprintf(sip, "%d.%d.%d.%d ", (unsigned char)pIp[0],
(unsigned char)pIp[1], (unsigned char)pIp[2], (unsigned char)pIp[3]);
//printf("%s",sip);
char dIp[100];
sprintf(dIp,"%d.%d.%d.",(unsigned char)pIp[0],
(unsigned char)pIp[1], (unsigned char)pIp[2]);
//printf("%s",dIp);
ULONG Mac[2];
ULONG uLen=6;
char strMacAddr[100]={0};
HRESULT hr;
cout<<"马上开始扫描..."<<endl;
::Sleep(1500);
cout<<"您的ip是:"<<sip<<endl;
cout<<"-------------------------------"<<endl;
int i=0;
BYTE * bPhysAddr;
for(i=0;i<=255;i++)
{uLen=6;
char destIp[200];
sprintf(destIp,"%s%d",dIp,i);
//printf("%s",destIp);
hr=::SendARP(inet_addr(destIp),inet_addr(sip),Mac,&uLen);
if(hr==NO_ERROR)
{
cout<<desttIp<<"在线"<<" "<<"物理地址:";
bPhysAddr = (BYTE *) & Mac;
if (uLen)
{
for (int j = 0; j < (int) uLen; j++)
{
if (j == (uLen - 1))
printf("%.2X\n", (int) bPhysAddr[j]);
else
printf("%.2X-", (int) bPhysAddr[j]);
}
}
cout<<endl;
}
}
cin.get();
}