Java Tutorial

 
 
 
 
//  Server

import java.net.*;
import java.io.*;
import java.util.*;
class eserver implements Runnable
{

    Socket socket;
    int id;       
    int count;
    public static void main(String args[])
    {
    int  count=0;
             try
        {
            ServerSocket s= new ServerSocket(13);

    while(true)
        {
            Socket socket=s.accept();
            eserver server=new eserver(socket,count);
            Thread t=new Thread(server);
            t.start();
        }
    }
        catch(Exception e)
        {
            System.out.println(\"Exception caught is::\"+e);    
        }    
    }

            eserver(Socket sa,int i)
    { 
        socket=sa;
                 id=i;
        }

public void run()
{
           int flag=0;StringTokenizer t;
           String id,ipAddress,str;   
           int i;
           try{     
                         InputStream is=socket.getInputStream();
        InputStreamReader isr=new InputStreamReader(is);
        BufferedReader br=new BufferedReader(isr);

                        String cls=br.readLine();            

                           System.out.println(\"Client Requested : \"+cls);  

                          if(cls.trim().equals(\"quit\"))
                          {
             System.out.println(\"Client has Disconnected.....\");
             br.close(); 
        }


        try{ 
               OutputStream op=socket.getOutputStream();    
               PrintWriter pw=new PrintWriter(op);  
               String clientid=cls;

               String val=\"\";
               String msg=\"\";    

     FileReader fr=new FileReader(\"iptable.txt\");
              BufferedReader bf=new BufferedReader(fr);              

              while(flag==0)
              {
               String b=bf.readLine();
                if(b==null) 
                break;

                t=new StringTokenizer(b);
                id=t.nextToken();
                ipAddress=t.nextToken();

               if(id.equals(clientid))  
      {

             System.out.println(\"URL          :: \"+id);
             System.out.println(\"IP Address :: \"+ipAddress);
                    val=\"*\";flag=1;msg=\"URL=\"+id+\" IP Address= \"+ipAddress;
             }

               else
               {
           flag=0;msg=\"Sorry, Given URL is not Found.....\";
               }

        }        

               bf.close();
     fr.close();

                  if(flag==1)                  
                  pw.println(msg);
                 else
                 pw.println(msg);  
                  pw.flush();
                  pw.close();


          }   //endtry2
                    catch(Exception e)
        {
            System.out.println(\"Exception caught is::\"+e);    
        }    

       }//endtry1
 catch(IOException e){}                

//end while 
       }//end run    

}

// Client 

import java.net.*;
import java.io.*;

public class eclient 
{


public static void main(String args[])
    {
                String  instr,str;


         try{
         while(true)
          {
                   InetAddress address=InetAddress.getByName(null);
          System.out.println(\"\\n\\n==========================\");    
                   System.out.println(\"Enter Name / Enter \'quit\'  to exit\");
          System.out.println(\"Client : \");

                  InputStreamReader isr=new InputStreamReader(System.in);
         BufferedReader br=new BufferedReader(isr);  


                  instr=br.readLine();
                  while(!instr.trim().equals(\"quit\"))
                  { 
                      Socket s=new Socket(address,13);             
                      OutputStream os= s.getOutputStream();
             OutputStreamWriter osw=new OutputStreamWriter(os);
                      PrintWriter pw=new PrintWriter(osw);

             pw.println(instr);
                      pw.flush();

                      InputStream inStream=s.getInputStream();
                      InputStreamReader ipr=new InputStreamReader(inStream);
                      BufferedReader brs=new BufferedReader(ipr);

                         str=brs.readLine();

                      System.out.println(\"Reply form Server is :  \"+str);
                      System.out.println(\"\\n\\n==========================\");    
                       System.out.println(\"Enter Name / Enter \'quit\'  to exit\");
             System.out.println(\"Client : \");
                      instr=br.readLine();    
                  }

          br.close();
          System.exit(0);                                   

       }
 }

    catch(Exception e)
        {
            System.out.println(\"Exception caught is::\"+e);    
        }    

     }

}    


OUTPUT :

iptabel.txt
----------

www.yahoo.com 127.0.0.1 13
www.hotmail.com 127.12.1.1 13
www.glsict.org 127.0.2.12 13
www.rediffmail.com 127.1.32.1 13



// Client
C:\\jdk1.1.3\\bin>java eclient

==========================
Enter Name / Enter \'quit\'  to exit
Client :
www.glsict.org
Reply form Server is :  URL=www.glsict.org IP Address= 127.0.2.12


==========================
Enter Name / Enter \'quit\'  to exit
Client :
quit

C:\\jdk1.1.3\\bin>


// Server 
C:\\jdk1.1.3\\bin>java eserver
Client Requested : www.glsict.org
URL          :: www.glsict.org
IP Address :: 127.0.2.12

Client has Disconnected.....

C:\\jdk1.1.3\\bin>