创建Socket失败

zf826768216 2015-12-04 08:23:43
运行后显示创建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
}
...全文
1042 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
曦光gy 2018-12-26
  • 打赏
  • 举报
回复
为啥还是 创建失败
zf826768216 2015-12-07
  • 打赏
  • 举报
回复
原因肯定是 bool flag = true; if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)&flag, sizeof(flag)) == SOCKET_ERROR) { cout << endl << "setsockopt操作失败" << WSAGetLastError() << endl; 不知道为什么setsockopt错了。。
zf826768216 2015-12-07
  • 打赏
  • 举报
回复
引用 1 楼 lx624909677 的回复:
你的代码中,走到哪一行失败的?

zf826768216 2015-12-07
  • 打赏
  • 举报
回复
附上最后可以运行的程序: //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; int main() //argv需要抓取的IP数据包的数量 { int nRetCode = 0; /*if (argc != 2) //检查命令行参数 { cout << endl << "请输入命令行:PackParse packet_sum" << endl; //int packsum = 1; //return; }*/ FILE * file; if ((file = fopen("history.txt", "wb+")) == NULL) { printf("fail to open file %s"); return -1; } //启动winsock WSADATA WSAData; if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0) { cout << endl << "WSASTartup初始化失败" << GetLastError() << endl; return -1; } //初始化socket创建,原始套接字 SOCKET sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP); if (sock == INVALID_SOCKET) { cout << endl << "创建Socket失败!" << WSAGetLastError() << endl; return -1; } bool flag = true; //DWORD flag = true; /*if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag)) == SOCKET_ERROR) { cout << endl << "setsockopt操作失败" << WSAGetLastError() << endl; return -1; }*/ setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag)); //获得本地主机名 char hostName[128]; if (gethostname(hostName, 100) == SOCKET_ERROR) { cout << endl << "获取主机名操作失败" << GetLastError() << endl; return -1; } //获得本机IP地址 hostent *pHostIP; if ((pHostIP = gethostbyname(hostName)) == NULL) { cout << endl << "获取主机地址操作失败" << GetLastError() << endl; return -1; } //填充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操作失败" << GetLastError() << endl; return -1; } //将网卡模式调为混杂,捕获所有的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操作失败" << GetLastError() << endl; return -1; } cout << endl << "开始解析经过本机的IP数据包:" << endl; char buffer[65535]; //设置解析包的数量 packsum /*int packsum = 10; 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; } }*/ //不设定解析包的数量,一直执行解析 while (true) { 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 return nRetCode; }
zf826768216 2015-12-07
  • 打赏
  • 举报
回复
已经解决了 将 if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)&flag, sizeof(flag)) == SOCKET_ERROR) { cout << endl << "setsockopt操作失败" << WSAGetLastError() << endl; return; } 改为: setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag)); 即可解决
信阳毛尖 2015-12-07
  • 打赏
  • 举报
回复

int flag = 1;
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &flag, sizeof(flag)) == SOCKET_ERROR)
{
cout << endl << "setsockopt操作失败" << endl;
return;
}
lx624909677 2015-12-06
  • 打赏
  • 举报
回复
你的代码中,走到哪一行失败的?
paschen 2015-12-06
  • 打赏
  • 举报
回复
WSAGetLastError

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧