sam
sam

Reputation: 2579

can't send large image files over udp

I am trying to send an image over UDP using JAVA. I have managed to implement the sender and receiver and it works for a small image (18KB in this case). When I try to send a larger image (2MB) the receiver seems to jam after sending a few hundred datagrams.

Here is the loop in my receiver which recieves each packet from the sender.

while(true) {
            packetCount++;
            System.out.println("PKT: " + packetCount);
            receievePacket = new DatagramPacket(recieveData, recieveData.length);

            try {
                receieveSocket.receive(receievePacket);
            } catch (IOException e) {
                System.out.println("Error receieving packet");
                e.printStackTrace();
            }

            bos.write(recieveData, 0, (int) DATAGRAM_MAX_SIZE);
            // Terminate loop if last packet received
            if (packetCount == packetNum) {
                break;
            }

        }

The code before this loop just receives the port number from the user, sets up the socket and recieves a single packet from the sender which specifies how many packets will be sent. This is stored in packetNum.

Can anyone think of a reason why it stalls when trying to send larger images? Thanks

Upvotes: 3

Views: 3844

Answers (2)

Erik Ekman
Erik Ekman

Reputation: 2066

UDP can only handle 64kB in each packet. You need to fragment the data into blocks and give them numbers, since they are not guaranteed to arrive in order.

But really, you should switch to TCP unless you have some special reason. It helps you with the problems you have, and some you have not yet seen :)

Upvotes: 3

Kiril
Kiril

Reputation: 40395

You mean "receiver seems to jam after receiving a few hundred datagrams," not sending, right? If that's the case, then you may possibly be the victim of the infamous UDP packet loss! :) There are two options:

  1. Slow down the transmission rate.
  2. Implement reliable UDP.

The first one is easier to do and it should allow you to quickly determine if you're experiencing packet loss. Run a few tests and see if you're receiving the same number of bytes by varying the transmission rates (i.e. put a small sleep between sending out each packet). If you detect a difference in packet loss, then implement reliable UDP and request the re-transmission of out-of-sequence or missing packets.

Upvotes: 1

Related Questions