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 SERVERADDRESS "localhost"; Controller address
private static final int PORT ; Server port
public static void mainString args throws Exception
Connect to the server
Socket socket new SocketSERVERADDRESS, PORT;
System.out.printlnConnected to server.";
Initialize the input and output streams
BufferedReader br new BufferedReadernew InputStreamReadersocketgetInputStream;
PrintWriter pw new PrintWritersocketgetOutputStream true;
while true
Receive the size of the slice from the server
String sliceSizeStr brreadLine; Read actual slice size from server
if sliceSizeStr null sliceSizeStr.isEmpty
Handle unexpected end of stream or invalid data
System.out.printlnServer 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.parseIntsliceSizeStr; Parse slice size
catch NumberFormatException e
System.err.printlnReceived invalid slice size from server: sliceSizeStr;
break; Exit the loop or handle the error as needed
if sliceSize
No more data to process end of data
break;
System.out.printlnWorker received slice of size: sliceSize;
Initialize the sum for this slice
double partialSum ;
Read the numbers in the slice and calculate the sum
for int i ; i sliceSize; i
String numStr brreadLine; Read each number in the slice
if numStr null
Handle end of stream or unexpected data
System.err.printlnUnexpected end of data when reading numbers.";
break;
double num;
try
num Double.parseDoublenumStr; Parse number
catch NumberFormatException e
System.err.printlnInvalid number format: numStr;
continue; Skip this number and continue with the next one
System.out.printlnWorker received number: num; Debug: Show each number
partialSum num; Add the value to the partial sum
Send the partial sum back to the server
pwprintlnpartialSum;
Debug: Show the partial sum the worker is sending back to the server
System.out.printlnWorker partial sum: partialSum;
After processing all slices, terminate the worker
System.out.printlnWorker 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:
the server terminates by itself when it should not be doing that
after the workers are finished, the final sum should reset to but it doesnt.
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
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
