Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday 6 April 2019

TCP Factorial (Modification)

Question Label Internal 5 TCP 
Write a Java program to implement client-server interaction using connection-oriented sockets. 
 The client must send a number to a server as a request. The server must process the request  as a thread and return the factorial of the requested number as a response to the client.
   If a negative number is sent from the client, the server must throw a user-defined exceptionnamed NegativeNumberException.


Server Side
// Java implementation of  Server side 
// It contains two classes : Server and 
//ClientHandler 
// Save file as Server.java 
  
import java.io.*; 
import java.net.*; 
//Exception Class
class NegativeNumberException extends Exception {
private String op;

   public NegativeNumberException(String op) {
this.op = op;
}

public String toString() {
return "NegativeNumberException: Operation Factorial is Denied by server";
}
}

// Server class 
public class ServerFactorial  

    public static void main(String[] args) throws IOException  
    { 
        // server is listening on port 5080
        ServerSocket ss = new ServerSocket(5080); 
         Socket s = null; 
              
            try 
            { 
                // socket object to receive incoming client requests 
                s = ss.accept(); 
                     
                // obtaining input and out streams 
                DataInputStream dis = new DataInputStream(s.getInputStream()); 
                DataOutputStream dos = new DataOutputStream(s.getOutputStream()); 
                  
                // create a new thread object 
                Thread t = new ClientHandle(s, dis, dos); 
  
                // Invoking the start() method 
                t.start();         
            } 
            catch (Exception e){ 
                s.close(); 
                e.printStackTrace(); 
            } 
        } 
    } 

  
// ClientHandler class 
class ClientHandle extends Thread{
final DataInputStream dis; 
    final DataOutputStream dos; 
    final Socket s; 
      
  
    // Constructor 
    public ClientHandle(Socket s, DataInputStream dis, DataOutputStream dos)  
    { 
        this.s = s; 
        this.dis = dis; 
        this.dos = dos; 
    } 
  
    @Override
    public void run()  
    {  
        String received; 
        String toreturn; 
  int num,count = 1;
             try { 
  
                // Ask user what he wants 
                dos.writeUTF("Press any number to get Factorial..\n");
                // receive the answer from client 
                received = dis.readUTF(); 
                
                num=Integer.parseInt(received);
                try{
                if (num<0) {
    throw new NegativeNumberException(received);
    }
                }catch(Exception e) {
                toreturn=e.toString();
                dos.writeUTF(toreturn);
                System.out.println("Closing this connection."); 
                        this.s.close(); 
                                      }
                if(num<=9) {
                count=1;
                for(int i=1;i<=num;i++) {
                count=count*i;
                }
                 // write the 
                // answer from the client 
                toreturn = "Factorial of "+num+"="+count; 
                dos.writeUTF(toreturn);  
                }
      //Today's Modification Ma'am Asked
     //If the number is Greather than 9 Step //Down It to One's Place And
// Find It one's place FFactorial
//Example: 24=>4 => Factorial Of 4=24
                else {
                count=1;
                int n=num%10;
                for(int i=1;i<=n;i++) {
                count=count*i;
                }
                 // write the 
                // answer from the client 
            toreturn="Step Down the num:"+num+" to: "+n+"\n Factorial="+count;
            dos.writeUTF(toreturn);
                }
            

                Thread.sleep(1000); 
             }catch(Exception ef)
             {
            ef.printStackTrace();
             }
        try
        { 
            // closing resources 
            this.dis.close(); 
            this.dos.close(); 
              
        }catch(IOException ee){ 
            ee.printStackTrace(); 
        }  
        }
    }



Client Side:
  
import java.io.*; 
import java.net.*; 
import java.util.Scanner; 
  
// Client class

public class ClientFactorial  

    public static void main(String[] args) throws IOException  
    { 
        try
        { 
            Scanner scn = new Scanner(System.in); 
              
            // getting localhost ip 
            InetAddress ip = InetAddress.getByName("localhost"); 
      
 // establish the connection with server port 5080 
            Socket s = new Socket(ip, 5080); 
      
  // obtaining input and out streams 
            DataInputStream dis = new DataInputStream(s.getInputStream()); 
            DataOutputStream dos = new DataOutputStream(s.getOutputStream()); 
      
                System.out.println(dis.readUTF()); 
                String tosend = scn.nextLine(); 
                dos.writeUTF(tosend);             
                String received = dis.readUTF(); 
                System.out.println(received);
                              
        }catch(Exception e){ 
            e.printStackTrace(); 
        } 
    } 


Note: Green mark one is Modification Asked in B1 Lab Ia.

3 comments:

  1. Server

    package socket;

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    class ServerSoc extends Thread{
    DataOutputStream dout;
    int v;
    public ServerSoc(DataOutputStream d,int v) {
    this.dout=d;
    this.v=v;
    }
    @Override
    public void run() {
    try {
    if(v<0)
    throw new NegativeNumberException();
    int res=1;
    while(v!=0) {
    res*=v--;
    }
    System.out.println(res);
    dout.writeInt(res);
    System.out.println("Server responded");
    }catch (Exception e) {
    // TODO: handle exception
    System.out.println(e);
    }
    }
    }
    public class server {
    public static void main(String[] args) throws IOException {
    ServerSocket ss=new ServerSocket(3000);
    while(true) {
    Socket s=null;
    s=ss.accept();
    DataInputStream din=new DataInputStream(s.getInputStream());
    DataOutputStream dout=new DataOutputStream(s.getOutputStream());
    int v=din.readInt();
    new ServerSoc(dout, v).start();
    }
    }
    }
    class NegativeNumberException extends Exception{
    @Override
    public String toString() {
    return "NegativeNumberException";
    }
    }


    Client


    package socket;

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;

    public class client {
    public static void main(String[] args){
    try {
    Socket s=new Socket("localhost", 3000);
    Scanner sc=new Scanner(System.in);
    DataOutputStream dout=new DataOutputStream(s.getOutputStream());
    DataInputStream din=new DataInputStream(s.getInputStream());
    System.out.println("Enter Number");
    dout.writeInt(sc.nextInt());
    int res=din.readInt();
    System.out.println("Results from server " + res);
    }catch (Exception e) {
    // TODO: handle exception
    System.out.println(e);
    e.printStackTrace();
    }
    }
    }

    ReplyDelete
  2. For the same problem ma'am told another modification today

    Client send a number
    Server should check whether it is a palindrome or not if it is palindrome then server should reply can't display palindrome
    If no then find its factorial

    ReplyDelete