Thread: Problem in sending dns packet

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Angry Problem in sending dns packet

    Hello Guys

    I want to send a dns packet to the dns server to get the ip of requested hostname. I'm trying to send the data but wireshark says it's a malform packet. Kindly help me out to get the corrent format to send the packet...
    Here's my code

    Code:
    #include <winsock.h>
    #include<stdio.h>
    #include<string.h>
    #include <io.h>
    #include <sys/stat.h>
    
    int main()
    {
    	
    	WSADATA wsaData;
    	if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
    		fprintf(stderr, "WSAStartup failed.\n");
    		exit(1);
    	}
    
    	int sock;
    	struct sockaddr_in serv_addr;
    
    	sock = socket(AF_INET,SOCK_DGRAM,0);
    	
    	if(sock == -1){
    		printf("Error creating socket");
    	}
    
    	serv_addr.sin_family = AF_INET;
    	serv_addr.sin_port = htons(53);
    	serv_addr.sin_addr.s_addr = inet_addr("208.67.222.222");//Notice that inet_addr() returns the address in Network Byte Order already–you don’t have to call htonl()
    	//bzero(&(serv_addr.sin_zero),8);
    
    	int i= connect(sock, (struct sockaddr*)& serv_addr, sizeof(struct sockaddr));	
    	if(i==-1) {
    		printf("nahhhhh");
    	}
    	else{
    		printf("Connection Established\n");
    	
    	}
    	char *send_data, recv_data[1024];
    	send_data="110010000003www6google3com00101"; //sending data here
    	printf("send_data is: %s\n", send_data);
    	send(sock, send_data, strlen(send_data), 0); // send data to server
    		
    	if(strcmp(send_data, "EXIT") == 0) {
    		printf("%s", "Disconnected!\n\n");
    		//break;
    	}
    
    
    	int byte_count = recv(sock,recv_data,1024,0); // receive data from server
    	recv_data[byte_count] = '\0';
    
    	printf("Recieved data is: %s",recv_data);
    		
    	closesocket(sock);
    
    	return 0;
    
    }

    Kindly reason out the issue...

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 10:35 AM
  2. Problem in sending vector <string> by MPI c++
    By vineet pratap in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2009, 08:45 AM
  3. Problem sending Unicode in C++
    By alfaximena in forum C++ Programming
    Replies: 15
    Last Post: 07-23-2009, 04:11 PM
  4. Multi-threaded UDP Server Problem
    By nkhambal in forum C Programming
    Replies: 0
    Last Post: 07-05-2005, 02:27 PM
  5. Sending problem, or 1 = smiley face
    By Asmodeus in forum Tech Board
    Replies: 1
    Last Post: 04-25-2005, 11:01 AM