UDP DatagramSocket.receive() glitches


Last Updated:

  1. Sir^Knigget

    Sir^Knigget Member This Topic's Starter

    Joined:
    Sep 30, 2009
    Messages:
    11
    Likes Received:
    0
    Hello,
    I have a thread that constantly polls a UDP connection and posts the received data to another thread. It uses a connected DatagramSocket, reads a packet (which is always the same size), gets the data and posts it to a message queue (not the android one, my implementation).
    The problem happens mainly over 3G connection, not wifi.

    Code looks like:

    protected void work() {
    byte[] buffer = new byte[SOME_BUFFER_SIZE];

    // Read UDP packet
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    long startTime = System.currentTimeMillis();
    socket.receive(packet);
    long elapsed = System.currentTimeMillis() - startTime;
    Log.out.d("UDPReader: time for socket read: " + elapsed);

    // Post the data
    messageQueue.post(packet.getData());
    }

    My server side sends the UDP packets at a constant rate (About 80 millis) and I expect that rate more or less for streaming purposes.
    Problem is: in the beginning all is fine (log print shows a good interval - around 80ms), then there a huge gap (about half a second), then some packets arrive with no interval at all (like they were buffered somewhere), then everything gets normal for the rest of the operation.
    So the effect for me is having a glitch in the beginning of the stream, and the everything is back to normal infinitely.

    I verified that my reader thread isn't blocking anywhere else, and that the socket read elapsed time really covers just the read itself, so I get reliable log prints (as seen in the code). I also used wireshark in the server side to verify constant packet rate.
    So the 2 left options are:
    1. Network issue - but that would happen for the whole duration, not just in the beginning, right? It always glitches like mad in the start, then everything is normal.
    2. Platform issue - is there anything about DatagramSocket implementation on Android that might cause this? It seems like there's an initial capacity for buffering and then it's increased after that first glitch and it doesn't happen anymore...

    I also checked: 1. CPU usage during the glitch and after (using traceview), nothing unusual. 2. DatagramSocket receive buffer size - it seems to default to 100000+ bytes which is far more than my packet size.

    I would appreciate any help =)
     

    Advertisement

Share This Page

Loading...