Java UDP消息交换在本地工作,但在Internet上不起作用?

3

客户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地址时,它只发送消息,没有接收到任何东西。
有人能指点我正确的方向吗?
谢谢!

1
你是否碰巧在防火墙和/或 NAT 后面? - Uku Loskit
我确保我的路由器已经转发了端口? - lex
请描述您的网络设置,不清楚您在哪里运行这两个进程。 - devmiles.com
2个回答

4

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

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

如果你在路由器后面,那么你应该监听路由器分配给你的本地IP地址,并在路由器设置中将端口转发到此地址。


抱歉,这是为客户A还是B? - lex
两个客户端必须拥有各自的IP地址,否则它们将无法在互联网上相互通信。当它们在同一台机器上运行时,本地主机和127.0.0.1才适用。 - Stainedart
当我运行时,出现以下错误: Exception in thread "main" java.net.BindException: Cannot assign requested address - lex
请描述您的网络设置。您提到了一些路由器用于转发端口。我会编辑我的答案。 - devmiles.com
在Win7上运行的Ubuntu虚拟机通过无线连接到路由器,并已在路由器上转发了我使用的端口,同时使用我的公共IP地址。 - lex
显示剩余3条评论

0

我建议使用Socket Test工具来测试TCP和UDP套接字:

我用它来解决双向套接字程序的问题。您可以使用两个SocketTest程序进行端到端测试,然后比较结果等。非常有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接