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
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:
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.