0% found this document useful (0 votes)
9 views2 pages

Ex 9b

cn

Uploaded by

priya j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Ex 9b

cn

Uploaded by

priya j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Implementation of dns using UDP

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;

if (args.length == 0)
ipAddress = InetAddress.getLocalHost();
else
ipAddress = InetAddress.getByName(args[0]);

byte[] sendData = new byte[1024];


byte[] receiveData = new byte[1024];
int portAddr = 1362;

System.out.print("Enter the hostname: ");


String sentence = br.readLine();
sendData = sentence.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipAddress,


portAddr);
clientSocket.send(sendPacket);

DatagramPacket recvPacket = new DatagramPacket(receiveData, receiveData.length);


clientSocket.receive(recvPacket);

String modified = new String(recvPacket.getData()).trim();


System.out.println("IP Address: " + modified);
clientSocket.close();
}
}
udpdnsserver.java
import java.io.*;
import java.net.*;
import java.util.Hashtable;

public class udpdnsserver {

public static void main(String arg[]) throws IOException {

Hashtable<String, String> dnsTable = new Hashtable<>();


dnsTable.put("yahoo.com", "68.180.206.184");
dnsTable.put("gmail.com", "209.85.148.19");
dnsTable.put("cricinfo.com", "80.168.92.140");
dnsTable.put("facebook.com", "69.63.189.16");

System.out.println("Press Ctrl + C to Quit");

while (true) {
DatagramSocket serverSocket = new DatagramSocket(1362);
byte[] sendData = new byte[1021];
byte[] receiveData = new byte[1021];

DatagramPacket recvPacket = new DatagramPacket(receiveData, receiveData.length);


serverSocket.receive(recvPacket);

String requestedHost = new String(recvPacket.getData()).trim();


InetAddress clientAddress = recvPacket.getAddress();
int clientPort = recvPacket.getPort();

String response;
System.out.println("Request for host " + requestedHost);
response = dnsTable.getOrDefault(requestedHost, "Host Not Found");

sendData = response.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
clientAddress, clientPort);
serverSocket.send(sendPacket);

serverSocket.close();
}
}
}

You might also like