Java 如何确保所有数据包都已接收并存储在阵列中?
1在Java无连接套接字编程中,如何确保来自我的5个处理器的所有数据包都已接收并在到达时存储在一个数组中?基本上,我希望收集所有进程发送的所有部分和,其中5个进程是多播组的一部分,并将这些部分和存储在一个数组中,然后在该数组上执行一些操作 2同样,我也希望能在这段代码中找到整数数组中给定元素的索引。我需要执行的操作之一是查找数组的最大值并检索其索引 下面是我的线程代码片段,其中包含接收方法Java 如何确保所有数据包都已接收并存储在阵列中?,java,arrays,sockets,multicast,Java,Arrays,Sockets,Multicast,1在Java无连接套接字编程中,如何确保来自我的5个处理器的所有数据包都已接收并在到达时存储在一个数组中?基本上,我希望收集所有进程发送的所有部分和,其中5个进程是多播组的一部分,并将这些部分和存储在一个数组中,然后在该数组上执行一些操作 2同样,我也希望能在这段代码中找到整数数组中给定元素的索引。我需要执行的操作之一是查找数组的最大值并检索其索引 下面是我的线程代码片段,其中包含接收方法 readThread(InetAddress g, int port){ group = g;
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();
}
}
}
更新:我从whiletrue循环中移出了一些元素,但现在我在编译时遇到了一种新类型的错误。它显示某些行,并表示无法访问的语句。有什么想法吗
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对应的索引处的部分和数组中。逻辑中的问题实际上不在代码中,而是在我作为程序用户启动进程的方式中。我需要确保在每个进程中点击return,以便它将其部分和发送到多播组。 要检索给定项的索引,下面的代码执行此任务,maximumSum是试图在数组arrayPartialSum中查找其索引的元素:
一般来说您可以通过维护收到的数量的计数并将其与总计数进行比较来实现这一点,可能会使用bool[]来增加该计数,指示是否已收到特定的响应,以避免重复计数重复响应(如果这是一种风险),或者在数据数组中使用一个特殊的不可能的初始值来完成同样的事情。但你的逻辑有几个主要的缺陷,你需要首先解决;最值得注意的是,每次收到一个结果时,您都会创建一个全新的数组。我不理解您评论的第一部分,但我喜欢您在第二部分给出的优点。我只是不确定应该将代码的哪一部分放到whiletrue循环之外@JasonOnly每次收到数据包时应该发生的事情应该进入循环。在您的情况下,您只需要创建一个数组,它应该在循环之外。另外,您只需要处理和显示一次结果,这也应该是循环之外的。您需要为每个响应接收和解析一个数据包——这应该进入循环。至于我的评论的第一部分;简单的实现就是简单地计算您收到的有效响应数量。一旦你收到5张,你就可以出发了。剩下的是处理重复的可能性,不确定是否适用。@JasonC谢谢,我将做一些更改,看看现在是什么样子。@JasonC在我移动从int[]arrayPartialSum=new int[multicastSenderReceiver.numProcesses]开始的整个块之后;在whiletrue循环之外,我现在遇到了一种以前从未见过的新类型的编译错误。它说某些陈述是不可触及的。你知道这对我来说意味着什么吗?我将在问题区域添加代码更新,以显示我所做的移动。
for (int i = 0; i < arrayPartialSum.length; i++) {
if (arrayPartialSum[i] == maximumSum) {
retval = i;
break;
}
}