views:

229

answers:

1

I'm using the DatagramSocket class in Java to receive udp packets from a client written in C. Here is the code that receives (the server socket is already set up):

byte[] inputByte = new byte[1];
        DatagramPacket recvdPacket = new DatagramPacket(inputByte, inputByte.length);



        try {
            serverSocket.receive(recvdPacket);

And then it prints the contents. My debugging messages (not shown in this code) indicate that it's successfully getting to the receive() part and is waiting (it's a blocking call). Here is the problem:

This server only receives packets the second time the client sending the messages is run, never the first. Even if the client sends multiple packets the first run, the server never shows anything until the second time the entire client is run. Is it safe to assume the problem is in the server side, not the client side? (the client side code was not written by me...it's disgusting, and in C).

+2  A: 

I'll try to give the best answer I can, but with only 3 lines of code to look at, you may be doing something bad I can't see. Can you post some more of the relevant code?

Sounds to me like the problem may be in the client. There's also a possibility that your network is doing something funky to UDP traffic (I've run into some of those). If you want to isolate which side has the problem, I'd recommend running wireshark on the server machine through both runs of the client and then see what kind of packets the server machine is receiving. Make sure you give a few seconds between successive runs of the client, then filter for the UDP port in question. If there's no packets reaching the server on that first run of the client, then your problem is probably in the client somewhere.

Seth
+1 but run Wireshark on BOTH the server machine AND the client machine. I've had issues with routers where running a trace on the server implied the client wasnt sending (we got no packets) whereas running the trace on the client as well clearly showed that the client WAS sending but that the packets were getting lost between client and server.
Len Holgate
+1 packet sniffer is an invaluable tool for any network programming, be it in Java, C, Haskel, or Cobol :)
Nikolai N Fetissov