title: Java UDP message exchange works over localhost but not Internet?
body: <p>Client A</p>
  
  <pre><code>    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();
         }
  }
      }
  </code></pre>
  
  <p>Client B</p>
  
  <pre><code>    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();
  
          }
        }
  </code></pre>
  
  <p>I don't understand why this works over localhost but when I put my IP address in, it just sends the message and nothing is received.</p>
  
  <p>Can anyone point me in the right direction here? </p>
  
  <p>Thank you!</p>