Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Explanation: We will be creating 5 different projects in our eclipse or NetBeans IDE We will be using the microservices principle in java Create a
Explanation:
We will be creating 5 different projects in our eclipse or NetBeans IDE
We will be using the microservices principle in java
Create a new project called client1
inside the src folder, create the following java class
Client1.java
package client1; import java.io.*; import java.net.*; import java.util.Scanner; public class Client1 { public static void main(String argv[]) throws Exception { long startTime = System.currentTimeMillis(); String ip = "localhost"; int port = 9999; Socket s = new Socket(ip,port); System.out.println("Server Connected..."); System.out.print("\nEnter the range : "); Scanner sc = new Scanner(System.in); int range = sc.nextInt(); System.out.println(); System.out.println("Data from Load Balance Server in Client 1 is: "); for(int i=1;i<=range;i++) { int flag=0; for(int j=2;j
Create a new project called client2
inside the src folder, create the following java class
Client2.java
package client2; import java.io.*; import java.net.*; import java.util.Scanner; public class Client2 { public static void main(String argv[]) throws Exception { package loadbalancer; import java.io.*; import java.net.*; public class LoadBalanceServer { public static void main(String argv[]) throws Exception { System.out.println(""); System.out.println("Load Balance Server is Started..."); ServerSocket ss = new ServerSocket(9999); System.out.println(""); System.out.println("Load Balance Server is Waiting for Servers..."); Socket server1 = ss.accept(); System.out.println(""); System.out.println("Server 1 Connected to the Load Balance Server..."); BufferedReader br3=new BufferedReader(new InputStreamReader(server1.getInputStream())); Socket server2 = ss.accept(); System.out.println(""); System.out.println("Server 2 Connected to the Load Balance Server..."); BufferedReader br4 =new BufferedReader(new InputStreamReader(server2.getInputStream())); System.out.println(""); System.out.println("Load Balance Server is Waiting for Clients..."); Socket s = ss.accept(); System.out.println(""); System.out.println("Client 1 Connected..."); System.out.println(""); BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); int range = br.read(); System.out.print("Given Range from the Client 1 is: "); System.out.println(+range); System.out.println(""); System.out.println("Range sent to Server 1 to calculate the PRIME NUMBER"); OutputStreamWriter os1 = new OutputStreamWriter(server1.getOutputStream()); os1.write(range); os1.flush(); System.out.println(); int time = br.read(); System.out.println("Result Collected from Server 1 and Sent back to Client 1..."); System.out.println(""); System.out.print("Elapsed Time For Client 1 is: 0."); System.out.print(+time); System.out.println(" seconds"); Socket s2 = ss.accept(); System.out.println(""); System.out.println("Client 2 Connected..."); BufferedReader br1=new BufferedReader(new InputStreamReader(s2.getInputStream())); int range1 = br1.read(); System.out.println(""); System.out.print("Given Range from the Client 2 is: "); System.out.println(+range1); System.out.println(""); System.out.print("Range sent to Server 2 to calculate the PRIME NUMBER"); System.out.println(""); OutputStreamWriter os2 = new OutputStreamWriter(server2.getOutputStream()); os2.write(range1); os2.flush(); System.out.println(); int time2 = br.read(); System.out.println("Result Collected from Server 2 and Sent back to Client 2..."); System.out.println(""); System.out.print("Elapsed Time For Client 2 is: 0."); System.out.print(+time2); System.out.println(" seconds"); System.out.println(""); int avrgTime = ((time+time2)/2); System.out.print("Average Elapsed time for Clients is: 0."); System.out.print(avrgTime); System.out.print(" seconds"); } }package loadbalancer; import java.io.*; import java.net.*; public class LoadBalanceServer { public static void main(String argv[]) throws Exception { System.out.println(""); System.out.println("Load Balance Server is Started..."); ServerSocket ss = new ServerSocket(9999); System.out.println(""); System.out.println("Load Balance Server is Waiting for Servers..."); Socket server1 = ss.accept(); System.out.println(""); System.out.println("Server 1 Connected to the Load Balance Server..."); BufferedReader br3=new BufferedReader(new InputStreamReader(server1.getInputStream())); Socket server2 = ss.accept(); System.out.println(""); System.out.println("Server 2 Connected to the Load Balance Server..."); BufferedReader br4 =new BufferedReader(new InputStreamReader(server2.getInputStream())); System.out.println(""); System.out.println("Load Balance Server is Waiting for Clients..."); Socket s = ss.accept(); System.out.println(""); System.out.println("Client 1 Connected..."); System.out.println(""); BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); int range = br.read(); System.out.print("Given Range from the Client 1 is: "); System.out.println(+range); System.out.println(""); System.out.println("Range sent to Server 1 to calculate the PRIME NUMBER"); OutputStreamWriter os1 = new OutputStreamWriter(server1.getOutputStream()); os1.write(range); os1.flush(); System.out.println(); int time = br.read(); System.out.println("Result Collected from Server 1 and Sent back to Client 1..."); System.out.println(""); System.out.print("Elapsed Time For Client 1 is: 0."); System.out.print(+time); System.out.println(" seconds"); Socket s2 = ss.accept(); System.out.println(""); System.out.println("Client 2 Connected..."); BufferedReader br1=new BufferedReader(new InputStreamReader(s2.getInputStream())); int range1 = br1.read(); System.out.println(""); System.out.print("Given Range from the Client 2 is: "); System.out.println(+range1); System.out.println(""); System.out.print("Range sent to Server 2 to calculate the PRIME NUMBER"); System.out.println(""); OutputStreamWriter os2 = new OutputStreamWriter(server2.getOutputStream()); os2.write(range1); os2.flush(); System.out.println(); int time2 = br.read(); System.out.println("Result Collected from Server 2 and Sent back to Client 2..."); System.out.println(""); System.out.print("Elapsed Time For Client 2 is: 0."); System.out.print(+time2); System.out.println(" seconds"); System.out.println(""); int avrgTime = ((time+time2)/2); System.out.print("Average Elapsed time for Clients is: 0."); System.out.print(avrgTime); System.out.print(" seconds"); } }
Create a new project called server1
inside the src folder, create the following java class
Server1.java
package server1; import java.io.*; import java.net.*; public class Server1 { public static void main(String argv[]) throws Exception { String ip = "localhost"; int port = 9999; Socket server1 = new Socket(ip,port); System.out.println(""); System.out.println("Load Balancer is Connected..."); BufferedReader br=new BufferedReader(new InputStreamReader(server1.getInputStream())); int range = br.read(); System.out.println(""); System.out.print("Given Range by the Client 1 From The Load Balancer is: "); System.out.println(range); System.out.println("Prime Number of The given Range Calculated and Send to the Load balancer..."); for(int i=1;i<=range;i++) { int flag=0; for(int j=2;j
Create a new project called server2
inside the src folder, create the following java class
Server2.java
package server2; import java.io.*; import java.net.*; public class Server2 { public static void main(String argv[]) throws Exception { String ip = "localhost"; int port = 9999; Socket server2 = new Socket(ip,port); System.out.println(""); System.out.println("Load Balancer is Connected..."); BufferedReader br=new BufferedReader(new InputStreamReader(server2.getInputStream())); int range1 = br.read(); System.out.println(""); System.out.print("Given Range by the Client 2 From The Load Balancer is: "); System.out.println(range1); System.out.println("Prime Number of The given Range Calculated and Send to the Load balancer..."); for(int i=1;i<=range1;i++) { int flag=0; for(int j=2;j
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started