Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Development
  4. DNS query with winsock was failed

DNS query with winsock was failed

Scheduled Pinned Locked Moved Windows Development
databasecomhardwaredebuggingquestion
9 Posts 2 Posters 25 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eugene Pustovoyt
    wrote on last edited by
    #1

    Hi, everybody I understand winsock has gethostbyname for this. But I need to debug for embedded application and first I try the code on Windows. Here is a my code:

    sockaddr\_in dns\_addr;
    int dns\_size = sizeof(dns\_addr);
    ZeroMemory(&dns\_addr, dns\_size);
    dns\_addr.sin\_family = AF\_INET;
    dns\_addr.sin\_port = htons(53);
    dns\_addr.sin\_addr.s\_addr = inet\_addr("8.8.8.8");
    
    SOCKET out = socket(AF\_INET, SOCK\_DGRAM, 0);
    
    char msg\[\] = {
    	0x12, 0x34,			// ID 
    	0x01, 0x00,			// QR ... RCODE
    	0x00, 0x01,			// QDCOUNT
    	0x00, 0x00,			// ANCOUNT
    	0x00, 0x00,			// NSCOUNT
    	0x00, 0x00,			// ARCOUNT
    	0x03, 
    	0x77, 0x77, 0x77,		// www
    	0x03, 
    	0x74, 0x75, 0x74,		// tut
    	0x02, 
    	0x62, 0x79,			// by
    	0x00, 
    	0x00, 0x01,			// TYPE\_A
    	0x00, 0x01			// CLASS\_IN
    };
    int msg\_size = sizeof(msg);
    int iSendOk = sendto(out, msg, msg\_size, 0, (sockaddr\*)&dns\_addr, dns\_size);
    
    if (iSendOk != SOCKET\_ERROR)
    {
    	char bufstr\[100\];
    	sockaddr\_in dest\_addr;
    	int dest\_addr\_size = sizeof(dest\_addr);
    	int iReceived = recvfrom(out, bufstr, 100, 0,(sockaddr\*)&dest\_addr, &dest\_addr\_size);
    }
    
    closesocket(out);
    

    And this code freezes when recvfrom function is execution. The function is executed with an infinite loop and never returns. What do I do wrong?

    Eugene Pustovoyt Soft and Hard Developer CPPMessageBox v1.0 CPPToolTip v2.1 CPPDumpCtrl v1.2 CPPHtmlStatic v1.2

    L 3 Replies Last reply
    0
    • E Eugene Pustovoyt

      Hi, everybody I understand winsock has gethostbyname for this. But I need to debug for embedded application and first I try the code on Windows. Here is a my code:

      sockaddr\_in dns\_addr;
      int dns\_size = sizeof(dns\_addr);
      ZeroMemory(&dns\_addr, dns\_size);
      dns\_addr.sin\_family = AF\_INET;
      dns\_addr.sin\_port = htons(53);
      dns\_addr.sin\_addr.s\_addr = inet\_addr("8.8.8.8");
      
      SOCKET out = socket(AF\_INET, SOCK\_DGRAM, 0);
      
      char msg\[\] = {
      	0x12, 0x34,			// ID 
      	0x01, 0x00,			// QR ... RCODE
      	0x00, 0x01,			// QDCOUNT
      	0x00, 0x00,			// ANCOUNT
      	0x00, 0x00,			// NSCOUNT
      	0x00, 0x00,			// ARCOUNT
      	0x03, 
      	0x77, 0x77, 0x77,		// www
      	0x03, 
      	0x74, 0x75, 0x74,		// tut
      	0x02, 
      	0x62, 0x79,			// by
      	0x00, 
      	0x00, 0x01,			// TYPE\_A
      	0x00, 0x01			// CLASS\_IN
      };
      int msg\_size = sizeof(msg);
      int iSendOk = sendto(out, msg, msg\_size, 0, (sockaddr\*)&dns\_addr, dns\_size);
      
      if (iSendOk != SOCKET\_ERROR)
      {
      	char bufstr\[100\];
      	sockaddr\_in dest\_addr;
      	int dest\_addr\_size = sizeof(dest\_addr);
      	int iReceived = recvfrom(out, bufstr, 100, 0,(sockaddr\*)&dest\_addr, &dest\_addr\_size);
      }
      
      closesocket(out);
      

      And this code freezes when recvfrom function is execution. The function is executed with an infinite loop and never returns. What do I do wrong?

      Eugene Pustovoyt Soft and Hard Developer CPPMessageBox v1.0 CPPToolTip v2.1 CPPDumpCtrl v1.2 CPPHtmlStatic v1.2

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      sockaddr_in dns_addr;
      int dns_size = sizeof(dns_addr);

      What's the "size" of the "uninitialized" dns_addr? What other values can iSendOk take if it isn't SOCKET_ERROR?

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      E 1 Reply Last reply
      0
      • L Lost User

        sockaddr_in dns_addr;
        int dns_size = sizeof(dns_addr);

        What's the "size" of the "uninitialized" dns_addr? What other values can iSendOk take if it isn't SOCKET_ERROR?

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        E Offline
        E Offline
        Eugene Pustovoyt
        wrote on last edited by
        #3

        You're right. Getting size of structure better after initialization. But in my cause this structure has constant length and her size will be same after initialization. Second, I check other returning values but I cut this code for this example. When I'm debugging this code sendto function return 28. This value is equal to the length of sent data.

        Eugene Pustovoyt Soft and Hard Developer CPPMessageBox v1.0 CPPToolTip v2.1 CPPDumpCtrl v1.2 CPPHtmlStatic v1.2

        1 Reply Last reply
        0
        • E Eugene Pustovoyt

          Hi, everybody I understand winsock has gethostbyname for this. But I need to debug for embedded application and first I try the code on Windows. Here is a my code:

          sockaddr\_in dns\_addr;
          int dns\_size = sizeof(dns\_addr);
          ZeroMemory(&dns\_addr, dns\_size);
          dns\_addr.sin\_family = AF\_INET;
          dns\_addr.sin\_port = htons(53);
          dns\_addr.sin\_addr.s\_addr = inet\_addr("8.8.8.8");
          
          SOCKET out = socket(AF\_INET, SOCK\_DGRAM, 0);
          
          char msg\[\] = {
          	0x12, 0x34,			// ID 
          	0x01, 0x00,			// QR ... RCODE
          	0x00, 0x01,			// QDCOUNT
          	0x00, 0x00,			// ANCOUNT
          	0x00, 0x00,			// NSCOUNT
          	0x00, 0x00,			// ARCOUNT
          	0x03, 
          	0x77, 0x77, 0x77,		// www
          	0x03, 
          	0x74, 0x75, 0x74,		// tut
          	0x02, 
          	0x62, 0x79,			// by
          	0x00, 
          	0x00, 0x01,			// TYPE\_A
          	0x00, 0x01			// CLASS\_IN
          };
          int msg\_size = sizeof(msg);
          int iSendOk = sendto(out, msg, msg\_size, 0, (sockaddr\*)&dns\_addr, dns\_size);
          
          if (iSendOk != SOCKET\_ERROR)
          {
          	char bufstr\[100\];
          	sockaddr\_in dest\_addr;
          	int dest\_addr\_size = sizeof(dest\_addr);
          	int iReceived = recvfrom(out, bufstr, 100, 0,(sockaddr\*)&dest\_addr, &dest\_addr\_size);
          }
          
          closesocket(out);
          

          And this code freezes when recvfrom function is execution. The function is executed with an infinite loop and never returns. What do I do wrong?

          Eugene Pustovoyt Soft and Hard Developer CPPMessageBox v1.0 CPPToolTip v2.1 CPPDumpCtrl v1.2 CPPHtmlStatic v1.2

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          It is most likely that the remote device is not responding to the datagram. If there is no data to be read from the socket then recvfrom will block forever, or until the connection is broken. You should use a non-blocking method to check whether any data has been received and use some form of timed loop to allow the program to abort the activity after a fixed time period. See recvfrom function (winsock.h) - Win32 apps | Microsoft Docs[^] for full details.

          E 1 Reply Last reply
          0
          • L Lost User

            It is most likely that the remote device is not responding to the datagram. If there is no data to be read from the socket then recvfrom will block forever, or until the connection is broken. You should use a non-blocking method to check whether any data has been received and use some form of timed loop to allow the program to abort the activity after a fixed time period. See recvfrom function (winsock.h) - Win32 apps | Microsoft Docs[^] for full details.

            E Offline
            E Offline
            Eugene Pustovoyt
            wrote on last edited by
            #5

            I tried to specify the reading size to 1 byte instead 100 from example. But I got same result.

            Eugene Pustovoyt Soft and Hard Developer CPPMessageBox v1.0 CPPToolTip v2.1 CPPDumpCtrl v1.2 CPPHtmlStatic v1.2

            L 1 Reply Last reply
            0
            • E Eugene Pustovoyt

              I tried to specify the reading size to 1 byte instead 100 from example. But I got same result.

              Eugene Pustovoyt Soft and Hard Developer CPPMessageBox v1.0 CPPToolTip v2.1 CPPDumpCtrl v1.2 CPPHtmlStatic v1.2

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Of course you did. The socket is still waiting for a response from the remote address.

              E 1 Reply Last reply
              0
              • L Lost User

                Of course you did. The socket is still waiting for a response from the remote address.

                E Offline
                E Offline
                Eugene Pustovoyt
                wrote on last edited by
                #7

                Does it mean that the DNS server does not respond if there is not a single byte in response? But why?

                Eugene Pustovoyt Soft and Hard Developer CPPMessageBox v1.0 CPPToolTip v2.1 CPPDumpCtrl v1.2 CPPHtmlStatic v1.2

                L 1 Reply Last reply
                0
                • E Eugene Pustovoyt

                  Does it mean that the DNS server does not respond if there is not a single byte in response? But why?

                  Eugene Pustovoyt Soft and Hard Developer CPPMessageBox v1.0 CPPToolTip v2.1 CPPDumpCtrl v1.2 CPPHtmlStatic v1.2

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  That server is owned by Google, so you should check with them how to get information from it. I am not sure that direct messages are accepted. See Get Started - Public DNS - Google Developers[^]

                  1 Reply Last reply
                  0
                  • E Eugene Pustovoyt

                    Hi, everybody I understand winsock has gethostbyname for this. But I need to debug for embedded application and first I try the code on Windows. Here is a my code:

                    sockaddr\_in dns\_addr;
                    int dns\_size = sizeof(dns\_addr);
                    ZeroMemory(&dns\_addr, dns\_size);
                    dns\_addr.sin\_family = AF\_INET;
                    dns\_addr.sin\_port = htons(53);
                    dns\_addr.sin\_addr.s\_addr = inet\_addr("8.8.8.8");
                    
                    SOCKET out = socket(AF\_INET, SOCK\_DGRAM, 0);
                    
                    char msg\[\] = {
                    	0x12, 0x34,			// ID 
                    	0x01, 0x00,			// QR ... RCODE
                    	0x00, 0x01,			// QDCOUNT
                    	0x00, 0x00,			// ANCOUNT
                    	0x00, 0x00,			// NSCOUNT
                    	0x00, 0x00,			// ARCOUNT
                    	0x03, 
                    	0x77, 0x77, 0x77,		// www
                    	0x03, 
                    	0x74, 0x75, 0x74,		// tut
                    	0x02, 
                    	0x62, 0x79,			// by
                    	0x00, 
                    	0x00, 0x01,			// TYPE\_A
                    	0x00, 0x01			// CLASS\_IN
                    };
                    int msg\_size = sizeof(msg);
                    int iSendOk = sendto(out, msg, msg\_size, 0, (sockaddr\*)&dns\_addr, dns\_size);
                    
                    if (iSendOk != SOCKET\_ERROR)
                    {
                    	char bufstr\[100\];
                    	sockaddr\_in dest\_addr;
                    	int dest\_addr\_size = sizeof(dest\_addr);
                    	int iReceived = recvfrom(out, bufstr, 100, 0,(sockaddr\*)&dest\_addr, &dest\_addr\_size);
                    }
                    
                    closesocket(out);
                    

                    And this code freezes when recvfrom function is execution. The function is executed with an infinite loop and never returns. What do I do wrong?

                    Eugene Pustovoyt Soft and Hard Developer CPPMessageBox v1.0 CPPToolTip v2.1 CPPDumpCtrl v1.2 CPPHtmlStatic v1.2

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Hi, The code you posted is implemented as a blocking socket. It will literally block the entire thread until it receives a response from the other side. To avoid this you can set the SO_RCVTIMEO[^] socket option. Best Wishes, -David Delaune

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups