1)在Java无连接套接字编程中,如何确保来自我的5个处理器的所有数据包都已收到并在它们到达时存储在数组中?基本上,我想收集作为多播组一部分的所有进程(其中5个)发送的所有部分和,并将这些部分和存储在一个数组中,然后我将执行一些操作。
2)另外,我很感激如何在同一代码中找到整数数组的给定元素的索引。我需要执行的操作之一是找到我的数组的Max并检索其索引。
下面是我的线程代码片段,其中包含接收方法。
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();
}
}
}
更新:我从while(true)循环中移出了一些元素,但现在我在编译时遇到了一种新的错误。它显示某些行并说“无法访问的声明”。有什么想法吗?
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();
}
}
答案 0 :(得分:0)
我终于重新访问了我的源代码并理解代码实际上是有用的,即。好。可以进行一些调整以使其更漂亮并改善逻辑。例如,我将一些变量声明从无限循环中移出......
我的问题的答案非常简单:
arrayPartialSum[recvdProcessID] = recvdPartialSum;
实际上做了它应该做的事情。也就是说,将每个接收到的部分和存储在与其进程ID相对应的索引处的部分和数组中。逻辑中的问题实际上并不在代码中,而是作为程序用户的方式启动流程。我需要确保在每个进程中都返回它,以便将其部分总和发送到多播组。要检索给定项目的索引,下面的代码执行作业,maximumSum
是其索引尝试在数组arrayPartialSum
中找到的元素:
for (int i = 0; i < arrayPartialSum.length; i++) {
if (arrayPartialSum[i] == maximumSum) {
retval = i;
break;
}
}