1) In Java Connectionless Socket Programming, how do I make sure all the packets from my 5 processors have been received and are being stored in an array as they arrive? Basically, I want to collect all the partial sums that are being sent by all the processes (5 of them) that are part of the multicast group and store those partial sums in an array on which I will then perform a few operations.
2) Also, I would appreciate ideas for how to find the index of a given element of an array of integers in this same code. One of the operations I need to perform is to find the Max of my array and retrieve its index.
Below is a snippet of my thread code that has the receiving methods.
readThread(InetAddress g, int port){
group = g;
multicastPort = port;
}
public void run(){
try {
MulticastSocket readSocket = new MulticastSocket(multicastPort);
readSocket.joinGroup(group);
while (true) {
byte[] recvBuffer = new byte[MAX_MSG_LEN];
DatagramPacket packet = new DatagramPacket(recvBuffer, recvBuffer.length, group, multicastPort);
readSocket.receive(packet);
String rString = new String(packet.getData());
String message = new String(rString.getBytes()).trim(); // Process the received message before use here
StringTokenizer stk = new StringTokenizer(message, ",");
int recvdProcessID = Integer.parseInt(stk.nextToken());
int recvdPartialSum = Integer.parseInt(stk.nextToken());
System.out.println("\n"+recvdProcessID+" "+recvdPartialSum);
int[] arrayPartialSum = new int[multicastSenderReceiver.numProcesses]; // array to store partial sums
/* Store each received partial sum in an array of partial
sums at index corresponding to the respective */
arrayPartialSum[recvdProcessID] = recvdPartialSum;
// For debug, here is another way of listing the elements of the array of partial sums.
System.out.println("\nHere is the array of partial sums: ");
for (int element: arrayPartialSum){
System.out.println("\n"+element);
}
// Compute and Display the sum of all the partial sums:
int grandTotal = 0;
for (int s: arrayPartialSum) {
grandTotal += s;
}
System.out.println("\nGrand Total: "+grandTotal);
/*Finding the maximum value in the array of partial sums */
int maximumSum = 0;
maximumSum = maximum.Max(arrayPartialSum);
System.out.println("The Maximum of all partial sums is"+maximumSum);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
UPDATE: I moved a few elements out of the while(true) loop, but now I have a new type of error at compilation. It shows certain lines and say "Unreachable statement." Any ideas?
public void run(){
try {
MulticastSocket readSocket = new MulticastSocket(multicastPort);
readSocket.joinGroup(group);
int[] arrayPartialSum = new int[multicastSenderReceiver.numProcesses]; // array to store partial sums
int grandTotal = 0;
while (true) {
byte[] recvBuffer = new byte[MAX_MSG_LEN];
DatagramPacket packet = new DatagramPacket(recvBuffer, recvBuffer.length, group, multicastPort);
readSocket.receive(packet);
String rString = new String(packet.getData());
String message = new String(rString.getBytes()).trim(); // Process the received message before use here
StringTokenizer stk = new StringTokenizer(message, ",");
int recvdProcessID = Integer.parseInt(stk.nextToken());
int recvdPartialSum = Integer.parseInt(stk.nextToken());
System.out.println("\n"+recvdProcessID+" "+recvdPartialSum);
/* Store each received partial sum in an array of partial
sums at index corresponding to the respective */
arrayPartialSum[recvdProcessID] = recvdPartialSum;
}
// Compute and Display the sum of all the partial sums:
for (int s: arrayPartialSum) {
grandTotal += s;
}
System.out.println("\nGrand Total: "+grandTotal);
/*Finding the maximum value in the array of partial sums */
int maximumSum = 0;
maximumSum = maximum.Max(arrayPartialSum);
System.out.println("The Maximum of all partial sums is"+maximumSum);
// For debug, here is another way of listing the elements of the array of partial sums.
System.out.println("\nHere is the array of partial sums: ");
for (int element: arrayPartialSum){
System.out.println("\n"+element);
}
}
catch (Exception e) {
e.printStackTrace();
}
}