【问题标题】:Received UDP packet length接收到的 UDP 数据包长度
【发布时间】:2011-01-16 02:38:38
【问题描述】:

如何获取接收到的UDP数据包的长度?使用wireshark我可以看到数据报的正确长度。如何在我的简单 udp 服务器程序中打印此值?我正在接收二进制数据(不可打印的 ascii 字符作为数据)所以我不能使用 strlen(buf),它会抛出不正确的长度。

      if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1){
            error = ioctl(s, FIONREAD, &value);
            printf(" from ioctl UDP packet length is : %d error is : %d\n", value, error);
      }

从上面的代码中,udp 数据包长度总是'0'。有cmets吗?

我也尝试过如下

         if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)!=-1){
            unsigned short iphdrlen;
            struct iphdr* iph = (struct iphdr*)buf;
            iphdrlen =iph->ihl*4;

            printf("IP version :%d\n", ((unsigned int)((iph->version))));
            printf("protocol .. %d\n", ((unsigned int)(iph->protocol)));
            printf("total len .. %d\n", (ntohs(iph->tot_len)));
         }

上面的代码总是从 ip 标头返回错误的值?有cmets吗?

此处包含原始 C 文件。

#include "udp_common.h"

int main(void)
{
        struct sockaddr_in si_me, si_other;
        int s, i, slen=sizeof(si_other);
        unsigned char buf[BUFLEN];
        int noofbytes=0;
        int ret=0,error,value;

        memset(buf, 0, 512);
        if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
                diep("socket");

        memset((char *) &si_me, 0, sizeof(si_me));
        si_me.sin_family = AF_INET;
        si_me.sin_port = htons(PORT);
        si_me.sin_addr.s_addr = htonl(INADDR_ANY);
        if (bind(s, (struct sockaddr *)&si_me, sizeof(si_me))==-1)
                diep("bind");

        for (i=0; ; i++)
        //for (i=0; i<NPACK ; i++) 
        {
                if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1)
                diep("recvfrom()");
                printf(" source port is : %d %d\n", ntohs(si_other.sin_port), slen);

                unsigned short iphdrlen;
                struct iphdr* iph = (struct iphdr*)buf;
                iphdrlen =iph->ihl*4;

                printf("IP version :%d\n", ((unsigned int)((iph->version))));
                printf("protocol .. %d\n", ((unsigned int)(iph->protocol)));
                printf("total len .. %d\n", (ntohs(iph->tot_len)));
                error = ioctl(s, FIONREAD, &value);
                printf(" from ioctl UDP packet length is : %d error is : %d\n", value, error);
                printf(" Return code from recvfrom is : %d\n", ret);
                printf("Received packet from %s:%d\nData: %s\n\n",
                inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);

                int j=0;
                for ( j = 0; j <20; j++)
                {
                        char x = buf[j];
                        short int i;
                        for (i=1;i<9;i++)
                        {
                                ((x | (1<<8-i)) == x)?printf("1"):printf("0");
                        }
                        printf(" ");
                }
                printf("\n");
        }


        close(s);
        return 0;
}

我收到的输出如下:

 source port is : 4232 16
IP version :1
protocol .. 65
total len .. 4096
 from ioctl UDP packet length is : 0 error is : 0
 Return code from recvfrom is : 0
Received packet from 127.0.0.1:4232
Data: 

00010000 00000001 00010000 00000000 11110001 00010001 00010001 00100100 01000100 01000001 00010001 00100100 01000100 01000000 00000000 00010000 00000000 00010000 10100000 10100000 

以上解密数据与我发送的数据匹配。

【问题讨论】:

    标签: c networking udp datagram


    【解决方案1】:

    您是如何创建套接字的?它是原始套接字(SOCK_RAW)还是 dgram 套接字(SOCK_DGRAM)?在第二种情况下,您不会获得 ip/udp 标头,只有有效负载。

    顺便说一句,数据包长度是函数 recvfrom 的返回值——在你的例子中是“ret”变量。检查 recvfrom 手册页。

    【讨论】:

    • 我使用 SOCK_DGRAM 创建了套接字。 “ret”变量的值始终为“0”。但是,如果我盲目地解密数据,我可以正确地看到原始发送的数据。
    • 如果值为 0,那么套接字只是出于某种原因出现故障(我很好,这对您没有多大帮助;))
    • 嘿,当你在做 if(ret = recvfrom(... stuff ..) == -1) 时,你忘记了几个括号。像这样写,ret 会影响 "recvfrom(... stuff ..) == -1" 的值,而不是 recvfrom 值。你必须写:“if((ret = recvfrom(...)) == -1) ...
    • 嘿,你是对的!我以这种方式获得了数据长度。谢谢!
    【解决方案2】:

    简短回答(虽然它已经在上面的 cmets 中):recvfrom 返回接收到的字节数。

    【讨论】:

      【解决方案3】:

      问题来了:

      if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1)
      

      改写为:

      ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)
      if (ret==-1)
      {
      }
      

      ret 将包含字节数

      【讨论】:

        猜你喜欢
        • 2010-12-09
        • 1970-01-01
        • 2013-11-08
        • 2014-12-14
        • 2011-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多