Iterative Name Server Implementation Using Java

Iterative Name Server Implementation Using Java

Introduction:
                       The Domain Name System (DNS) is a hierarchical naming system built on a distributed database for computers, services, or any resource connected to the Internet or a private network. The Domain Name System makes it possible to assign domain names to groups of Internet resources and users in a meaningful way, independent of each entity's physical location. Because of this, World Wide Web (WWW) hyperlinks and Internet contact information can remain consistent and constant even if the current Internet routing arrangements change

                      The Domain Name System distributes the responsibility of assigning domain names and mapping those names to IP addresses by designating iterative name servers for each domain.

                       The Domain Name System is maintained by a distributed database system, which uses the client-server model. The nodes of this database are the name servers. Each domain has at least one authoritative DNS server that publishes information about that domain and the name servers of any domains subordinate to it. The top of the hierarchy is served by the root name servers, the servers to query when looking up (resolving) a top-level domain name

Iterative Name Server Implementation Using Java

Implementation:
  à Design the table with in the database as specified in the next section.
  à Implement individual server for each domain with in DNS.
  à Each server program is now in waiting state.
  à Implement client program which accepts URL from user, divides the URL into different domain and fetch 
       the IPs of the domains from the corresponding servers.
  à Combine the IPs received from different server and display them to the user.

Table Name : Client
Iterative Name Server Implementation Using Java


Client.java:
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;

class Client{
public static void main(String a[]){
   Socket clisock;
   DataInputStream input;
   PrintStream ps;
   String url,ip,s,u,p,str;
   int pno=5123;
   Connection con;
   Statement smt;
   ResultSet rs;
   boolean status=true;
   try{
   ip=s=p=u="\0";
   System.out.println("enter name to resolve");
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   url=br.readLine();
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("jdbc:odbc:NameServer");
   smt=con.createStatement();
   while(status){
      s="\0";
      StringTokenizer st=new StringTokenizer(url,".");
      if(st.countTokens()==1){status=false;}
      while(st.countTokens()>1)
        s=s+st.nextToken()+".";
        s=s.substring(0,s.length()-1).trim();
        u=st.nextToken();
        rs=smt.executeQuery("select port,ipadd from client where name='"+u+"'");
        if(rs.next()){
p=rs.getString(1);
pno=Integer.parseInt(p);
str=rs.getString(2);
url=s;
ip=str+"."+ip;
        }
        else {
clisock=new Socket("127.0.0.1",pno);
input=new DataInputStream(clisock.getInputStream());
ps=new PrintStream(clisock.getOutputStream());
ps.println(url);
p=input.readLine();
pno=Integer.parseInt(p);
str=input.readLine();
url=input.readLine();
ip=str+"."+ip;
smt.executeUpdate("insert into client values('"+u+"','"+str+"','"+p+"')");
        }
        ip=ip.substring(0,ip.length()-1).trim();
    }
    System.out.println("ip address is:"+ip);
    con.close();
  }
  catch(Exception e)
  {   System.err.println(e);   }
  }
}




Server1.java:

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
class Server1{
  public static void main(String a[]){
    ServerSocket sock;
    Socket client;
    DataInputStream input;
    PrintStream ps;
    String url,u,s;
    Connection con;
    Statement smt;
    ResultSet rs;
    try{
      s=u="\0";
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:NameServer");
      smt=con.createStatement();
      sock=new ServerSocket(5123);                                      
      while(true){
client=sock.accept();
input=new DataInputStream(client.getInputStream());
ps=new PrintStream(client.getOutputStream());
url=input.readLine();
StringTokenizer st=new StringTokenizer(url,".");
while(st.countTokens()>1)
   s=s+st.nextToken()+".";
   s=s.substring(0,s.length()-1).trim();
   u=st.nextToken();
   rs=smt.executeQuery("select port,ipadd from client where name=’"+u+"’");
   if(rs.next()){
      ps.println(rs.getString(1));
      ps.println(rs.getString(2));
      ps.println(s);
   }
   else{
      ps.println("Illegal address pleasr check the spelling again");
      con.close();
   }
}
   }
   catch(Exception e)
   {   System.err.println(e);   }
   }
}



  
Server2.java:

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
class Server1{
  public static void main(String a[]){
    ServerSocket sock;
    Socket client;
    DataInputStream input;
    PrintStream ps;
    String url,u,s;
    Connection con;
    Statement smt;
    ResultSet rs;
    try{
      s=u="\0";
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:NameServer");
      smt=con.createStatement();
      sock=new ServerSocket(5124);                                      
      while(true){
client=sock.accept();
input=new DataInputStream(client.getInputStream());
ps=new PrintStream(client.getOutputStream());
url=input.readLine();
StringTokenizer st=new StringTokenizer(url,".");
while(st.countTokens()>1)
   s=s+st.nextToken()+".";
   s=s.substring(0,s.length()-1).trim();
   u=st.nextToken();
   rs=smt.executeQuery("select port,ipadd from client where name=’"+u+"’");
   if(rs.next()){
      ps.println(rs.getString(1));
      ps.println(rs.getString(2));
      ps.println(s);
   }
   else{
      ps.println("Illegal address pleasr check the spelling again");
      con.close();
   }
}
   }
   catch(Exception e)
   {   System.err.println(e);   }
   }
}

  
Server3.java:

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
class Server1{
  public static void main(String a[]){
    ServerSocket sock;
    Socket client;
    DataInputStream input;
    PrintStream ps;
    String url,u,s;
    Connection con;
    Statement smt;
    ResultSet rs;
    try{
      s=u="\0";
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:NameServer");
      smt=con.createStatement();
      sock=new ServerSocket(5125);                                      
      while(true){
client=sock.accept();
input=new DataInputStream(client.getInputStream());
ps=new PrintStream(client.getOutputStream());
url=input.readLine();
StringTokenizer st=new StringTokenizer(url,".");
while(st.countTokens()>1)
   s=s+st.nextToken()+".";
   s=s.substring(0,s.length()-1).trim();
   u=st.nextToken();
   rs=smt.executeQuery("select port,ipadd from client where name=’"+u+"’");
   if(rs.next()){
      ps.println(rs.getString(1));
      ps.println(rs.getString(2));
      ps.println(s);
   }
   else{
      ps.println("Illegal address pleasr check the spelling again");
      con.close();
   }
}
   }
   catch(Exception e)
   {   System.err.println(e);   }
   }
}


Name Server: OUTPUT
C:\Documents and Settings\cse4y>e:
E:\>cd Lab
E:\Lab>cd NameServer
E:\Lab\NameServer>javac *.java
E:\Lab\NameServer>start java Server1
E:\Lab\NameServer>start java Server2
E:\Lab\NameServer>start java Server3
E:\Lab\NameServer>java Client
enter name to resolve
google.com
ip address is:12.10