客户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();
       }
}
    }

客户乙
    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();

        }
      }

我不明白为什么这会在 localhost 上工作,但是当我输入 IP 地址时,它只是发送消息而没有收到任何消息。

任何人都可以指出我正确的方向吗?

谢谢!

最佳答案

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

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

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

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

10-11 19:17