0% found this document useful (0 votes)
36 views

Program For Domain Name System (DNS) Using UDP: Client

This document describes a program for a basic domain name system (DNS) using UDP. The client program allows a user to enter a domain name or IP address and sends a request to the server. The server program receives requests, checks the input against hardcoded domain name and IP address mappings, and returns the corresponding IP address or domain name to the client. The programs demonstrate a simple implementation of DNS lookup and name resolution using UDP datagrams for communication.

Uploaded by

mtkkumaran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Program For Domain Name System (DNS) Using UDP: Client

This document describes a program for a basic domain name system (DNS) using UDP. The client program allows a user to enter a domain name or IP address and sends a request to the server. The server program receives requests, checks the input against hardcoded domain name and IP address mappings, and returns the corresponding IP address or domain name to the client. The programs demonstrate a simple implementation of DNS lookup and name resolution using UDP datagrams for communication.

Uploaded by

mtkkumaran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program for Domain Name System (DNS) using UDP

Client: 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) 2

{ System.out.println(e); } } } Output I:\ex>java Serverdns12 I:\ex>java Clientdns12 Enter the DOMAIN NAME or IP adress: 165.165.80.80 IP address or DOMAIN NAME: www.aptitudeguru.com I:\ex>java Clientdns12 Enter the DOMAIN NAME or IP adress: www.downloadcyclone.blogspot.com IP address or DOMAIN NAME: 165.165.79.1

You might also like