嘿,我在用Java接收UDP数据包时遇到麻烦。我目前已经在locolhost和uni的服务器上对其进行了测试。没有异常被抛出,只是我的客户没有收到一个数据包。这是一些代码:
在我的服务器调用的第136行
ps.PacketUtilSendFileLength();
// Packet Service class snippet
public void PacketUtilSendFileLength() throws IOException {
DatagramPacket packet = null;
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.putLong(file_length);
buffer.flip();
if (V6_Mode) {
packet = new DatagramPacket(hash(buffer.array()), buffer.array().length, Hostv6, PORT);
} else packet = new DatagramPacket(hash(buffer.array()), buffer.array().length, Hostv4, PORT);
datagramSocket.send(packet);
}
我的客户
ByteBuffer buffer = ByteBuffer.allocate(8);// these 2 lines are made at the start of the main
DatagramPacket FileLength_ACK = new DatagramPacket(buffer.array(),buffer.array().length);
...
ps.PacketUtilRecieve(FileLength_ACK);```
``` public DatagramPacket PacketUtilRecieve(DatagramPacket p){
try {
datagramSocket.receive(p);
return p;
} catch (IOException e) {
PacketUtilSendError();
e.printStackTrace();
}
return null;
}
我的服务器正在发送数据包,但是我的客户端从未收到它,这是我在做什么错了?
完整代码:https://github.com/WeStandUnited/SlidingWindow/tree/master/src