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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:55:17+00:00 2026-05-26T03:55:17+00:00

I tried to solve the problem in many ways but without success and I

  • 0

I tried to solve the problem in many ways but without success and I have also looked for information in this forum but with same results, so here we go.

I am actually doing a server daemon that accepts client requests and then it (the server) transfers all the files contained in a specific folder. I’m going to post the code of the sendFileData (on the server) and the receiveFileData (on the client).

The server uses:

public static void sendFileData(File file, Socket socket) throws FileNotFoundException, IOException, SocketException {
    byte[] auxiliar = new byte[8192];
    byte[] mybytearray = new byte[(int) file.length()];
    int longitud = mybytearray.length;

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    bis.read(mybytearray, 0, longitud);
    DataOutputStream os = new DataOutputStream(socket.getOutputStream());
    int paquetes = longitud / 8187;
    int resto = longitud % 8187;
    int i = 0;
    while(i<paquetes){//The length goes on the first 4 bytes and the 5th tells if there are more packets to send (8192 bytes or less).
        byte[] bytes = ByteBuffer.allocate(4).putInt(8187).array();
        auxiliar[0] = bytes[0];
        auxiliar[1] = bytes[1];
        auxiliar[2] = bytes[2];
        auxiliar[3] = bytes[3];
        auxiliar[4] = 1;
        for(int j = 5; j < 8192; j++){
            auxiliar[j] = mybytearray[i*8187+(j-5)];
        }
        os.write(auxiliar, 0, 8192);

        i+=1;
    }
    if(resto > 0){
        byte[] bytes = ByteBuffer.allocate(4).putInt(resto).array();
        auxiliar[0] = bytes[0];
        auxiliar[1] = bytes[1];
        auxiliar[2] = bytes[2];
        auxiliar[3] = bytes[3];
        auxiliar[4] = 0;
        for(int j = 5; j < resto+5; j++){
            auxiliar[j] = mybytearray[i*8187+(j-5)];
        }
        os.write(auxiliar, 0, resto+5);
    }
    os.flush();
}

And in the client side:

public static void receiveFileData(String nombreFichero, Socket s) throws IOException{
        File monitored = new File(nombreFichero);
        if(monitored.exists() == false){
            monitored.createNewFile();
        }
        byte[] mybytearray;
        DataInputStream is = new DataInputStream(s.getInputStream());
        FileOutputStream fos = new FileOutputStream(monitored);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int bytesRead = 0;
        int hasNext = 1;
        do {
            bytesRead = is.readInt();//Leo longitud
            try {
                Thread.sleep(1);// HERE!!!!
            } catch (InterruptedException e) {
            }
//          System.out.println("Bytes read "+bytesRead );
            if(bytesRead <= 8187 && bytesRead > 0){
//              System.out.println("Bytes leídos "+bytesRead );
                hasNext = is.readByte();//Leo si hay más datos por enviar
                mybytearray = new byte[bytesRead];
                is.read(mybytearray);
                if(monitored.exists()){
                    synchronized(monitored){
                        bos.write(mybytearray, 0, mybytearray.length);
                    }
                }
                mybytearray = null;
            }else{
                System.out.println("Fuera de rango "+bytesRead);
            }
        }while(hasNext == 1);
        bos.close();
        mybytearray = null;
        System.out.println("Fichero recibido: "+monitored.getAbsolutePath());

    }

In the receiveFileData code, if I do not put a Thread.sleep(1) or a System.out.println() or whatever who takes time to execute, I am not receiving the data in the correct way on the client, because readInt() returns a very high number randomly negative or positive (which implies Heap out of memory and other exceptions).

Sure it’s something about synchronization but I think the transfering schema between the two methods is correct (maybe the client is too slow and server too fast).

What is happening?? Because I do not want to put a Thread.sleep, this is not good programming here I think.

Thank you so much!

  • 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-26T03:55:17+00:00Added an answer on May 26, 2026 at 3:55 am

    is.read(bytes) is not guaranteed to fill the supplied byte array. You need to check its return value to see how many bytes were read or (better) use readFully().

    The sleep() probably just allows time for all bytes to have been returned from the socket.

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

Sidebar

Related Questions

I tried many ways to solve this problem but none works. suppose I have
I tried everything I know to solve this problem, but no success. My site
I tried to solve problem# 276 from Project Euler , but without success so
I saw many questions about this, and tried to solve the problem, but after
I tried many ways, to solve the problem, that mails are throw to spam
All, there are many question on this subject but none solve my problem. I
I've googled many similar situations, but none of them could solve my problem. Please
I tried to solve this problem Problem Description It seems correct idea is to
I tried now many things and I googled for hours but I couldn't solve
First i couldn't start MSDTC service.I tried following link and solve that problem. link

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.