英文:
UDP Packet not being recieved on localhost
问题
嘿,大家好,我在Java中遇到了接收UDP数据包的问题。我已经在本地测试过,也在我的大学服务器上测试过。没有抛出任何异常,只是我的客户端没有接收到任何数据包。以下是一些代码片段:
在我的服务器调用的第136行:
```java
ps.PacketUtilSendFileLength();
Packet Service类片段:
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); // 这两行在主函数的开始处
DatagramPacket FileLength_ACK = new DatagramPacket(buffer.array(), buffer.array().length);
// ...
ps.PacketUtilRecieve(FileLength_ACK);
Packet Service类中的方法:
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
<details>
<summary>英文:</summary>
Hey guys im having trouble recieving UDP packets in java. I've currently tested it on locolhost and my uni's servers. No exceptions are being thrown its just my client not recieve a single packet. Heres some code :
at line 136 in my Server calls
```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);
}
My client which calls
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);```
try {
datagramSocket.receive(p);
return p;
} catch (IOException e) {
PacketUtilSendError();
e.printStackTrace();
}
return null;
}
my Server is sending the packet in but my client never recieves it what am i doing wrong here?
full code : https://github.com/WeStandUnited/SlidingWindow/tree/master/src
专注分享java语言的经验与见解,让所有开发者获益!
评论