CN LAB Ex5
CN LAB Ex5
AIM:
To write a java program for DNS application
PRE LAB DISCUSSION:
The Domain Name System (DNS) is a hierarchical decentralized naming system for
computers, services, or other resources connected to the Internet or a private network. It
associates various information with domain names assigned to each of the participating
entities.
The domain name space refers a hierarchy in the internet naming structure. This
hierarchy has multiple levels (from 0 to 127), with a root at the top. The following
diagram shows the domain name space hierarchy.
Name server contains the DNS database. This database comprises of various names
and their corresponding IP addresses. Since it is not possible for a single server to
maintain entire DNS database, therefore, the information is distributed among many
DNSservers.
Types of NameServers
Root Server is the top level server which consists of the entire DNS tree. It does not
contain the information about domains but delegates the authority to the otherserver
Primary Server stores a file about its zone. It has authority to create, maintain, and
update the zonefile.
Secondary Server transfers complete information about a zone from another server
which may be primary or secondary server. The secondary server does not have
authority to create or update a zonefile.
DNS is a TCP/IP protocol used on different platforms. The domain name space is
divided into three different sections: generic domains, country domains, and
inversedomain.
The main function of DNS is to translate domain names into IP Addresses, which
computers can understand. It also provides a list of mail servers which accept Emails for
each domain name. Each domain name in DNS will nominate a set of name servers to
beauthoritative for its DNSrecords.
ALGORITHM
Server
1. Start
2. Create UDP datagramsocket
3. Create a table that maps host name and IPaddress
4. Receive the host name from the client
5. Retrieve the client’s IP address from the receiveddatagram
6. Get the IP address mapped for the host name from the table.
7. Display the host name and corresponding IPaddress
8. Send the IP address for the requested host name to the client
9. Stop.
Client
1. Start
2. Create UDP datagramsocket.
3. Get the host name from the client
4. Send the host name to the server
5. Wait for the reply from the server
6. Receive the reply datagram and read the IP address for the requested hostname
7. Display the IPaddress.
8. Stop.
PROGRAM
DNSSERVER.java
import java.io.*;
import java.net.*;
public class DNSSERVER
{
public static void main(String arg[])throws IOException
{
String[] hosts = {"yahoo.com", "gmail.com","cricinfo.com", "facebook.com"};
String[] ip = {"68.180.206.184", "209.85.148.19","80.168.92.140", "69.63.189.16"};
System.out.println("Press Ctrl + C to Quit");
String str;
while (true)
{
DatagramSocket serversocket=new DatagramSocket(1362);
byte[] sd = new byte[1021];
byte[] rd = new byte[1021];
DatagramPacket recvpack = new DatagramPacket(rd, rd.length);
serversocket.receive(recvpack);
String sen = new String(recvpack.getData());
InetAddress client = recvpack.getAddress();
int port = recvpack.getPort();
str = sen.trim();
String capsent="NULL";
System.out.println("Request for host : " + str + “->from : “+client);
for (int i=0; i < hosts.length; i++)
{
if (hosts[i].equals(str))
{
capsent =ip[i];
break;
}
}
if(capsent.equals("NULL"))
{ capsent = "Host Not Found"; System.out.println("UNKNOWN HOST");}
sd = capsent.getBytes();
DatagramPacket pack = new DatagramPacket (sd, sd.length,client,port);
serversocket.send(pack);
serversocket.close();
}
}
}
udpdnsclient.java
import java.io.*;
import java.net.*;
public class udpdnsclient
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientsocket = new DatagramSocket();
InetAddress ipaddress;
ipaddress = InetAddress.getLocalHost();
byte[] senddata = new byte[1024];
byte[] receivedata = new byte[1024];
int port = 1362;
System.out.print("Enter the hostname : ");
String sentence = br.readLine();
senddata = sentence.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,senddata.length,ipaddress,port);
clientsocket.send(pack);
DatagramPacket recvpack =new DatagramPacket(receivedata,receivedata.length);
clientsocket.receive(recvpack);
String modified = new String(recvpack.getData());
System.out.println("IP Address: " + modified);
clientsocket.close();
}
}
OUTPUT: