【问题标题】:C++ recvfrom doesn't receive all (binary) dataC++ recvfrom 不接收所有(二进制)数据
【发布时间】:2012-09-06 21:59:45
【问题描述】:

我正在尝试在 Linux 2.6.32 上以 C++ 接收 DNS 请求(带有 DNS 标头的 DNS 内容)。当我使用 netcat 通过 UDP 编写字符串时,该代码有效。但如果我执行“nslookup google.com”,它只会收到以下信息:

root@EliteBook:/home/.../dns# xxd request.bin  
0000000: 8a2b 01

如果我使用 "tcpdump -x 'udp port 53' 它会显示更多字节。

我的 C++ 代码:

#include <string>
#include <iostream>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#define BUFLEN 1024*6
#define NPACK 10
#define PORT 53

using namespace std;

void diep(const string &s)
{
        perror(s.c_str());
        exit(1);
}

int main()
{
        struct sockaddr_in si_me;
        struct sockaddr si_other;
        int slen=sizeof(si_other);
        int s;                                          // UDP Socket
        char buf[BUFLEN];
        buf[0] = 'X';
        buf[1] = 'Y';
        buf[2] = 'Z';

        if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
                diep("Failed to create 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, (const sockaddr*) &si_me, sizeof(si_me))==-1)
                diep("bind");

        for (int i=0; i<NPACK; i++) {
                if (recvfrom(s, buf, BUFLEN, 0, &si_other, (socklen_t*) &slen)==-1)
                        diep("recvfrom()");

                struct sockaddr_in *si_other_in = (struct sockaddr_in *)&si_other;

                cout << "Pkg arrived: " << buf << " " << inet_ntoa(si_other_in->sin_addr) << ":" << ntohs(si_other_in->sin_port) << endl;

                FILE * pFile;
                pFile = fopen ("request.bin","wb");
                if (pFile!=NULL)
                {
                        fputs (buf, pFile);
                        fclose (pFile);
                }
        }
        close(s);

        return 0;
}

编译

g++ -Wall main.cpp -o dns

那么,我的错是什么,是我忘记了什么还是必须使用其他调用来读取二进制数据?

PS: localhost 是 /etc/resolv.conf 中的第一个域名服务器

编辑:

我查看了 recvfrom 的返回值,它告诉我:

size: **27**
Pkg arrived: �. 127.0.0.1:36686
size: **45**
Pkg arrived: l 127.0.0.1:38952

应该没问题。为什么我的缓冲区没有保存所有字节?

【问题讨论】:

  • "为什么我的缓冲区没有保存所有字节?" 您的缓冲区确实保存了所有字节。您只是没有将它们全部打印出来。

标签: c++ linux dns binary


【解决方案1】:

您大多忽略了recvfrom() 的返回值。该返回值告诉您收到了多少字节。

来自man page

成功完成后,recvfrom() 返回消息的长度(以字节为单位)

没有这些信息,您就无法知道您没有收到所有字节。

那么,我的错是什么,是我忘记了什么还是必须使用其他调用来读取二进制数据?

读取二进制数据很好,但您写入它不正确。您对std::cout &lt;&lt; buf 的调用和对fputs(buf, pFile) 的调用均在字符串中操作,而不是二进制数据。具体来说,它们每个都将字节写入到第一个零值字节,但不包括第一个零值字节。

要修复您的程序,请记录来自 recvfrom 的答案,跳过对 cout &lt;&lt; buf 的调用,并将您的 fputs() 替换为 fwrite

// UNTESTED
for (int i=0; i<NPACK; i++) {
    int bufsize;
    if ( (bufsize = recvfrom(s, buf, BUFLEN, 0, &si_other, (socklen_t*) &slen))==-1)
        diep("recvfrom()");

        struct sockaddr_in *si_other_in = (struct sockaddr_in *)&si_other;

        cout << "Pkg arrived: " << bufsize << " " << inet_ntoa(si_other_in->sin_addr) << ":" << ntohs(si_other_in->sin_port) << endl;

        FILE * pFile;
        pFile = fopen ("request.bin","wb");
        if (pFile!=NULL)
        {
            fwrite(buf, 1, bufsize, pFile);
            fclose (pFile);
        }
    }
}

【讨论】:

  • 但是我把它放在一个 FOR 循环中,它只迭代一次(见 std::cout 输出)
猜你喜欢
  • 2011-05-31
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多