Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Wednesday, 27 July 2011

Program for Domain Name System (DNS) using TCP


Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientdns
{
            public static void main(String args[])
            {
            try
            {         
                        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                        Socket clsct=new Socket("127.0.0.1",139);

                        DataInputStream din=new DataInputStream(clsct.getInputStream());
                        DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
                        System.out.println("Enter the Address (IP or DOMAIN NAME):");
                        String str1=in.readLine();
                        dout.writeBytes(str1+'\n');
                        String str=din.readLine();
                        System.out.println("DOMAIN NAME or IP: "+str);            
                        clsct.close();
            }
            catch (Exception e)
            {
            System.out.println(e);
            }
            }
}
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverdns
{
            public static void main(String args[])
            {
            try
            {
                        ServerSocket obj=new ServerSocket(139);
                        Socket obj1=obj.accept();
                        while(true)
                        {
                                    DataInputStream din=new DataInputStream(obj1.getInputStream());
                                    DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
                                    String str=din.readLine();
                                    String ip[]={"165.165.80.80","165.165.79.1"};
                                    //String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
                                    String name[]={"www.skcet.ac.in","www.skcetlib.ac.in"};
                                    for(int i=0;i<ip.length;i++)
                                    {
                                                if(str.equals(ip[i]))
                                                {
                                                            dout.writeBytes(name[i]+'\n');
                                                            break;
                                                }
                                                else if(str.equals(name[i]))
                                                {
                                                            dout.writeBytes(ip[i]+'\n');
                                                            break;
                                                }
                                               
                                    }                     
                                    obj.close();
                        }
                       
            }
            catch(Exception e)
            {
                        System.out.println(e);
            }
            }
}
Output:
E:\networks>java Clientdns
Enter the Address (IP or DOMAIN NAME):
www.skcet.ac.in
DOMAIN NAME or IP: 165.165.80.80
E:\networks>java Clientdns
Enter the Address (IP or DOMAIN NAME):
165.165.79.1
DOMAIN NAME or IP: www.skcetlib.ac.in

No comments:

Post a Comment

Followers