Palsur 2022-01-03 08:52 采纳率: 44.4%
浏览 72
已结题

C++捕获本机网卡IP包并解析的程序中void main(int argc,char *argv[]) 出错了,希望能帮我改正

#include<iostream> 
#include<winsock2.h> 
#include<ws2tcpip.h> 
#include<fstream>
#include <windows.h> 
#pragma comment(lib,"ws2_32") //指定连接到网络应用和internet
#define IO_RCVALL _WSAIOW(IOC_VENDOR,1)
using namespace std; 
typedef struct IP_HEAD 
{ 
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; //定义IP头部的数据结构
void main(int argc,char *argv[]) 
{ 
ofstream outfile("C:\\logfile.txt",ios::out);
if(argc!=2) 
{ 
cout<<endl<<"请以下格式输入命令行:PackParse packet_sum"<<endl; 
return; 
} 
WSADATA WSAData; 
if(WSAStartup(MAKEWORD(2,2), &WSAData)!=0) 
{ 
cout<<endl<<"WSASTartup初始化失败"<<endl; 
return; 
} 
SOCKET sock=socket(AF_INET,SOCK_RAW,IPPROTO_IP); //三个参分别为通信发生的区字段,套接字的类型,与IP协议
if(sock==INVALID_SOCKET) 
{ 
cout<<endl<<"创建Socket失败!"<<endl; 
closesocket(sock); 
WSACleanup(); 
} 
BOOL flag=TRUE; 
if(setsockopt(sock,IPPROTO_IP,IP_HDRINCL,(char *) &flag,sizeof(flag))==SOCKET_ERROR) 
{ 
cout<<endl<<"setsockopt操作失败:"<<WSAGetLastError()<<endl; 
closesocket(sock); 
WSACleanup(); 
} 
char hostName[128];//获取主机名 
if(gethostname(hostName,100)==SOCKET_ERROR) 
{ 
cout<<endl<<"gethostname操作失败:"<<WSAGetLastError()<<endl; 
closesocket(sock); 
WSACleanup(); 
} 
hostent *pHostIP; //获取本地IP
if((pHostIP=gethostbyname(hostName))==NULL) 
{ 
cout<<endl<<"gethostbyname操作失败:"<<WSAGetLastError()<<endl; 
closesocket(sock); 
WSACleanup(); 
} 
sockaddr_in host_addr;// 
host_addr.sin_family=AF_INET; 
host_addr.sin_port=htons(6000); 
host_addr.sin_addr=*(in_addr *)pHostIP->h_addr_list[0]; 
if(bind(sock,(PSOCKADDR)&host_addr,sizeof(host_addr))==SOCKET_ERROR) 
{ 
cout<<endl<<"bind操作失败:"<<WSAGetLastError()<<endl; 
closesocket(sock); //绑定网卡
WSACleanup(); 
} 
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操作失败:"<<WSAGetLastError()<<endl; 
closesocket(sock); //将网卡设为混杂模式,以接受所有数据
WSACleanup(); 
} 
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_head ip=*(ip_head *)buffer; 
cout<<"-----------------------"<<endl; 
cout<<"版本:"<<(ip.Version>>4)<<endl; //获取头部长度字段
cout<<"头部长度:"<<((ip.HeadLen &0x0f)*4)<<endl; //获取头部长度字段
cout<<"服务类型:Priority"<<(ip.ServiceType>>5)<<", Service"<<((ip.ServiceType>>1)&0x0f)<<endl;   //优先级子域和TOS子域
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; //获取源IP地址字段
cout<<"目的IP地址:"<<inet_ntoa(*(in_addr *)&ip.DestinAddr)<<endl; //获取目的IP地址字段
outfile<<"-----------------------"<<endl; 
outfile<<"版本:"<<(ip.Version>>4)<<endl; 
outfile<<"头部长度:"<<((ip.HeadLen &0x0f)*4)<<endl; 
outfile<<"服务类型:Priority"<<(ip.ServiceType>>5)<<", Service"<<((ip.ServiceType>>1)&0x0f)<<endl; 
outfile<<"总长度:"<<ip.TotalLen<<endl; 
outfile<<"标识符:"<<ip.Identifier<<endl; 
outfile<<"标志位:"<<((ip.Flags>>15)&0x01)<<",DF= "<<((ip.Flags>>14)&0x01)<<",Mf="<<((ip.Flags>>13)&0x01)<<endl; 
outfile<<"片偏移:"<<(ip.FragOffset&0x1fff)<<endl; 
outfile<<"生存周期:"<<(int)ip.TimeToLive<<endl; 
outfile<<"协议:Protocol"<<(int)ip.Protocol<<endl; 
outfile<<"头部校验和:"<<ip.HeadChecksum<<endl; 
outfile<<"原地址:"<<inet_ntoa(*(in_addr *)&ip.SourceAddr)<<endl; 
outfile<<"目的IP地址:"<<inet_ntoa(*(in_addr *)&ip.DestinAddr)<<endl; 
} 
} 
closesocket(sock); 
WSACleanup(); 
}


展开全部

  • 写回答

1条回答 默认 最新

  • 书山客 2022-01-03 09:13
    关注

    改成void main(int argc, char** argv)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 1月24日
  • 已采纳回答 1月17日
  • 创建了问题 1月3日

悬赏问题

  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing