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 7984685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:21:06+00:00 2026-06-04T11:21:06+00:00

I trying to create a TFTP server but when it receives a file it

  • 0

I trying to create a TFTP server but when it receives a file it seems that not all of it is saved on to the server (some bytes are missing). The file is created fine and the majority of data is written but as the file is not complete it is classed as corrupt and unopenable. Does anyone know how to fix this issue?

main class

            WRQ WRQ = new WRQ();
            ACK ACK = new ACK();
            DatagramPacket outPacket;
            BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(filename));
            byte[] bytes;
            byte[] fileOut;
            outPacket = WRQ.firstPacket(packet);
            socket.send(outPacket);

            socket.receive(packet);

            while (packet.getLength() == 516){

            bytes = WRQ.doWRQ(packet);
            bufferedOutput.write(bytes);

            outPacket = ACK.doACK(packet);
            socket.send(outPacket);

            socket.receive(packet); 

            }

            bytes = WRQ.doWRQ(packet);
            bufferedOutput.write(bytes);

            bufferedOutput.close();

            outPacket = ACK.doACK(packet);
            socket.send(outPacket);

WRQ class

public class WRQ {

public DatagramPacket firstPacket(DatagramPacket packet) throws IOException{

    ACK ACK = new ACK();
    DatagramPacket ACKpacket = ACK.doACK(packet);

    //takes ACK packet and sets block # as 0 to signal that this is the first packet in a WRQ
    byte[] ACKcontents = new byte[3];
    ACKcontents = ACKpacket.getData();
    ACKcontents[2] = 0;
    ACKcontents[3] = 0;
    ACKpacket.setData(ACKcontents);

    return ACKpacket;

}

public byte[] doWRQ(DatagramPacket packet){

    int length = packet.getLength();
    byte[] packetData = packet.getData();
    byte[] data = new byte[length - 4];
    data = Arrays.copyOfRange(packetData, 4, length - 4);

    return data;

}

}
  • 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-06-04T11:21:08+00:00Added an answer on June 4, 2026 at 11:21 am

    This code looks very suspicious to me:

    byte[] packetData = packet.getData();
    byte[] data = new byte[length - 4];
    data = Arrays.copyOfRange(packetData, 4, length - 4);
    

    Your output array (data) is of length length - 4, but you only copy length - 8 bytes to it. If the bytes to ignore in packetData are the first 4 bytes, it should be

    data = Arrays.copyOfRange(packetData, 4, length);
    

    because the last argument is not a length, but the to index (exclusive). See the javadoc for details.

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

Sidebar

Related Questions

I'm trying create a new file with a Java Applet, but I don't know
I am trying create alert dailog using glade,but its not working .am i doing
I'm trying create a simple audio stream server like a concept proof, but I'm
Trying to create an app that does some socket communication (Writing only). I can
I'm trying create a method that returns the sum of all values in an
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
Trying to create COM -object on server. code is: var myGuid = new Guid(530A1815-820C-11D3-BBB7-008048DE406A);
I'm trying create a RCP Application with Eclipse, but I can't get past the
I'm trying create a box in my Django app that displays text (and possibly
I'm trying create a ASMX webservice that can perform a HTTP GET request. I

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.