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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:53:12+00:00 2026-05-30T20:53:12+00:00

I am having a problems trying to send a file from a server program

  • 0

I am having a problems trying to send a file from a server program to a client program over a socket. SO fair I have tried splitting it into bytes but I haven’t had any success so far. Also the server must be concurrent so I am unsure where I should put the code to send the file.

Thanks

EDIT:

Here is the code that I have tried so far, at the moment It transfers a copy of the file into where its meant to be but the file size is zero bytes 🙁

In the Protocol class :

try {
    File program = new File("./src/V2AssignmentCS/myProgram.jar");

byte[] mybytearray = new byte[4096];

FileInputStream fis = new FileInputStream(program);

BufferedInputStream bis = new BufferedInputStream(fis);

bis.read(mybytearray, 0, mybytearray.length);

OutputStream os = sock.getOutputStream();

System.out.println("Sending...");

os.write(mybytearray, 0, mybytearray.length);

os.flush();

} catch (IOException e) {

        System.err.println("Input Output Error: " + e);

    }

And on the client side :

long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
// localhost for testing

// receive file
byte [] mybytearray  = new byte [ServerResponse.programSize()];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("./src/V2AssignmentCS/newProgram.jar");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;

// thanks to A. Cádiz for the bug fix
do {
   bytesRead =
      is.read(mybytearray, current, (mybytearray.length-current));
   if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

bos.write(mybytearray, 0 , current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
sock.close();
  • 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-30T20:53:13+00:00Added an answer on May 30, 2026 at 8:53 pm
    bis.read(mybytearray, 0, mybytearray.length);
    

    You are ignoring the result code returned by this method. Check the Javadoc. It isn’t what you clearly expect it to be.

    os.write(mybytearray, 0, mybytearray.length);
    

    Here you are writing exactly 4096 bytes. Was that your intention?

    bytesRead = is.read(mybytearray,0,mybytearray.length);
    current = bytesRead;
    // thanks to A. Cádiz for the bug fix
    do {
       bytesRead =
          is.read(mybytearray, current, (mybytearray.length-current));
       if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);
    

    The canonical way to copy streams in Java, which you should be using at both ends, is as follows:

    int count;
    byte[] buffer = new byte[8192]; // or whatever you like
    while ((count = in.read(buffer)) > 0)
    {
      out.write(buffer, 0, count);
    }
    out.close();
    in.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been having some problems trying to get my PHP running. When I
I'm having some problems trying to perform a query. I have two tables, one
I am trying to send data from an Android app to a PHP file
I have a server and a client program (both running on the same machine).
Hey, I'm currently trying to send an image file to a web server using
Having big problems trying to get my php script to send email. Using this
Im trying to read in image file from a server , with the code
I´m having a problem, in my server, after I send a file with X
I'm having problems trying to send an XMPP message to a 'Room' in our
Im having some problems sending mail. I have a ubuntu server running postfix as

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.