zoukankan      html  css  js  c++  java
  • 1475.ip数据包解析

    题目描述:

    我们都学习过计算机网络,知道网络层IP协议数据包的头部格式如下:
      
      其中IHL表示IP头的长度,单位是4字节;总长表示整个数据包的长度,单位是1字节。
      传输层的TCP协议数据段的头部格式如下:
      
      头部长度单位为4字节。
      你的任务是,简要分析输入数据中的若干个TCP数据段的头部。 详细要求请见输入输出部分的说明。

    输入:

    第一行为一个整数T,代表测试数据的组数。
    以下有T行,每行都是一个TCP数据包的头部分,字节用16进制表示,以空格隔开。数据保证字节之间仅有一个空格,且行首行尾没有多余的空白字符。
    保证输入数据都是合法的。

    输出:

    对于每个TCP数据包,输出如下信息:
    Case #x,x是当前测试数据的序号,从1开始。
    Total length = L bytes,L是整个IP数据包的长度,单位是1字节。
    Source = xxx.xxx.xxx.xxx,用点分十进制输出源IP地址。输入数据中不存在IPV6数据分组。
    Destination = xxx.xxx.xxx.xxx,用点分十进制输出源IP地址。输入数据中不存在IPV6数据分组。
    Source Port = sp,sp是源端口号。
    Destination Port = dp,dp是目标端口号。
    对于每个TCP数据包,最后输出一个多余的空白行。
    具体格式参见样例。
    请注意,输出的信息中,所有的空格、大小写、点符号、换行均要与样例格式保持一致,并且不要在任何数字前输出多余的前导0,也不要输出任何不必要的空白字符。

    样例输入:
    2
    45 00 00 34 7a 67 40 00 40 06 63 5a 0a cd 0a f4 7d 38 ca 09 cd f6 00 50 b4 d7 ae 1c 9b cf f2 40 80 10 ff 3d fd d0 00 00 01 01 08 0a 32 53 7d fb 5e 49 4e c8
    45 00 00 c6 56 5a 40 00 34 06 e0 45 cb d0 2e 01 0a cd 0a f4 00 50 ce 61 e1 e9 b9 ee 47 c7 37 34 80 18 00 b5 81 8f 00 00 01 01 08 0a 88 24 fa c6 32 63 cd 8d
    样例输出:
    Case #1
    Total length = 52 bytes
    Source = 10.205.10.244
    Destination = 125.56.202.9
    Source Port = 52726
    Destination Port = 80
    
    Case #2
    Total length = 198 bytes
    Source = 203.208.46.1
    Destination = 10.205.10.244
    Source Port = 80
    Destination Port = 52833


    #include <iostream>
    #include <cstdio>
    #include <cstring> 
    using namespace std; 
    int num(char c){
        if(c>='0'&&c<='9'){
            return c-'0';
        }else{
            return c-'a'+10;
        }
    }
    int main(int argc, char *argv[]) {
         
        int n,c=1;
        while(cin>>n){
            c=1;
            char str[1000];
            gets(str); //为了冲掉回车!!! 
            while(n--){
                gets(str);  //注意空格也是一个字符,数的时候也要算上!!! 
                int offset=num(str[1])*4*3; 
                cout<<"Case #"<<c++<<endl;
                cout<<"Total length = "<<num(str[6])*256*16+num(str[7])*256+num(str[9])*16+num(str[10])<<" bytes"<<endl;
                cout<<"Source = "<<num(str[36])*16+num(str[37])<<"."<<num(str[39])*16+num(str[40])<<"."<<num(str[42])*16+num(str[43])<<"."<<num(str[45])*16+num(str[46])<<endl;                                               
                cout<<"Destination = "<<num(str[48])*16+num(str[49])<<"."<<num(str[51])*16+num(str[52])<<"."<<num(str[54])*16+num(str[55])<<"."<<num(str[57])*16+num(str[58])<<endl;
                cout<<"Source Port = "<<num(str[offset])*16*256+num(str[offset+1])*256+num(str[offset+3])*16+num(str[offset+4])<<endl;
                cout<<"Destination Port = "<<num(str[offset+6])*16*256+num(str[offset+7])*256+num(str[offset+9])*16+num(str[offset+10])<<endl<<endl;
            }
        }
     
        return 0;
    }
  • 相关阅读:
    liunx命令二
    移动App专项测试
    web安全性测试用例
    jmeter分布式测试的坑(转)
    全新linux中通过编译方式安装nginx
    Centos的yum源更换为国内的阿里云源
    配置spotlight连接linux服务器
    linux创建新用户并给予root权限
    linux下安装sar
    liunx中安装禅道
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/9735151.html
Copyright © 2011-2022 走看看