标题:计算机网络的对ip数据包的解析,但是运行窗口只是闪了一下就没了。有加syst ...
只看楼主
tzhg555
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-12-5
结帖率:100%
 问题点数:0 回复次数:1 
计算机网络的对ip数据包的解析,但是运行窗口只是闪了一下就没了。有加system ("pause");求高人指点。
#include"stdafx.h"
#include<iostream>
#include<winsock2.h>
#include<ws2tcpip.h>

#pragma comment(lib,"ws2_32")

#define IO_RCVALL _WSAIOW(IOC_VENDOR,1)

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;

void main(int argc,char *argv[])
{
 using namespace std;
 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);
 if(sock==INVALID_SOCKET)
 {
  cout<<endl<<"创建Socket失败!"<<endl;
  goto exit_clean;
 }
 BOOL flag=TRUE;
 if(setsockopt(sock,IPPROTO_IP,IP_HDRINCL,(char *) &flag,sizeof(flag))==SOCKET_ERROR)
 {
  cout<<endl<<"setsockopt操作失败:"<<WSAGetLastError()<<endl;
  goto exit_clean;
 }
 char hostName[128];
 if(gethostname(hostName,100)==SOCKET_ERROR)
 {
  cout<<endl<<"gethostname操作失败:"<<WSAGetLastError()<<endl;
  goto exit_clean;
 }
 hostent *pHostIP;
 if((pHostIP=gethostbyname(hostName))==NULL)
 {
  cout<<endl<<"gethostbyname操作失败:"<<WSAGetLastError()<<endl;
  goto exit_clean;
 }
 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;
  goto exit_clean;
 }
 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;
  goto exit_clean;
 }
 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;
   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;
  }
 }

exit_clean:
 closesocket(sock);
 WSACleanup();
 system ("pause");
}
搜索更多相关主题的帖子: 计算机网络 comment include system 数据包 
2011-11-26 16:55
我是菜鸟C
Rank: 4
等 级:业余侠客
帖 子:74
专家分:200
注 册:2011-3-27
得分:0 
会不会是在bind那里就退出去了?
有点看不懂。。。
2011-12-02 09:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-356296-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.119459 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved