1. To develop any distributed application through implementing client-server communication programs based on Java Sockets and RMI techniques.

 /*Assignment No:-01

Title: Design a distributed application which consist of a

server and client using threads.

*/

//MyClient.java

import java.net.*;

import java.io.*;

public class MyClient

{

public static void main (String args[])

{

int n,ch,ch1,fact;

String st,st1,st2,first,last;

 try

{

Socket s = new Socket(args[0], 2001);

DataInputStream in = new DataInputStream( s.getInputStream());

DataOutputStream out =new DataOutputStream( s.getOutputStream());

BufferedReader object = new BufferedReader(new InputStreamReader(System.in));

do

 {

 System.out.println("1.Factorial\n2.Adddition of digits\n3.String operations\n4.Exit\nEnter ur choice:");

 ch= Integer.parseInt(object.readLine());

out.writeUTF(Integer.toString(ch));

switch(ch)

{

case 1:

System.out.println("Enter a number=");

n=Integer.parseInt(object.readLine());

 out.writeUTF(Integer.toString(n));

fact=Integer.parseInt(in.readUTF());

System.out.println("Factorial of "+n+"is "+fact) ;

 break;

case 2:

System.out.println("Enter a number");

n=Integer.parseInt(object.readLine());

out.writeUTF(Integer.toString(n));

int sum=Integer.parseInt(in.readUTF());


System.out.println("Addition of digits of "+n+" is "+sum) ;

 break;

case 3:

do

{

System.out.println("1.concatenation\n2.substring\n3.palindrome \n4.Exit\nEnter ur choice:");

ch1=Integer.parseInt(object.readLine());

out.writeUTF(Integer.toString(ch1));

switch(ch1)

 {

case 1:


System.out.println("Enter First string");

 st1=object.readLine();


out.writeUTF(st1);


System.out.println("Enter second string");

 st2=object.readLine();


out.writeUTF(st2);


st=in.readUTF();


System.out.println("Concatenated String of "+st1+" and "+st2+"is :: "+st) ;

 break;

case 2:


System.out.println("Enter The string");

 st1=object.readLine();


out.writeUTF(st1);


System.out.println("Enter The Start Position of the substring");


first=object.readLine();


out.writeUTF(first);


System.out.println("Enter The end Position of the substring");


last=object.readLine();


out.writeUTF(last);



st=in.readUTF();


System.out.println("Substring of string "+st1+" is :: "+st) ;

 break;

case 3:


System.out.println("Enter The string");


st=object.readLine();


out.writeUTF(st);


n=Integer.parseInt(in.readUTF());

 if(n==0)

System.out.println("string"+st+" is Palindrome") ;

 else

System.out.println("string"+st+" is not Palindrome") ;

 break;

}

}while(ch1>0 && ch1!=4);

break;

 case 4:

System.exit(0);

 break;

 }

 }while(ch>0);

 }

 catch (Exception e)

 {

 System.out.println("Exception:"+e.getMessage());

}

}

}



//MyServer.java


 import java.net.*;

 import java.io.*;

 public class MyServer extends Thread

 {

 DataInputStream in;

 DataOutputStream out;

 Socket clientSocket;

 int n,ch,ch1,f,l;

 String st,st1,st2;

 public MyServer()

 {

 try

 {

 ServerSocket listenSocket = new ServerSocket(2001);

 System.out.println("\nServer is Running") ;

 clientSocket = listenSocket.accept();

 System.out.println("\n Client is Connected") ;

 in = new DataInputStream( clientSocket.getInputStream());

 out =new DataOutputStream( clientSocket.getOutputStream());

 this.start();

 }

 catch(IOException e)

 {

 System.out.println("Connection:"+e.getMessage());

 }

 }

 public void run()

 {

 try

 {

 while(true)

 {

 ch =Integer.parseInt(in.readUTF());

 switch(ch)

 {

 case 1:

 int fact= 1;

 n =Integer.parseInt(in.readUTF());

 for (int i= 1; i<=n; i++)

 {

 fact=fact*i;

 }

 out.writeUTF(Integer.toString(fact));

 break;

 case 2:


 int sum=0,rem;

 n =Integer.parseInt(in.readUTF());

 while(n!=0)

 {

 rem = n%10;

 n = n/10;

 sum=sum + rem;

 }

 out.writeUTF(Integer.toString(sum));

 break;

 case 3:


 while(true)

 {

 ch1 =Integer.parseInt(in.readUTF());

 switch(ch1)

 {

 case 1:

 st1=in.readUTF();

 st2=in.readUTF();

 st=st1.concat(st2);

 out.writeUTF(st);

 break;

 case 2:

 st1=in.readUTF();

 f =Integer.parseInt(in.readUTF());

 l =Integer.parseInt(in.readUTF());

 st=st1.substring(f,l);

 out.writeUTF(st);

 st=null;

 break;

 case 3:

 st=in.readUTF();

 st1 = new StringBuffer(st).reverse().toString();

 n=st.compareTo(st1);

 out.writeUTF(Integer.toString(n));

 break;

 }

 }


 }


 }


 }

 catch (Exception e)

 {

 System.out.println("Exception :"+e.getMessage());

 }

 }

 public static void main (String args[])

 {

 try

 {

 MyServer s=new MyServer();



 }

 catch(Exception e)

 {

 System.out.println("Listen socket:"+e.getMessage());

 e.printStackTrace();

 }

 }

 } 

Comments

Popular posts from this blog

2. To develop any distributed application using Message Passing Interface (MPI).