Java UDP消息交换是通过localhost而不是Internet工作的?

时间:2012-03-14 20:40:56

标签: 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();

        }
      }

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

有人能指出我在正确的方向吗?

谢谢!

2 个答案:

答案 0 :(得分:4)

您应该使用DatagramSocket的{​​{1}}方法将其绑定到您的互联网界面,否则它只会监听bind127.0.0.1。像这样:

localhost

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

答案 1 :(得分:0)

我建议使用带有TCP和UDP套接字的Socket Test工具:

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