原始套接字编写Sniffer失败??
希望有过原始套接字编程经验的朋友帮忙看一下:#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment (lib,"ws2_32.lib")
//#define SIO_RCVALL 0x98000001
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
void main()
{
SOCKET Sniffer;
int timeout=1000;//接收超时
struct sockaddr_in addr;
DWORD dwret=0;
WSADATA wsa;
int ret,i;
BOOL flag=TRUE;
char RecvBuf[6144];//6KB的接收缓冲区
if(WSAStartup(MAKEWORD(2,1),&wsa))
{
printf("套接字版本协商出错!\n");
return;
}
Sniffer=socket(AF_INET,SOCK_RAW,IPPROTO_RAW);//创建原始套接字
if(INVALID_SOCKET ==Sniffer)
{
printf("创建套接字失败!\n");
return;
}
setsockopt(Sniffer, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag)); //设置 IP 头操作选项
//设置套接字接收超时
if(SOCKET_ERROR ==setsockopt(Sniffer,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,sizeof(timeout)))
{
printf("设置套接字超时失败!\n");
closesocket(Sniffer);
WSACleanup();
return;
}
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
//设置本地地址
addr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
//设置监听端口
addr.sin_port=htons(1234);
//绑定端口
if(SOCKET_ERROR==bind(Sniffer,(struct sockaddr *)&addr,sizeof(addr)))
{
printf("绑定本地端口失败!错误号:%d\n",WSAGetLastError());
closesocket(Sniffer);
WSACleanup();
return;
}
if(SOCKET_ERROR==ioctlsocket(Sniffer, SIO_RCVALL, (unsigned long*)&dwret))
{
printf("设置Socket I/O模式出错!\n");
closesocket(Sniffer);
WSACleanup();
return;
}
//循环接收数据
while(1)
{
ret=recv(Sniffer, RecvBuf,sizeof( RecvBuf),0);
if(SOCKET_ERROR==ret)
{
if(WSAETIMEDOUT==WSAGetLastError())continue;
printf("接收数据包出错!\n");
closesocket(Sniffer);
return;
}
i++;
printf("接收到数据包%d",i);
}
WSACleanup();
return;
}
----------------解决方案--------------------------------------------------------
顶顶顶顶顶
----------------解决方案--------------------------------------------------------
搞那么多==干什么?
----------------解决方案--------------------------------------------------------
暂时看不懂,帮你顶一下
----------------解决方案--------------------------------------------------------
各位 帮帮忙呀?
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------
晕 我相信一定有人有这方面的经验的
----------------解决方案--------------------------------------------------------
不太懂,帮你托起来.
----------------解决方案--------------------------------------------------------