Java DatagramSocket(UDP Socket) only receives when previously have sent a packet

By : Somych
Source: Stackoverflow.com
Question!

I have the following problem:

I have an extremely simple Java Datagram client and server located on a remote machine. And the client will not receive anything from the server unless it sent a packet to the server earlier(it doesn't matter what information did the packet hold)

Here is what my client looks like:

    public static void TheClient() throws Exception { 
        ds = new DatagramSocket(clientPort); 
        while(true) { 
            DatagramPacket p = new DatagramPacket(buffer, buffer.length);   
            ds.receive(p); 
            System.out.println(new String(p.getData(), 0, p.getLength())); 
        } 
    } 

Basically all it does is listen on port clientPort and then prints whatever it receives. However it does not work.

Now slightly modifying it solves the problem:

public static void TheClient() throws Exception {
    ds = new DatagramSocket(clientPort); 

    //Sending an empty packet
    byte tempBuffer1[] = new byte[10];
    InetAddress address = InetAddress.getByName(SERVER_IP);
    DatagramPacket packet1 = new DatagramPacket(
            tempBuffer1, tempBuffer1.length, address, serverPort);
    ds.send(packet1);

    while(true) { 
        DatagramPacket p = new DatagramPacket(buffer, 
                buffer.length);     
        ds.receive(p); 
        System.out.println(new String(p.getData(), 0, 
                p.getLength())); 
    } 
} 

Does anyone know what might be causing this problem? While the workaround did solve the problem it doesn't make any sense to me why it wouldn't work originally so the solution might not actually work on all the computers.

Also, it should be noted that the code works fine when both the client and the server are on the same machine.

By : Somych


Answers

This happens on AWS EC2. The port on the client side is not opened on your firewall (security group). When your client sends a packet to the server, the firewall allows the data from the server for a short of time. The client can receive from the server without sending anything to the server if the client port on the firewall is opened.

By : Xin Gao


Check to see if your PC is using a private IP address, typically one starting with either "10.x.x.x" or "192.168.x.x". If so, the most likely cause is that you're using a NAT-based firewall which shares a single public address and multiple private addresses.

When you send a packet outbound, the firewall knows which internal address should receive the return packet. When a connection is initiated from an external address, however, it has no way of knowing what internal address should receive the traffic. Most SOHO (small office / home office) routers will allow you to "port forward" traffic to a specific internal IP address. If you're using DHCP, you'll want to assign yourself a static IP address or your port forwarding may stop working at some future date because it grabs a different IP address.



This video can help you solving your question :)
By: admin