Question

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();
    }
}
Was it helpful?

Solution

I finally revisited my source code and understood that the code is actually functional,i.e. good. A few adjustments could be made to make it a little prettier and improve the logic. For example, I moved some variable declarations out of the infinite while loop...

The answers to my questions are pretty simple:

  1. The line arrayPartialSum[recvdProcessID] = recvdPartialSum; actually does what it is supposed to do. That is, to store each received partial sum in the array of partial sums at the index corresponding to its process ID. The problem in the logic was actually not in the code, but rather in the way I, as a user of the program, was launching the processes. I need to make sure I hit return in each process for it to send its partial sum to the multicast group.
  2. To retrieve the index of a given item, the below code does the job, maximumSum being the element whose index one is trying to find in the array arrayPartialSum:

    for (int i = 0; i < arrayPartialSum.length; i++) {
        if (arrayPartialSum[i] == maximumSum) {
            retval = i;
            break;
        }
    }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top