Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8523697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:28:59+00:00 2026-06-11T07:28:59+00:00

I’m trying to recieve a DNS request (DNS content with DNS header) in C++

  • 0

I’m trying to recieve a DNS request (DNS content with DNS header) in C++ on Linux 2.6.32. The code works when I write a string over UDP with netcat. But if I do a “nslookup google.com”, it recieves only this:

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

And if I use “tcpdump -x ‘udp port 53’ it shows me many bytes more.

My C++ code:

#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;
}

Compile this with

g++ -Wall main.cpp -o dns

So, what is my fault, did I forget something or must I use an other call to read binary data?

PS: Localhost is the first nameserver in /etc/resolv.conf

EDIT:

I looket at the return value from recvfrom, and it shows me:

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

That should be OK. Why does my buffer doesn’t hold all bytes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:29:01+00:00Added an answer on June 11, 2026 at 7:29 am

    You are mostly ignoring the return value of recvfrom(). That return value tells you how many bytes you received.

    From the man page:

    Upon successful completion, recvfrom() returns the length of the message in bytes

    Without that information, you cannot know that you didn’t receive all of the bytes.

    So, what is my fault, did I forget something or must I use an other call to read binary data?

    You are reading the binary data just fine, but you are writing it incorrectly. Your call to std::cout << buf and your call to fputs(buf, pFile) each operate in character strings, not on binary data. Specifically, they each write the bytes up to, but not including, the first zero-valued byte.

    To fix your program, record the answer from recvfrom, skip the call to cout << buf, and replace your fputs() with an 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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.