Question: the server code is in the provided images heres the client code: import java.io . * ; import java.net. * ; public class DIYAppWorker {

the server code is in the provided images
heres the client code:
import java.io.*;
import java.net.*;
public class DIYAppWorker {
private static final String SERVER_ADDRESS = "localhost"; // Controller address
private static final int PORT =16789; // Server port
public static void main(String[] args) throws Exception {
// Connect to the server
Socket socket = new Socket(SERVER_ADDRESS, PORT);
System.out.println("Connected to server.");
// Initialize the input and output streams
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
while (true){
// Receive the size of the slice from the server
String sliceSizeStr = br.readLine(); // Read actual slice size from server
if (sliceSizeStr == null || sliceSizeStr.isEmpty()){
// Handle unexpected end of stream or invalid data
System.out.println("Server closed the connection or sent invalid data.");
break; // Exit the loop or handle the error as needed
}
// Try parsing the slice size, and handle any exceptions
int sliceSize;
try {
sliceSize = Integer.parseInt(sliceSizeStr); // Parse slice size
} catch (NumberFormatException e){
System.err.println("Received invalid slice size from server: "+ sliceSizeStr);
break; // Exit the loop or handle the error as needed
}
if (sliceSize ==0){
// No more data to process (end of data)
break;
}
System.out.println("Worker received slice of size: "+ sliceSize);
// Initialize the sum for this slice
double partialSum =0;
// Read the numbers in the slice and calculate the sum
for (int i =0; i sliceSize; i++){
String numStr = br.readLine(); // Read each number in the slice
if (numStr == null){
// Handle end of stream or unexpected data
System.err.println("Unexpected end of data when reading numbers.");
break;
}
double num;
try {
num = Double.parseDouble(numStr); // Parse number
} catch (NumberFormatException e){
System.err.println("Invalid number format: "+ numStr);
continue; // Skip this number and continue with the next one
}
System.out.println("Worker received number: "+ num); // Debug: Show each number
partialSum += num; // Add the value to the partial sum
}
// Send the partial sum back to the server
pw.println(partialSum);
// Debug: Show the partial sum the worker is sending back to the server
System.out.println("Worker partial sum: "+ partialSum);
}
// After processing all slices, terminate the worker
System.out.println("Worker terminating.");
socket.close();
}
}
instructions: Write a distributed application that calculates the sum of a huge data set.
for the most part this code functions but there are some issues:
1. the server terminates by itself when it should not be doing that
2. after the workers are finished, the final sum should reset to 0, but it doesnt.
3. ive tried to make it work to where it can calculate the sum regardless of whether one worker is working or all of the workers are working with interesting problems as a result.
these are the problems I have been dealing with and I am rather lost on what to do next
 the server code is in the provided images heres the client

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!