Java UDP 消息交换可以在本地主机上工作,但不能在 Internet 上工作?

标签 java udp ip localhost ports

客户A

    import java.io.*;
    import java.net.*;
    import java.util.*;

    public class ClientA 
    {
    final private static int PORT = 5005; // arbitrarily assigned port to use

public static void main(String args[]) throws 
IOException
{
    DatagramSocket socket = new DatagramSocket(PORT); // create new connection on that port
    while (true) 
    {
        byte buffer[] = new byte[256]; // data buffer
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length); // new packet containing buffer

        socket.receive(packet);  // look for packet

        String clientBMsg = new String(packet.getData()); // get data from received packet
        InetAddress address = packet.getAddress(); // get address of received packet

        System.out.println("ClientB at " + address + " says " + clientBMsg);

        buffer = null;
        String msgString = "I'm ClientA, vegetables are fun";
        buffer = msgString.getBytes(); // put String in buffer


        int port = packet.getPort(); // get port of received packet
        packet = new DatagramPacket(buffer, buffer.length, address, port); // create new packet with this data
        socket.send(packet); // send packet back containing new buffer!
        System.out.println("Message Sent");
        socket.close();
       }
}
    }

客户端B

    import java.io.*;
    import java.net.*;


    public class ClientB 
    {
final private static int PORT = 5005; // arbitrarily assigned port - same as server

public static void main(String args[]) throws 
    IOException {
        // if (args.length == 0) { // requires host
            // System.err.println
                // ("Please specify host");
            // System.exit(-1);
        // }
        // String host = args[0]; // user defined host
        DatagramSocket socket = new DatagramSocket(); // open new socket

        String host = "localhost";//"86.0.164.207";
        byte message[] = new byte[256]; // empty message
        String msgString = "Hello, I'm client B and I like trees";
        message = msgString.getBytes(); // put String in buffer

        InetAddress address = InetAddress.getByName(host); // determines address
        System.out.println("Sending to: " + address); // tells user it's doing something
        DatagramPacket packet = new DatagramPacket(message, message.length, address, PORT); // create packet to send

        socket.send(packet); // send packet
        System.out.println("Message Sent");

        message = new byte[256];
        packet = new DatagramPacket(message, message.length);
        socket.receive(packet); // wait for response
        String clientAreply = new String(packet.getData());
        System.out.println("ClientA at " + host + " says " + clientAreply);
        socket.close();

        }
      }

我不明白为什么这在本地主机上有效,但是当我输入我的 IP 地址时,它只是发送消息而没有收到任何消息。

谁能给我指明正确的方向?

谢谢!

最佳答案

您应该使用DatagramSocketbind 方法将其绑定(bind)到您的互联网接口(interface),否则它只会监听127.0.0.1本地主机。像这样:

DatagramSocket socket = new DatagramSocket(null);
socket.bind(new InetSocketAddress(InetAddress.getByName("your.ip.add.ress"),5005);

如果您在路由器后面,那么您应该监听路由器提供给您的本地 IP 地址,并在路由器设置中使用端口转发到该地址。

关于Java UDP 消息交换可以在本地主机上工作,但不能在 Internet 上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9709956/

相关文章:

c++ - Gstreamer 源代码不起作用

c - 使用 TCP 应用程序发送超过 65536 字节

java正则表达式获取ip

java - JAX-RS 发布多个对象

java - 如何将javabean转换为post参数字符串?

java - 调整减法形状的大小

java - 如何使用MySQL准备语句缓存?

C# UDP 数据包发送和接收

c# - 严肃的高性能服务器的 Tcp 可靠性与 Udp 负担

vue.js - 如何在 Vue.js 中获取用户 IP 地址