Write a Java application that simulates the Domain Name Service (DNS) functional
ID: 3790367 • Letter: W
Question
Write a Java application that simulates the Domain Name Service (DNS) functionality using UDP sockets. DNS translates between URLs and IP addresses (both ways). Unlike the real DNS protocol which follows RFC 1034, your program will use APIs provided by Java to do the name resolution. In addition, you would only need to translate a URL to an IP address alone. To see how DNS works, open a command window and type nslookup. At the prompt, enter a URL (for e.g., toolman.wiu.edu). You should see the URL translated to an IP address. C:>nslookup www.wiu.edu Server: UnKnown Address: 192.168.16.2 Name: www.wiu.edu.localdomain Addresses: 143.43.221.130 143.43.221.130 Client Specification Write a java program with the name DNSClient.java that carries out the following. The client should be run as follows: java DNSClient servername:port_num [optional argument] servername is the name or IP address of the server, and port_num is the port number on which the server is listening on. If no other arguments are specified, the client should run a loop to prompt the user to enter a URL, and resolve the URL to an IP address. The client should then be terminated by typing in “exit” or by pressing Control-C. If an URL is specified as an optional argument, the client should resolve the URL to an IP address and exit the program. If the URL resolution is successful, both the URL and the resolved IP address should be shown to the user. In case of failure, an appropriate error message (for e.g., “cannot resolve host name”) should be shown to the user. Server Specification Write a java program with the name DNSServer.java that carries out the following. The server should be run as follows: java DNSServer port_num where port_num is the port number to which the server is bound and listening for client requests. The server should passively listen for client requests and resolve URLs as the requests come in. If the requested URL is a fully qualified domain name (e.g. www.wiu.edu), then the resolution should be done without any modification of the request. However, if the requested string is not a fully qualified domain name, then the string should be treated as follows. Any string that does not have a suffix should be treated with the default domain name wiu.edu. Any string that is of the form x.y should be treated as www.x.y. For e.g., “toolman” should be treated as “toolman.wiu.edu” and “google.com” should be treated as www.google.com. Both the URL and the resolved IP address should be then returned to the client. In case of a URL that cannot be resolved, an appropriate error message should be returned to the client. Name Resolution Name resolution by the server should be carried out as follows: the server should first check a file called “hosts.txt” to see if the requested domain name is an entry in the file. The file should contain two columns, one for the domain name and the second for the IP address. Sample “hosts.txt” file: www.netsolutions.com 101.12.100.24 ww.wiu.edu 10.10.125.43 If the file does not contain the requested host name, the server should use the InetAddress.getbyName() method to resolve the IP address. If this too fails, then an appropriate error message to should be sent to the client. What to submit Upload your code in WesternOnline as well as toolman.wiu.edu. Use the submit_cs420 program to submit your source code (both DNSClient.java and DNSServer.java) only on toolman. For this assignment, you should use the command submit_cs420 –s hw2 DNSClient.java submit_cs420 –s hw2 DNSServer.java to submit your program. To verify submission, use the command submit_cs420 –l hw4 Note: DO NOT hard code port numbers or give an absolute path name for the hosts file (e.g.,, C:dirhosts.tx)t in your client program. This beats the purpose of dynamically providing the server port number to the client via command line argument. Note 2: DO NOT have package declarations for your class files. This will make it difficult for me to grade your submission on Toolman. To test the program on Toolman, run your server on a port number between 5900 and 5999. To avoid conflict with other students in the class, use the following port selection rule port number = 5900 + (WIU ID mod 100)
Explanation / Answer
import java.io.*;
import java.net.*;
import java.util.*;
class Clientdns12
{
public static void main(String args[])
{
try
{
DatagramSocket client=new DatagramSocket();
InetAddress addr=InetAddress.getByName("127.0.0.1");
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the DOMAIN NAME or IP adress:");
String str=in.readLine();
sendbyte=str.getBytes();
DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,1309);
client.send(sender);
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
client.receive(receiver);
String s=new String(receiver.getData());
System.out.println("IP address or DOMAIN NAME: "+s.trim());
client.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverdns12
{
public static void main(String args[])
{
try
{
DatagramSocket server=new DatagramSocket(1309);
while(true)
{
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
server.receive(receiver);
String str=new String(receiver.getData());
String s=str.trim();
//System.out.println(s);
InetAddress addr=receiver.getAddress();
int port=receiver.getPort();
String ip[]={"165.165.80.80","165.165.79.1"};
String name[]={"www.aptitudeguru.com","www.downloadcyclone.blogspot.com"};
for(int i=0;i<ip.length;i++)
{
if(s.equals(ip[i]))
{
sendbyte=name[i].getBytes();
DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
server.send(sender);
break;
}
else if(s.equals(name[i]))
{
sendbyte=ip[i].getBytes();
DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
server.send(sender);
break;
}
}
break;
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.