File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes DNS query, protocol Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "DNS query, protocol" Watch "DNS query, protocol" New topic

DNS query, protocol

Kovacs Zsolt

Joined: May 18, 2011
Posts: 2
Hello I want to create a simple java console application that queries a DNS server.
I dont want to use libraries like dnsjava or anything else.
I want to use datagrams.
Can anyone help me with this.

the problem is that i don't know how the message in the below code should look like.
How DNS query should llok like.

class DatagramTest
public static void main(String[] args)
throws Exception
DatagramSocket socket;
DatagramPacket packet;
InetAddress address;
byte[] message = new byte[256];
int port = 13;

socket = new DatagramSocket();
address = InetAddress.getByName("*****");
packet = new DatagramPacket(message, message.length,
address, port);

packet = new DatagramPacket(message,
String received = new String(packet.getData(), 0);
System.out.println("Received: " + received);

Thanks in advance!
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

Welcome to the Ranch!

That's quite a project you've picked up for yourself, especially considering the fact that you indicate you have no real knowledge about how DNS actually works. (Don't worry about that, neither do I ) Can you tell us why you don't want to use an existing library?

Anyway, the first step would be to research how the DNS protocol works. I've checked this briefly but it only mentions that UDP is used. Perhaps you can find more information from the references at the bottom of that page.

And could you please UseCodeTags next time? It makes code easier to read.

How To Ask Questions How To Answer Questions
Kovacs Zsolt

Joined: May 18, 2011
Posts: 2
Thank you for the answer. I want to do this because I have to . It is an assigment at the University.
And sorry for the CodeTags. Anyway if i figure out i post it maybe help someone someday.
There is so little information about this on the net
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I know even less about DNS than Rob does (apparently we both know what it is, but that's all). So I chose "dns protocol" as my google keywords and soon found a few documents which look like they might be what you need. Here's one of them: DNS Protocol; in addition there seem to be other more basic documents in the same set of pages.
I agree. Here's the link:
subject: DNS query, protocol
It's not a secret anymore!