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>

    Related Post:
  1. Biological Sequence Allignment Using Dynamic Programing

  2. Program to show the use of Bitwise Operator (|) OR

  3. Program to show an example of getting at characters in a String

  4. An applet program of showing new document using showDocument(URL) method

  5. Program to show the use of Comparison operator (==) for comparing Strings

  6. Program to display Floyds Triangle of a numbers

  7. Program for calculating area of a circle on server side,when a client sends request along with given radius and then displaying result on client side

  8. Program to show an example of creating a class and using its object

  9. Program of reading and writing primitive data

  10. File handling program to copy bytes from one file to another

  11. Program to convert the given temperature in faherenhiet to celsius using the following conversion formula C= F- 32/1.8

  12. Program to show some properties of StringBuffer object

  13. Program to read the price of an item in decimal form (like 75.95) and print the output in paise (like 7595 paise).

  14. Program to show the use of operator (>>) Shift Right, propagating the sign bit from the left

  15. Program to print some text on the same line using multiple print statements

  16. Program which calls the method sort(int []a) which throws the Exception ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException

  17. Program which simulate the dictionary in java

  18. CORBA based application for addition of two mathematical expressions of the form (ax + by) and displaying the result on client side

  19. Program that enabled applet that takes name of the user. Server returns back the number in uppercase clubbed with “HELLO” and display it on applet

  20. Program to read the ballots and count the votes cast for each candidate using an array variable count