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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:57:49+00:00 2026-05-28T03:57:49+00:00

i have a FileServer and a FileClient, server sends a file, when client connects.

  • 0

i have a FileServer and a FileClient, server sends a file, when client connects. it is a simple program, just to understand the concept behind.

I am able to send the file from Server to Client, with a buffer of 1024.
The problem is that the recieved file is always around 0.01 MB less than the original file.Thus mp3 files lose some info, and video files just doesn’t play.

I put some prinln statements in my while loops in both Server & Client. i found that my server is not sending the whole file.

  //Server
  byte [] mybytearray  = new byte [1024];    
  FileInputStream fis = new FileInputStream(myFile);
  BufferedInputStream bis = new BufferedInputStream(fis);
  bis.read(mybytearray,0,mybytearray.length);
  OutputStream os = sock.getOutputStream();
  System.out.println("Sending...\n mybytearray length:"+mybytearray.length+"file   length:"+(int)myFile.length());
  int read, readTotal=0;
  while ((read = fis.read(mybytearray,0,mybytearray.length)) != -1) { 

      os.write(mybytearray, 0, read);
      System.out.println("File REad:"+read+"readtotal:"+readTotal); //*
      readTotal += read;
}
  System.out.println("Final File Read:"+read+" Final readtotal:"+readTotal);
  os.flush();
  sock.close();
  } 

The Println statement output is:

Sending...
mybytearray length:1024file length:12767554
File REad:1024readtotal:0
File REad:1024readtotal:1024
.............and a lot of it...and then
File REad:1024readtotal:12756992
File REad:1024readtotal:12758016
File REad:322readtotal:12759040
Final File Read:-1 Final readtotal:12759362

The file length:12767554 & Last readTotal: 12759362 shud be equal. i don’t understand why the last read value is lower [322], while it can still have 1024.

Any kind of help wud be appreciated.
Thanks.

[EDIT]

//Client
int read;
int totalRead = 0;

while ((read = is.read(mybytearray,0,mybytearray.length)) != -1) {
        bos.write(mybytearray, 0 , read);

        totalRead += read;
        System.out.println("\nread:"+read+"\ntotalread: "+totalRead);
}
System.out.println("Final File Read:"+read+" Final readtotal:"+totalRead);
bos.write(mybytearray, 0 , read);  //57 Line in FileClient.java
bos.flush();

i again tried to send a file. a txt this time.
this is my server’s output

Sending...
mybytearray length:1024file length:1232
File REad:1024readtotal:0
File REad:208readtotal:1024
Final File Read:-1 Final readtotal:1232

and this one on client

read:208
totalread: 1232
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
   Final File Read:-1     Final readtotal:1232

at java.lang.System.arraycopy(Native Method)
at java.io.BufferedOutputStream.write(Unknown Source)
at FileClient.main(FileClient.java:57)

readtotal values are same, but sometimes i get this error, sometimes i don’t.

[BIG EDIT–COMPLETE CLIENT CODE]

public class FileClient{
 public static void main (String [] args ) throws IOException {

long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
final JFrame f = new JFrame("Sample");
f.getContentPane().setLayout(new FlowLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(590,490);
f.setVisible(true);
// localhost for testing
Socket sock = new Socket("localhost",13267);
System.out.println("Connecting...");
File f1=new File(RecieveObject(sock));


// receive file
byte [] mybytearray  = new byte [1024];
InputStream is = sock.getInputStream();


FileOutputStream fos = new FileOutputStream(f1);
ProgressMonitorInputStream nn= new ProgressMonitorInputStream(f,"reading",is);
BufferedOutputStream bos = new BufferedOutputStream(fos);
 /*   bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;


do {
   bytesRead =
      is.read(mybytearray, current, (mybytearray.length-current));
   System.out.println("mybytesarray length: "+mybytearray.length+"\ncurrent:"+current+"\nbytesread: "+bytesRead);
   if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

bos.write(mybytearray, 0 , current);
bos.flush();
 */
int read;
int totalRead = 0;
//InputStream clientInputStream = clientSocket.getInputStream();
while ((read = is.read(mybytearray,0,mybytearray.length)) != -1) {
        bos.write(mybytearray, 0 , read);

        totalRead += read;
        System.out.println("\nread:"+read+"\ntotalread: "+totalRead);
}
System.out.println("Final File Read:"+read+" Final readtotal:"+totalRead);
 //   bos.write(mybytearray, 0 , read);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
sock.close();

}
public static  String RecieveObject(Socket s) {
    String str = null;
    try{
    ObjectInputStream is = new ObjectInputStream(s.getInputStream());



            str =(String)is.readUTF();


    }
    catch(IOException ex){}
    return str;
}   

}

  • 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-28T03:57:50+00:00Added an answer on May 28, 2026 at 3:57 am

    You’re missing the beginning of the file (up to the first 1024 bytes), due to your 5th line (including the comment). You read from the input and advanced it, without sending it to the client. Remove this:

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

    Additionally, you’re not using the BufferedInputStream in your loop. Either use bis.read here instead of fis.read (if you still want buffered reads) – or remove the BufferedInputStream all together.

    Your other problem is that you’re reading the final bytes on the client, then entering the loop once more. is.read is again called. Instead of returning -1, it’s throwing an IOException as the other side of the socket has been closed. As such, bos.flush() and bos.close() aren’t getting called, and your final bytes are never written to disk. To assist with this, try calling sock.shutdownOutput before closing it. Regardless, you’ll want to add some proper exception handling around this.

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

Sidebar

Related Questions

I have to develop a file server service that transfer file form a specified
I have been working on designing a file server that could take the load
I'm using PuTTY to access my BSD file server and I have several terminal
I have this file sharing program, where i can get mye files from a
I have StartServer.php file that basically starts a server if it is not already
I have a simple script which copies a file from one SMB mount to
I have a series of files on a private ftp file server that I
I have a got a file server and a web server. They are running
I have a file server backup on an external hard drive a few months
i am a bit confused about threads. If i have a file server and

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.