UDP数据包在本地未收到。

huangapple 未分类评论49阅读模式
英文:

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&#39;ve currently tested it on locolhost and my uni&#39;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

huangapple
  • 本文由 发表于 2020年4月8日 05:57:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61090030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定