DNS query with winsock was failed
-
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
-
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
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
-
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
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
-
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
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. -
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.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
-
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
-
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
-
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
-
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