1) 在 Java 无连接套接字编程中,如何确保来自 5 个处理器的所有数据包均已收到并在到达时存储在数组中?基本上,我想收集属于多播组的所有进程(其中 5 个)发送的所有部分和,并将这些部分和存储在一个数组中,然后我将在该数组上执行一些操作。
2)另外,我希望了解如何在同一代码中查找整数数组的给定元素的索引。我需要执行的操作之一是找到数组的最大值并检索其索引。
下面是我的线程代码片段,其中包含接收方法。
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();
}
}
最佳答案
我终于重新审视了我的源代码并理解该代码实际上是功能性的,即好的。可以进行一些调整以使其更漂亮并改进逻辑。例如,我将一些变量声明移出无限 while 循环...
我的问题的答案非常简单:
- 行
arrayPartialSum[recvdProcessID] = recvdPartialSum;
实际上做了它应该做的事情。也就是说,将每个接收到的部分和存储在部分和数组中与其进程 ID 对应的索引处。逻辑中的问题实际上不在于代码,而在于我作为程序的用户启动进程的方式。我需要确保在每个进程中按回车键,以便将其部分和发送到多播组。 要检索给定项目的索引,下面的代码可以完成这项工作,
maximumSum
是试图在数组arrayPartialSum
中查找其索引的元素>:for (int i = 0; i < arrayPartialSum.length; i++) { if (arrayPartialSum[i] == maximumSum) { retval = i; break; } }
关于java - 如何确保所有数据包均已收到并存储在数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22444643/