创建Socket失败
运行后显示创建Socket失败,然后用管理员cmd,显示setsockopt操作失败。
怎么办。。。
源代码:
//IP数据包的捕获与解析
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <fstream>
#include <errno.h>
#pragma comment(lib,"ws2_32")
#define IO_RCVALL _WSAIOW(IOC_VENDOR,1)
using namespace std;
ofstream outfile("e:\\logfile.txt", ios::out);
typedef struct IP_HEAD //定义IP数据包数据结构
{
union
{
unsigned char Version;
unsigned char HeadLen;
};
unsigned char ServiceType;
unsigned short TotalLen;
unsigned short Identifier;
union
{
unsigned short Flags;
unsigned short FragOffset;
};
unsigned char TimeToLive;
unsigned char Protocol;
unsigned short HeadChecksum;
unsigned int SourceAddr;
unsigned int DestinAddr;
unsigned char Options;
}ip_head;
void main(int argc, char *argv[]) //argv需要抓取的IP数据包的数量
{
if (argc != 2) //检查命令行参数
{
cout << endl << "请输入命令行:PackParse packet_sum" << endl;
return;
}
//启动winsock
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)
{
cout << endl << "WSASTartup初始化失败" << endl;
return;
}
//初始化socket创建,原始套接字
SOCKET sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
if (sock == INVALID_SOCKET)
{
cout << endl << "创建Socket失败!" << endl;
return;
}
bool flag = true;
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)&flag, sizeof(flag)) == SOCKET_ERROR)
{
cout << endl << "setsockopt操作失败" << endl;
return;
}
//获得本地主机名
char hostName[128];
if (gethostname(hostName, 100) == SOCKET_ERROR)
{
cout << endl << "获取主机名操作失败" << endl;
return;
}
//获得本机IP地址
hostent *pHostIP;
if ((pHostIP = gethostbyname(hostName)) == NULL)
{
cout << endl << "获取主机地址操作失败" << endl;
return;
}
//填充sockaddr_in
sockaddr_in host_addr; //初始化socket创建/绑定
host_addr.sin_family = AF_INET;
host_addr.sin_port = htons(6000);
host_addr.sin_addr = *(in_addr *)pHostIP->h_addr_list[0];
//socket绑定
if (bind(sock, (PSOCKADDR)&host_addr, sizeof(host_addr)) == SOCKET_ERROR) //初始化socket(绑定)
{
cout << endl << "bind操作失败" << endl;
return;
}
//将网卡模式调为混杂,捕获所有的IP数据包,网卡不能接收mac地址不是自己的IP数据包
DWORD dwBufferLen[10];
DWORD dwBufferInLen = 1;
DWORD dwBytesReturned = 0;
if (WSAIoctl(sock, IO_RCVALL, &dwBufferInLen, sizeof(dwBufferInLen), &dwBufferLen, sizeof(dwBufferLen), &dwBytesReturned, NULL, NULL) == SOCKET_ERROR)
{
cout << endl << "WSAIoctl操作失败" << endl;
return;
}
cout << endl << "开始解析IP包:" << endl;
char buffer[65535];
int packsum = atoi(argv[1]);
for (int i = 0; i<packsum; i++)
{
if (recv(sock, buffer, 65535, 0)>0) //接收IP数据包
{
ip_head ip = *(ip_head *)buffer; //解析IP包头部字段
cout << "-----------------------" << endl;
cout << "版本:" << (ip.Version >> 4) << endl;
cout << "头部长度:" << ((ip.HeadLen & 0x0f) * 4) << endl;
cout << "服务类型:Priority" << (ip.ServiceType >> 5) << ",Service" << ((ip.ServiceType >> 1) & 0x0f) << endl;
cout << "总长度:" << ip.TotalLen << endl;
cout << "标识符:" << ip.Identifier << endl;
cout << "标志位:" << ((ip.Flags >> 15) & 0x01) << ",DF=" << ((ip.Flags >> 14) & 0x01) << ",Mf=" << ((ip.Flags >> 13) & 0x01) << endl;
cout << "片偏移:" << (ip.FragOffset & 0x1fff) << endl;
cout << "生存周期:" << (int)ip.TimeToLive << endl;
cout << "协议:Protocol" << (int)ip.Protocol << endl;
cout << "头部校验和:" << ip.HeadChecksum << endl;
cout << "原地址:" << inet_ntoa(*(in_addr *)&ip.SourceAddr) << endl;
cout << "目的IP地址:" << inet_ntoa(*(in_addr *)&ip.DestinAddr) << endl;
}
}
getchar(); getchar();
closesocket(sock); //关闭socket
WSACleanup(); //关闭winsock
}