ScanOneIP

来源:互联网 发布:年轻人 dha omega 知乎 编辑:程序博客网 时间:2024/05/16 17:19
// ScanOneIP.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "ScanOneIP.h"#include <AFXSOCK.H>#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// The one and only application objectCWinApp theApp;using namespace std;#define destPORT 137  //nbtstat name port#define myPORT 4321#define IDP_SOCKETS_INIT_FAILED         103///////////////////////////////////////////////////////////////////////////////--------------------------------var-------------------------------BYTE bs[50]={0x0,0x00,0x0,0x10,0x0,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x20,0x43,0x4b,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x0,0x0,0x21,0x0,0x1};unsigned char B1[4],B2[4];HANDLE wait_handle;int i;class CUDP : public CSocket{public:virtual void OnReceive(int nErrorCode);};CUDP m_UDPSocket;void CUDP::OnReceive(int nErrorCode){// TODO: Add your specialized code here and/or call the base classBYTE Buf[500];CString str,strIP,strHost,strHex,strMac,Host,Group,User;UINT dport;m_UDPSocket.ReceiveFrom(Buf,500,strIP,dport,0);//接收数据//如果接收到的ip为空或者与原来接收到的ip相同,则返回//if(strIP==(char)NULL||strIP==strOldIP)return;//strOldIP=strIP;//int index=m_ListView.InsertItem(0,strIP);//将ip插入ListViewprintf("strIP:%s\n",(LPCTSTR)strIP);strHost=""; //机器名字strHex="";//MAC地址User="?";//Host="\\";int tem=0,num=0;bool bAdd=true;//根据数据报规则取出相应的信息for(i=57;i<500;i++) //57-72{if(Buf[i]==0xcc)break; if(Buf[i]==0x20)bAdd=false;if(bAdd){str.Format("%c",Buf[i]);if(Buf[i]>=' ')strHost+=str;str.Format("%02x.",Buf[i]);strHex+=str;}if((++tem)%18==0){bAdd=true; strHost.TrimRight((char)NULL);if(strHost==""){strMac.Delete(17,strMac.GetLength()-17);//m_ListView.SetItem(index,4,LVIF_TEXT,strMac, 0, 0, 0,0);printf("strMac:%s\n",(LPCTSTR)strMac);break;}if(num==0&&strHost!=""){//m_ListView.SetItem(index,2,LVIF_TEXT,strHost, 0, 0, 0,0);printf("strHost:%s\n",(LPCTSTR)strHost);Host=strHost;num++;}else{if(Host!=strHost&&num==1&&strHost!=""){//m_ListView.SetItem(index,1,LVIF_TEXT,strHost, 0, 0, 0,0);printf("strHost:%s\n",(LPCTSTR)strHost);Group=strHost;num++;}else {if(strHost!=Host&&strHost!=Group&&num==2&&strHost!=""){User=strHost;if(User!="__MSBROWSE__"){//m_ListView.SetItem(index,3,LVIF_TEXT,User, 0, 0, 0,0);printf("User:%s\n",(LPCTSTR)User);num++;}}}}strMac=strHex;strHost="";strHex="";}}//触发事件,导致线程函数的继续执行SetEvent(wait_handle);CSocket::OnReceive(nErrorCode);}UINT NbtstatThread(LPVOID param);CStringm_strIP;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){int nRetCode = 0;// initialize MFC and print and error on failureif (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)){// TODO: change error code to suit your needscerr << _T("Fatal Error: MFC initialization failed") << endl;nRetCode = 1;}else if (!AfxSocketInit()){AfxMessageBox(IDP_SOCKETS_INIT_FAILED);nRetCode = 2;}else{if(!m_UDPSocket.Create(myPORT,SOCK_DGRAM)){AfxMessageBox("Failed Create Socket");}wait_handle=CreateEvent(NULL,true,false,"receive data");//创建处于非触发状态的事件。类型为手动//启动线程CWinThread* pThread =AfxBeginThread(NbtstatThread,NULL,THREAD_PRIORITY_NORMAL);WaitForSingleObject(pThread->m_hThread, INFINITE);MessageBox(NULL,"wait","delay",MB_OK);// TODO: code your application's behavior here.CString strHello;strHello.LoadString(IDS_HELLO);cout << (LPCTSTR)strHello << endl;}return nRetCode;}//--------------------------nbtstat线程----------------------UINT NbtstatThread(LPVOID param){//循环对要查询的ip发数据do{B1[0]=172,B1[1]=19,B1[2]=1,B1[3]=43;m_strIP.Format("%d.%d.%d.%d",B1[0],B1[1],B1[2],B1[3]);//得到ip//m_strIP.Format("%d.%d.%d.%d",172,19,1,43);//pDlg->m_ListBox.InsertString(0,pDlg->m_strIP);//将该ip插入ListView的ip字段if(B1[3]!=0&&B1[2]!=0)m_UDPSocket.SendTo((void*)bs,50,destPORT,m_strIP,0);//向指定的ip发数据报        int nWait=100;//pDlg->m_spin.GetPos();//设置超时printf("before WaitForSingleObject\n");WaitForSingleObject(wait_handle,        // 等待事件的句柄//INFINITE //nWait   // 超时);printf("after WaitForSingleObject\n");ResetEvent(wait_handle);//将事件重新置回非触发状态//=============================================}while(0);return 0;}

原创粉丝点击