Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DNS query, protocol

 
Kovacs Zsolt
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


import java.net.*;
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);
socket.send(packet);

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


Thanks in advance!
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kovacs Zsolt
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic