Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7560905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:01:32+00:00 2026-05-30T13:01:32+00:00

I am trying to implement a udp image transfer program in java. The sender

  • 0

I am trying to implement a udp image transfer program in java. The sender breaks the image up into 1024 byte packets where the first 2 bytes are a short carrying the sequence number and the 3rd byte is a flag indicating whether the packet is the last one or not.

When I execute the receiver and send the image using the sender the transferred image is incomplete and has strange colours on it.
Other times when I execute the receiver, it will stick after sending several hundred packets.

Here is the code for the receiver which takes the received packets and writes it to a byte stream.

        double DATAGRAM_MAX_SIZE = 1024;
        byte[] recieveData = new byte[(int) DATAGRAM_MAX_SIZE];
        ByteArrayInputStream bais = new ByteArrayInputStream(recieveData);
        DataInputStream dis = new DataInputStream(bais);

        // Loop until all packets received
        byte isLast = 0;
        while(true) {
            receievePacket = new DatagramPacket(recieveData, recieveData.length);
            try {
                receieveSocket.receive(receievePacket);
            } catch (IOException e) {
                System.out.println("Error receieving packet");
                e.printStackTrace();
            }
            Short sequenceNum = dis.readShort();
            isLast = dis.readByte();
            System.out.println(recieveData[0] + " " + recieveData[1] +" " +  recieveData[2]);
            if (!seenSeq.contains(sequenceNum)) {
                seenSeq.add(sequenceNum);
                bos.write(recieveData, 3, (int) DATAGRAM_MAX_SIZE-3);
            }
            dis.reset();
            // Terminate loop if last packet received
            if (isLast == 1) {
                break;
            }
        }

        //  Byte array to hold image data from stream 
        byte[] imageData = bos.toByteArray();

Whenever the receiver does manage to receieve all packets here is the resulting image:
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:01:34+00:00Added an answer on May 30, 2026 at 1:01 pm
    1. If you are connecting to yourself make sure to make use of:

      Socket.setReceiveBufferSize
      

      The problem I encountered is that sending to yourself is so fast that at some point buffer overflows and some packets are simply discarded. (even assuming the ideal network conditions when connecting to yourself)

    2. Do testing. What packets you are actually sending. Try to create from the Datagram Packets on sender side itself your image, so you are sending what you actually expect. On receiver side check exactly which packets are lost and try to observe the pattern.

    3. As stated before UDP is unrealiable – packets may not arrive and may also arrive in wrong sequence. Check that you place packets out of order in a correct sequence.

    4. Also note that upon setting up the connection several packets can be lost if sender say started milliseconds before the receiver, so receiver should always be up and ready before sender starts doing anything. (this happens even on localhost if threads are not strictly synchronized)

    Sending image over UDP is not really great idea. Image, text are type of data which should really be sent over TCP – partial loss here can have significant impact. VOIP is the one caring less for the packet loss especially with interleaving and compression.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send an image over UDP using JAVA. I have managed
I'm trying to implement a basic file transfer protocol, using UDP. I'm using Beej's
I'm trying to implement a JAVA UDP-based Chat server. I need to implement a
I trying to implement the active record pattern using Java/JDBC and MySQL along with
Ok well I'm trying implement something similar to the 'undo' function in many image
I am trying implement Unblock me Puzzle. i want to change image position from
I am trying to implement my own remote desktop solution in java. Using sockets
I'm trying implement a bracket in my program (using C#/.NET MVC) and I am
Trying to implement the new FP 10.1 Global error handler into my projects but
Just trying to implement mobFox into my app, but having trouble to make it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.