Note that there are some explanatory texts on larger screens.

plurals
  1. POJava DatagramSocket(UDP Socket) only receives when previously have sent a packet
    primarykey
    data
    text
    <p>I have the following problem:</p> <p>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)</p> <p>Here is what my client looks like:</p> <pre><code> 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())); } } </code></pre> <p>Basically all it does is listen on port clientPort and then prints whatever it receives. However it does not work. </p> <p>Now slightly modifying it solves the problem:</p> <pre><code>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())); } } </code></pre> <p>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.</p> <p>Also, it should be noted that the code works fine when both the client and the server are on the same machine. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload