zoukankan      html  css  js  c++  java
  • 九度oj 题目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

    这题很有北邮的特色
    一开始没注意到一行,以为是定长,也没注意到可以求ip头的长度,所以提交错误
    代码如下
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 using namespace std;
     6 //3 4 --length
     7 //13 14 15 16 --source
     8 //17 18 19 20 --des
     9 //21 22-sport
    10 //23 24-dport
    11 
    12 int cToN(char c) {
    13     if (c>= 'a' && c <= 'z') {
    14         return (c - 'a' + 10);
    15     }
    16     return (c - '0');
    17 }
    18 
    19 int    hToD(char st[2]) {
    20     int ans = 0;
    21     if (st[0] >= 'a' && st[0] <= 'z') {
    22         ans = ans + st[0] - 'a' + 10;
    23     }
    24     else {
    25         ans = ans + st[0] - '0';
    26     }
    27     if (st[1] >= 'a' && st[1] <= 'z') {
    28         ans = 16*ans + st[1] - 'a' + 10;
    29     }
    30     else {
    31         ans = 16*ans + st[1] - '0';
    32     }
    33     return ans;
    34 }
    35 char st[600][2];
    36 char str[1010];
    37 
    38 int main() {
    39     int T;
    40     //freopen("input.txt", "r", stdin);
    41     //freopen("output.txt", "w", stdout);
    42     while (scanf("%d", &T) != EOF) {
    43         gets(str);
    44         for (int t = 1; t <= T; t++) {
    45             gets(str);
    46             int len = strlen(str);
    47             int p = 1;
    48             for (int i = 0; i + 2 < len; i += 3) {
    49                 strncpy(st[p++], &str[i], 2);
    50             }
    51             int length = 256*hToD(st[3]) + hToD(st[4]);
    52             int source[4], des[4];
    53             for (int i = 0; i < 4; i++) {
    54                 source[i] = hToD(st[i+13]);
    55                 des[i] = hToD(st[i + 17]);
    56             }
    57             int ipLen = 4 * cToN(st[1][1]);
    58             int sp = 256 * hToD(st[ipLen + 1]) + hToD(st[ipLen+2]);
    59             int dp = 256 * hToD(st[ipLen + 3]) + hToD(st[ipLen+4]);
    60             printf("Case #%d
    ", t);
    61             printf("Total length = %d bytes
    ", length);
    62             printf("Source = %d.%d.%d.%d
    ", source[0], source[1], source[2], source[3]);
    63             printf("Destination = %d.%d.%d.%d
    ", des[0], des[1], des[2], des[3]);
    64             printf("Source Port = %d
    ", sp);
    65             printf("Destination Port = %d
    ", dp);
    66             puts("");
    67         }
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    ExtJS小试 TreePanel
    ExtJS 之panel tabpanel 和window 的简单使用
    ExtJs2.1未知明错误
    Extjs xtype
    ibatis 初体验
    MS数据库分页
    EXTJS 简单登陆
    php5.2.9 apache2.2 mysql5.1 phpMyAdmin3.2配置
    安装配置Ruby for Eclipse
    ExtJs之gridPanel初探
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5875524.html
Copyright © 2011-2022 走看看