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

  • Home
  • SEARCH
  • 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 6087057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:49:36+00:00 2026-05-23T11:49:36+00:00

Server side code public static boolean sendFile() { int start = Integer.parseInt(startAndEnd[0]) – 1;

  • 0

Server side code

public static boolean sendFile() {
        int start = Integer.parseInt(startAndEnd[0]) - 1;
        int end = Integer.parseInt(startAndEnd[1]) - 1;
        int size = (end - start) + 1;

        try {
            bos = new BufferedOutputStream(initSocket.getOutputStream());
            bos.write(byteArr,start,size);
            bos.flush();
            bos.close();
            initSocket.close();
            System.out.println("Send file to : " + initSocket);
        } catch (IOException e) {
            System.out.println(e.getLocalizedMessage());
            disconnected();
            return false;
        }

        return true;
    }

Client Side

public boolean receiveFile() {
        int current = 0;

        try {
            int bytesRead = bis.read(byteArr,0,byteArr.length);
            System.out.println("Receive file from : " + client);
            current = bytesRead;
            do {
                 bytesRead =
                 bis.read(byteArr, current, (byteArr.length-current));
                 if(bytesRead >= 0) current += bytesRead;
            } while(bytesRead != -1);
            bis.close();
            bos.write(byteArr, 0 , current);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            System.out.println(e.getLocalizedMessage());
            disconnected();
            return false;
        }
        return true;

    }

Client side is multithreading,server side not use multithreading. I just paste some code that made problem if you want see all code please tell me.

After I debug the code, I found that if I set max thread to any and then the first thread always stuck in this loop. That bis.read(....) always return 0. Although, server had close stream and it not get out of the loop. I don’t know why … But another threads are work correctly.

do {
     bytesRead =
     bis.read(byteArr, current, (byteArr.length-current));
     if(bytesRead >= 0) current += bytesRead;
} while(bytesRead != -1);
  • 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-23T11:49:37+00:00Added an answer on May 23, 2026 at 11:49 am

    How large is your input file (the one you send?) and how large is “byteArr”?
    Also, by the time your check how many bytes are read, you already called bis.read(..) twice:

        int bytesRead = bis.read(byteArr,0,byteArr.length);
    

    You probably want to read/send files larger than your buffer, so you probably want to do something like this:

            byte [] buffer = new byte[4096];
            int bytesRead;
            int totalLength = 0;
    
            while(-1 != (bytesRead = is.read(buffer))) {
                bos.write(buffer, 0, bytesRead);
                totalLength += bytesRead;
            }
            bos.close();
            is.close();
    

    “is” would be a plain InputStream, Peter is right, you do not need to buffer it.

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

Sidebar

Related Questions

My server side code: [WebMethod(CacheDuration = 0, EnableSession = true)] public static int UserID()
Server side code: byte[] size = new byte[4]; size = BitConverter.GetBytes(fileData.Length); stream.Write(size, 0, 4);
Developing server side code i finally got my eyes X-crossed trying to write -
When writing server-side code you need to explicitly stop execution after sending a Location:
I am trying to figure out the efficiency of my server side code. Using
I want to ask in which phase of Control Execution Lifecycle is the Server-Side-Code
For a particular project I have, no server side code is allowed. How can
I am validating a zip code using Javascript that is generated server-side, and injected
I'm thinking to add some code on the server side in asp.net, to verify
This is my code on the client side: import java.io.*; import java.net.*; public class

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.