abc123
unknown
java
2 years ago
8.2 kB
1
Indexable
Never
1. Create Client and Server programs, where Client to accept number from user andpassed it to the Server, Server finds whether the number is prime and sends message to accordingly back to client. Client displays the result to the user. Code: //Client Side program import java.net.*; import java.io.*; public class primeCli{ public static void main(String[] args){ try{ System.out.println("Client ready!!"); Socket sc = new Socket("localhost",9806); BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a no. : "); int n = Integer.parseInt(br.readLine()); PrintWriter pw = new PrintWriter(sc.getOutputStream(),true); pw.println(n); BufferedReader b1 = new BufferedReader(new InputStreamReader(sc.getInputStream())); String str = b1.readLine(); System.out.println(str); } catch(Exception e){ e.printStackTrace(); } } } //Server side program import java.net.*; import java.io.*; public class primeSer{ public static void main(String[] args){ try{ System.out.println("Waiting for client ..."); ServerSocket ss = new ServerSocket(9806); Socket sc = ss.accept(); System.out.println("Connection Established!!"); BufferedReaderbr = new BufferedReader(new InputStreamReader(sc.getInputStream())); int n = Integer.parseInt(br.readLine()); String str=""; for(int i = 2;i <= n/2;i++){ if( n%i == 0){ str = "is not Prime"; break; } else{ str = "is Prime"; } } PrintWriter pw = new PrintWriter(sc.getOutputStream(),true); pw.println(n + " "+str); } catch(Exception e){ e.printStackTrace(); } } } Output: Server side output: Client side output: 2. Create a Java Socket Client application which runs on port 8080 and accepts a string from user and sends it to server. Accepts result returned by server and displays it back to user. Create a Java Socket Server application which accepts string from client and check whether it is palindrome or not and send result back to client. Solution: //Client Side program import java.net.*; import java.io.*; public class pallindromeCli{ public static void main(String[] args){ try{ System.out.println("Client ready.."); Socket sc = new Socket("localhost",9806); BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter String : "); String str = br.readLine(); PrintWriter pw = new PrintWriter(sc.getOutputStream(),true); pw.println(str); BufferedReader b1 = new BufferedReader(new InputStreamReader(sc.getInputStream())); String str1 = b1.readLine(); System.out.println(str1); } catch(Exception e){ e.printStackTrace(); } } } //Server Side program import java.net.*; import java.io.*; public class pallindromeSer{ public static String pallindrome(String str){ String str1=""; for(int i = str.length()-1; i>=0;i--){ str1 += str.charAt(i); } if(str.equals(str1)){ String ab = (str +" is a pallindrome."); return ab; } else{ String bc = (str +" is not a pallindrome."); return bc; } } public static void main(String[] args){ try{ System.out.println("Waiting for client..."); ServerSocket ss = new ServerSocket(9806); Socket sc = ss.accept(); System.out.println("Server Connected!!"); BufferedReaderbr = new BufferedReader(new InputStreamReader(sc.getInputStream())); String str = br.readLine(); PrintWriter pw = new PrintWriter(sc.getOutputStream(),true); pw.println("Server replies : "+ pallindrome(str)); } catch(Exception e){ e.printStackTrace(); } } } Output: Server side output: Client side output: 3. Write a RMI application to accept a number and display its factorial value, using remote method fact (int n). Code: //Client Side program import java.net.*; import java.io.*; public class pallindromeCli{ public static void main(String[] args){ try{ System.out.println("Client ready.."); Socket sc = new Socket("localhost",9806); BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter String : "); String str = br.readLine(); PrintWriter pw = new PrintWriter(sc.getOutputStream(),true); pw.println(str); BufferedReader b1 = new BufferedReader(new InputStreamReader(sc.getInputStream())); String str1 = b1.readLine(); System.out.println(str1); } catch(Exception e){ e.printStackTrace(); } } } //Server Side program import java.net.*; import java.io.*; public class pallindromeSer{ public static String pallindrome(String str){ String str1=""; for(int i = str.length()-1; i>=0;i--){ str1 += str.charAt(i); } if(str.equals(str1)){ String ab = (str +" is a pallindrome."); return ab; } else{ String bc = (str +" is not a pallindrome."); return bc; } } public static void main(String[] args){ try{ System.out.println("Waiting for client..."); ServerSocket ss = new ServerSocket(9806); Socket sc = ss.accept(); System.out.println("Server Connected!!"); BufferedReaderbr = new BufferedReader(new InputStreamReader(sc.getInputStream())); String str = br.readLine(); PrintWriter pw = new PrintWriter(sc.getOutputStream(),true); pw.println("Server replies : "+ pallindrome(str)); } catch(Exception e){ e.printStackTrace(); } } } Output: 4. Create a RMI Application which invokes remote method sum_digit(int n) which returns the sum of all the digits of a number sent by client. Code: //Rmi interface program import java.rmi.*; public interface sumInRmi extends Remote{ public int sum_digit(int n) throws RemoteException; } //Rmi class program import java.rmi.*; import java.rmi.server.*; public class sumClaRmi extends UnicastRemoteObject implements sumInRmi{ sumClaRmi() throws RemoteException{ super(); } public int sum_digit(int n){ int rem=0; int sum=0; while(n>0){ rem = n%10; sum = sum+rem; n = n/10; } return sum; } } //Rmi Server program import java.rmi.*; import java.rmi.registry.*; public class sumSerRmi{ public static void main(String[] args){ try{ sumInRmi obj = new sumClaRmi(); LocateRegistry.createRegistry(8004); Naming.rebind("rmi://localhost:8004"+"/sumd",obj); } catch(Exception e){ e.printStackTrace(); } } } ////Rmi Client program import java.rmi.*; import java.util.*; public class sumCliRmi{ public static void main(String[] args){ try{ sumInRmist = (sumInRmi)Naming.lookup("rmi://localhost:8004"+"/sumd"); Scanner in = new Scanner(System.in); System.out.println("Enter a number to find sum of its digit : "); int n = in.nextInt(); System.out.println(st.sum_digit(n)); } catch(Exception e){ e.printStackTrace(); } } } Output: