//demo1.cpp : 定义控制台应用程序的入口点。//#include"stdafx.h"#include#include#include
//the macro HAVE_REMOTE must define before
#ifndef HAVE_REMOTE#define HAVE_REMOTE
#endif#include#include
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "packet.lib")
#pragma comment(lib, "wpcap.lib")
using namespacestd;/************************************************************************/
/*platfor win7 x64
* version of winpcap: 4.1.3
* version of developping tool: 4.1.2
* notes: The local/remote machine must install the Winpcap
and
Start the server(go to the install path and double click rpcapd.exe).
You must look out that the DEFAULT PORT is 2002.
If you use another port, the pcap_findalldevs_ex function return -1
and the erro information in errbuf is
[Is the server properly installed on XXX.XXX.XXX.XXX?
connect() failed: 由于目标计算机积极拒绝,无法连接。 (code 10061) ]
/************************************************************************/
int _tmain(int argc, _TCHAR*argv[])
{//char* pSource = "rpcap://";//localhost
char* pSource = "rpcap://XXX.XXX.XXX.XXX:2002"; //remote PC
struct pcap_rmtauth stAuth = {0};
stAuth.type=RPCAP_RMTAUTH_PWD;
stAuth.username= "xxxxx";
stAuth.password= "xxxxxxxxxxx";
pcap_if_t* pPcapIft =NULL;char chBuffer[PCAP_BUF_SIZE] = {0};int nCount = 0;if (0 == pcap_findalldevs_ex(pSource, &stAuth, &pPcapIft, chBuffer))
{for (pcap_if_t* pcap = pPcapIft; pcap != NULL; pcap = pcap->next)
{
cout<< endl << "----------- device"
<< nCount ++
<< "-------------" <
cout<< pcap->name<description<flags<
cout<< "-------- Output details below -----" <addresses;
pAddr!= NULL; pAddr = pAddr->next)
{struct sockaddr_in* psockAddr = (struct sockaddr_in*)(pAddr->addr);if (NULL !=psockAddr)
{
cout<< "IP is" << inet_ntoa(psockAddr->sin_addr) <
cout<< "Port is" << ntohs(psockAddr->sin_port) <
cout<< "Family is" << psockAddr->sin_family <
cout<< "-------" <
}
psockAddr= (struct sockaddr_in*)(pAddr->dstaddr);if (NULL !=psockAddr)
{
cout<< "Mask IP is" << inet_ntoa(psockAddr->sin_addr) <
cout<< "Mask Port is" << ntohs(psockAddr->sin_port) <
cout<< "Mask Family is" << psockAddr->sin_family <
cout<< "-------" <
}
psockAddr= (struct sockaddr_in*)(pAddr->broadaddr);if (NULL !=psockAddr)
{
cout<< "Broadcast IP is" << inet_ntoa(psockAddr->sin_addr) <
cout<< "Broadcast Port is" << ntohs(psockAddr->sin_port) <
cout<< "Broadcast Family is" << psockAddr->sin_family <
}
psockAddr= (struct sockaddr_in*)(pAddr->dstaddr);if (NULL !=psockAddr)
{
cout<< "P2P IP is" << inet_ntoa(psockAddr->sin_addr) <
cout<< "P2P Port is" << ntohs(psockAddr->sin_port) <
cout<< "P2P Family is" << psockAddr->sin_family <
}
cout<< "---------------------------------------" << endl << endl <
}//for
}//for
pcap_freealldevs(pPcapIft);
}//if
else{
cerr<< endl << "Last error is" << GetLastError() <
}
system("pause");return 0;
}