未从外部客户端接收UDP数据包

未从外部客户端接收UDP数据包,udp,datagram,Udp,Datagram,我有一个UDP/TCP服务器和客户端的游戏。一个UDP端口(2406)用于更新客户端位置,一个TCP端口(2407)用于聊天。这里的问题是2406 当我在本地网络中播放客户端时,一切都正常运行。但是当外部客户机想要加入时,我只收到第一个包(join命令),然后。。。没有什么。我(登录本地网络)无法看到外部播放器。但是他们可以看到我。聊天对双方都有效。所以它确实与DatagramSocket有关。我将尽可能多地发布与UDP相关的信息,而不是TCP 有人知道这里有什么问题吗 端口被转发,如UDP 2

我有一个UDP/TCP服务器和客户端的游戏。一个UDP端口(2406)用于更新客户端位置,一个TCP端口(2407)用于聊天。这里的问题是2406

当我在本地网络中播放客户端时,一切都正常运行。但是当外部客户机想要加入时,我只收到第一个包(join命令),然后。。。没有什么。我(登录本地网络)无法看到外部播放器。但是他们可以看到我。聊天对双方都有效。所以它确实与DatagramSocket有关。我将尽可能多地发布与UDP相关的信息,而不是TCP

有人知道这里有什么问题吗

端口被转发,如UDP 2406、TCP 2407

服务器、套接字:

DatagramSocket socket = new DatagramSocket(2406, InetAddress.getLocalHost());
ServerSocket serversocket_chat = new ServerSocket(2407, 0, InetAddress.getLocalHost());
服务器,接收线程:

byte[] buffer = new byte[1024];
DatagramPacket dp = new DatagramPacket(buffer, 1024);

while(true){
    try{
        this.socket.receive(dp);

        String data = new String(dp.getData(), 0, dp.getLength()).trim();
        String[] args = data.split(":");
        String command = args[0];

        String reply = null;
        try{
            reply = handleCommand(dp, command, args);
        } catch( Exception e ){
            System.err.println("Error while handling command: " + command);
            e.printStackTrace();
        }

        if(reply != null){
            reply += "\n";
            DatagramPacket reply_packet = new DatagramPacket(reply.getBytes(), reply.length(), dp.getSocketAddress());

            this.socket.send(reply_packet);
        }

    } catch (IOException e){
        e.printStackTrace();
    }
}

new Thread(chat_receive).start();
一旦有人发送消息,handleCommand方法就会发现它是什么。每条消息都是从字符串派生的字节[]。如果消息为“cj:Hello”,handleCommand将查找命令cs、用户名Hello。这是由服务器接收的。在那之后,如果同一个人发送了什么,什么也不会收到

客户端套接字:

private DatagramSocket socket;
private Socket socket_chat;
客户端连接:

this.socket = new DatagramSocket();
this.socket_chat = new Socket(ip, port+1);
客户端发送:

private Runnable send = new Runnable() {
    @Override
    public void run() {
        DatagramPacket dp;
        String sendStringBuffered;
        while(true){
            if(sendString != null){
                sendStringBuffered = sendString;
                dp = new DatagramPacket(sendStringBuffered.getBytes(), sendStringBuffered.length(), ip, port);
                try {
                    socket.send(dp);
                } catch (IOException ex) {
                    Logger.getLogger(NewClient.class.getName()).log(Level.SEVERE, null, ex);
                }
                 sendString = null;
            }
        }
    }
};

我想到两件事:

  • UDP不可靠。数据报可能随时丢失
  • UDP数据包通常不会像那样遍历NAT

为了进行系统故障排除,请使用数据包嗅探器/分析器(如tcpdump或wireshark)确保数据包真正到达目的地。

哦,好的。有趣的是,除了第一个数据包,没有任何数据包从外部客户端传输到我的服务器。你认为我应该使用TCP吗?我不熟悉包嗅探器/分析器,所以使用它们将是一个完整的学习过程。但如果你这么说,我会尽力找出答案。Wireshark非常直截了当(特别是在这个方面)。你应该没问题。好的,我试试,谢谢。我希望我能找出为什么我的服务器可以向发送,但不能从外部客户端接收。