显示出创建套接字失败,求教大佬?

nbanba123456890 2018-05-29 11:14:34
#include "winsock2.h"
#include "ws2tcpip.h"
#include <windows.h>
#include <fstream>
#include <iostream>
#pragma comment(lib,"ws2_32")
using namespace std;
typedef struct _IP_HEADER
{
union
{
BYTE Version;
BYTE HdrLen;
};
BYTE ServiceType;
WORD TotalLen;
WORD ID;
union
{
WORD Flags;
WORD Fragoff;
};
BYTE TimeToLive;
BYTE Protocol;
WORD HdrChksum;
DWORD SrcAddr;
DWORD DstAddr;
BYTE Options;

}IP_HEADER;

int main()
{
SOCKET sock;
WSADATA wsData;
ofstream ofs("ip.log", ios::app);
if (WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
{
printf("WSAStartup failed!\n");
return -1;
}
if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP)) == INVALID_SOCKET)
{
printf("create socket failed\n");
return -1;
}

BOOL flag = true;
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag)) == SOCKET_ERROR)
{
printf("setsockopt failed!\n");
return -1;
}
char hostName[128];
if (gethostname(hostName, 100) == SOCKET_ERROR)
{
printf("gethostname failed!\n");
return -1;
}
hostent* pHostIP;
if ((pHostIP = gethostbyname(hostName)) == NULL)
{
printf("gethostbyname failed\n");
return -1;
}
sockaddr_in addr_in;
addr_in.sin_addr = *(in_addr*)pHostIP->h_addr_list[0];
addr_in.sin_family = AF_INET;
addr_in.sin_port = htons(6000);
if (bind(sock, (PSOCKADDR)&addr_in, sizeof(addr_in)) == SOCKET_ERROR)
{
printf("bind failed\n");
return -1;
}
#define IO_RCVALL _WSAIOW(IOC_VENDOR,1)
DWORD dwBufferLen[10];
DWORD dwBufferInLen = 1;
DWORD dwBytesReturned = 0;
char buffer[100];
if (WSAIoctl(sock, IO_RCVALL, &dwBufferInLen, sizeof(dwBufferInLen),
&dwBufferLen, sizeof(dwBufferLen), &dwBytesReturned, NULL, NULL) == SOCKET_ERROR)
{
printf("ioctlsocket faild\n");
return -1;
}

printf("Analysis IP Packet !\n\n");
string s;
char * p;
char buffer1[100];
while (true)
{
int size = recv(sock, buffer, sizeof(buffer), 0);
IP_HEADER ip = *(IP_HEADER *)buffer;
cout << "-----------------------" << endl;
s += "\n-----------------------\n";
cout << "Version: " << (ip.Version >> 4) << endl;
_itoa_s(ip.Version >> 4, buffer1, 10);
s += "Version: ";
s += buffer1;
s += "\n";
cout << "IHL: " << ((ip.HdrLen & 0x0f) * 4) << endl;
s += "IHL:";
_itoa_s((ip.HdrLen & 0x0f) * 4, buffer1, 10);
s += buffer1;
s += "\n";
cout << "Type of service: Priority" << (ip.ServiceType >> 5) <<
", Service" << ((ip.ServiceType >> 1) & 0x0f) << endl;
s += "Type of service: Priority";
_itoa_s(ip.ServiceType >> 5, buffer1, 10);
s += buffer1;
s += ", Service";
_itoa_s((ip.ServiceType >> 1) & 0x0f, buffer1, 10);
s += buffer1;
s += "\n";

cout << "TOtal Length: " << ip.TotalLen << endl;
s += "TOtal Length: ";
_itoa_s(ip.TotalLen, buffer1, 10);
s += buffer1;
s += "\n";
cout << "Identifcation: " << ip.ID << endl;
s += "Identifcation: ";
_itoa_s(ip.ID, buffer1, 10);
s += buffer1;
s += "\n";
cout << "sign: " << ((ip.Flags >> 15) & 0x01) << ", DF = " <<
((ip.Flags >> 14) & 0x01) << ", Mf = " << ((ip.Flags >> 13) & 0x01) << endl;
s += "sign: ";
_itoa_s((ip.Flags >> 15) & 0x01, buffer1, 10);
s += buffer1;
s += ", DF = ";
_itoa_s((ip.Flags >> 14) & 0x01, buffer1, 10);
s += buffer1;
s += ", Mf = ";
_itoa_s((ip.Flags >> 13) & 0x01, buffer1, 10);
s += buffer1;
s += "\n";
cout << "Fragment offset: " << (ip.Fragoff & 0x1fff) << endl;
s += "Fragment offset: ";
_itoa_s(ip.Fragoff & 0x1fff, buffer1, 10);
s += buffer1;
s += "\n";
cout << "Time to live: " << (int)ip.TimeToLive << endl;
s += "Time to live: ";
_itoa_s((int)ip.TimeToLive, buffer1, 10);
s += buffer1;
s += "\n";
cout << "Protocol:" << (int)ip.Protocol << endl;
s += "Protocol:";
_itoa_s((int)ip.Protocol, buffer1, 10);
s += buffer1;
s += "\n";
cout << "Header Checksum: " << ip.HdrChksum << endl;
s += "Header Checksum: ";
_itoa_s(ip.HdrChksum, buffer1, 10);
s += buffer1;
s += "\n";
cout << "Source address: " << inet_ntoa(*(in_addr *)&ip.SrcAddr) << endl;
s += "Source address: ";
s += inet_ntoa(*(in_addr *)&ip.SrcAddr);
s += "\n";
cout << "Destination address: " << inet_ntoa(*(in_addr *)&ip.DstAddr) << endl;
s += "Destination address: ";
s += inet_ntoa(*(in_addr *)&ip.DstAddr);
s += "\n";
p = &s[0];
ofs.write(p, strlen(p));
}
::closesocket(sock);
::WSACleanup();
return 0;
}
...全文
1562 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
danscort2000 2019-02-14
  • 打赏
  • 举报
回复
SOCK_RAW, 建议你换 windows XP操作系统下,用vc6 或者vs2005下进行编译测试
据我所知, makedollar公司在新版本操作系统下,封掉了sock_raw
管理员权限也不一定行了
wuxia2118 2019-01-27
  • 打赏
  • 举报
回复
由于SOCK_RAW要管理员权限,在win7系统中, 无法创建成功,以管理员方式运行也不行.

解决方法:用管理员权限启动VS2010

重新编译.
Eleven 2018-05-30
  • 打赏
  • 举报
回复
SOCK_RAW ?? 以管理员权限运行试试~
zgl7903 2018-05-30
  • 打赏
  • 举报
回复
WSAGetLastError 看错误代码, 高版本的系统可能会限制直接构造IP包
nbanba123456890 2018-05-29
  • 打赏
  • 举报
回复

18,364

社区成员

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

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