Java UDP-message exchange funktioniert über localhost aber nicht Internet?

Client Eine

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

Client 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();

        }
      }

Ich verstehe nicht, warum dies funktioniert über localhost, aber wenn ich meine IP-Adresse, es ist nur die Nachricht wird gesendet und nichts empfangen.

Kann jemand mich in die richtige Richtung?

Danke!

  • sind Sie hinter einer firewall und/oder NAT-durch Zufall?
  • Ich habe dafür gesorgt, dass die ports weitergeleitet wurden, auf meinem router?
  • Bitte beschreiben Sie Ihr Netzwerk-setup, es ist nicht klar, wo Sie laufen diese beiden Prozesse.
InformationsquelleAutor lex | 2012-03-14
Schreibe einen Kommentar