#include “WinSock2.h”
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
SOCKET SnifferSocket
WSADATA wsaData;
iFlag=WSAStartup(MAKEWORD(2,2),&wsaData); //开启winsock.dll
SnifferSocket=WSASocket(AF_INET, //创建raw socket
SOCK_RAW,IPPROTO_IP,NULL,0,WSA_FLAG_OVERLAPPED);
char FAR name[128]; //获取本机IP地址
gethostname(name, sizeof(name));
struct hostent FAR * pHostent;
pHostent = gethostbyname(name);
SOCKADDR_IN sa; //填充SOCKADDR_IN结构的内容
sa.sin_family = AF_INET;
sa.sin_port = htons(6000); // 端口号可以随便改,当然与当然系统不能冲突
memcpy(&(sa.sin_addr),pHostent->h_addr,pHostent->h_length);
bind(SnifferSocket,(LPSOCKADDR)&sa,sizeof(sa)); //绑定
// 置ioctl来接收所有网络数据,关键步骤
DWORD dwBufferLen[10] ;
DWORD dwBufferInLen = 1 ;
DWORD dwBytesReturned = 0 ;
WSAIoctl(SnifferSocket, IO_RCVALL,&dwBufferInLen, izeof(dwBufferInLen),
&dwBufferLen, sizeof(dwBufferLen),&dwBytesReturned , NULL , NULL );