1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

A Simple Client-Server Model Implementation

Discussion in 'Java' started by pradeep, May 29, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Here is a simple implementation of client-server using Java. There are two java files, one for client code and one for server code. The code is quite self-explanatory.

    FactorialClient.java
    Code:
     /*
     **    A TCP based client that will send the number entered by the user for factorial
     **    calculation to a server
     **    @author: Tanaz Kerawala
     **    @author: S Pradeep
     **    @date: 5/29/2007
     */
     
     import java.io.*;
     import java.net.*;
     
     class FactorialClient
     {
         public static void main(String arg[])
         {
             int port=9999;
             Socket s;
             String msg="";
             try
             {
                 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                 InetAddress addr=InetAddress.getByName(null);
                 s=new Socket(addr,port);
                 OutputStreamWriter osw=new OutputStreamWriter(s.getOutputStream());
                 PrintWriter pw=new PrintWriter(osw);
                 BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream()));
                 System.out.print("Enter a Number :  ");
                 String str=br.readLine();
     
                 pw.println(str);
                 pw.flush();
                 msg=br1.readLine();
                 System.out.println("Answer from server : ");
                 System.out.println(msg);
             }
             catch(Exception e)
             {    
                 // Ignore
             }
         }
     }
     
    FactorialServer.java
    Code:
     /*
     **    A TCP based server that will calculate the factorial for client.
     **    @author: Tanaz Kerawala
     **    @author: S Pradeep
     **    @date: 5/29/2007
     */
     
     import java.io.*;
     import java.net.*;
     
     class FactorialServer implements Runnable
     {
         Socket s;
         int id;
         public static void main(String arg[])
         {
              int port=9999,count=0;
             try
             {
                 // create new socket    
                 ServerSocket ss=new ServerSocket(port);
                 System.out.println("Waiting for client");
                 while(true)
                 { 
                     Socket s=ss.accept();
                     FactorialServer serve=new FactorialServer(s,count);
                     // launch new thread
                     Thread t=new Thread(serve);
                     t.start();
                 }
             }
             catch(Exception e)
             {    
                 // Ignore error
                 System.out.println("Error");
             }
         }
     
     
         FactorialServer(Socket s,int id)
         {
             this.s = s;
             this.id = id;
         }
         
         public void run()
         {
             try
             {
                 BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                 PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())) ;
                 String str=br.readLine();
                 int n=Integer.parseInt(str);
                 int i;
                 long f=1;
                 System.out.println("Number sent by client: " + n);
                 for(i = 2; i <= n; i++)
                     f = f * i;
                 pw.println("Factorial is : "+f);                            
                 pw.flush();
             }
             catch(Exception e)
             {    
                 // Ignore error
                 System.out.println("Thread: Error");
             }
         }
     }
     
     
  2. msi_333

    msi_333 New Member

    Joined:
    May 29, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    Thanks ,,
    Good work :D
     
  3. somtherocker

    somtherocker New Member

    Joined:
    May 8, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Thank..
    Its a very nice program...
     
  4. newindiasolutions

    newindiasolutions New Member

    Joined:
    Apr 28, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Local SEO Services in Delhi, Search Engine Optimiz
    Location:
    New India Solutions, Plot No.12,Block-J, Dharampur
    Home Page:
    Client server chatting software is based in socket programming. Its a very interesting programming but more complected.
     

Share This Page