0

I have the problem i using UDP connection to send 3 messages to the computer2, it can only get the last messages only in the computer 2

String service = "deposit";     //send service
byteSend = service.getBytes();
sendPacket.setData(byteSend);
sendPacket.setAddress(destAdd);
sendPacket.setPort(destPort);
otherBranch.send(sendPacket);

byteSend1 = accNo.getBytes();    //send accNo
sendPacket.setData(byteSend1);
sendPacket.setAddress(destAdd);
sendPacket.setPort(destPort);
otherBranch.send(sendPacket);

byteSend2 = depositAmount.getBytes();  //send depositAmount
sendPacket.setData(byteSend2);
sendPacket.setAddress(destAdd);
sendPacket.setPort(destPort);
otherBranch.send(sendPacket);

after that the computer2 have this code to receive:

myServer.receive(packetReceive);
clientMessage = new String(packetReceive.getData(),0,packetReceive.getLength());
System.out.println("Service: "+clientMessage);

myServer.receive(packetReceive);
accNo = new String(packetReceive.getData(),0,packetReceive.getLength());
System.out.println("accNo: "+accNo);

myServer.receive(packetReceive);
depositAmount = new String(packetReceive.getData(),0,packetReceive.getLength());
System.out.println("depositAmount: "+depositAmount);

How come the output only can get my last value which is depositAmount only?

4

1 回答 1

1

您的代码没有传输节奏。使用 TCP,堆栈会为您传输起搏。使用 UDP,这是您的责任。

您需要编写代码来检测丢弃的数据报并重新传输它们。UDP 不保证消息传递。这就是应用程序的工作。

如果您需要 TCP 所做的一切,但您选择使用 UDP,您必须自己实现所有这些。

这包括:

  1. 传输起搏。

  2. 指数退避。

  3. 丢弃数据报检测和重传。

  4. 重复数据报检测。

  5. 乱序接收处理。

  6. 损坏数据报检测。

如果您不想做所有这些事情,请使用 TCP。然后堆栈为您完成所有工作。

于 2012-12-10T14:46:39.070 回答