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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:21:01+00:00 2026-05-27T03:21:01+00:00

I am working on transferring a file between two computers over a socket. Everything

  • 0

I am working on transferring a file between two computers over a socket. Everything seems to work, but when I look at the contents of the retrieved file, it is empty. What am I doing wrong?

Here is my server-side code. The file foobar.txt exists, and its contents are “hello world!”.

try{ 
    ServerSocket ssock = new ServerSocket(12345);
    Socket sock = ssock.accept();
    //here I get the filename from the client, but that works fine.
    File myFile = new File("foobar.txt");
    byte[] mybytearray = new byte[(int) myFile.length()];
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
    bis.read(mybytearray, 0, mybytearray.length);
    OutputStream os = sock.getOutputStream();
    os.write(mybytearray, 0, mybytearray.length);
    os.flush();
    sock.close();
} catch (Exception e){
    e.printStackTrace();
}

And here is my client code:

try {
    Socket socket = new Socket(host, port);
    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    out.print("get foobar.txt\r\n");
    out.flush();
    byte[] streamIn = new byte[1024];
    InputStream in = socket.getInputStream();
    FileOutputStream file_src = new FileOutputStream("foobar.txt"); 
    BufferedOutputStream file_writer = new BufferedOutputStream(file_src);
    int i;
    while ((i = in.read()) != -1) {
        file_writer.write(i);
    }
    file_writer.flush();
    file_writer.close();
    file_src.close();
    socket.close();
} catch (Exception e) {
   e.printStackTrace();
}

Solved

Since I am using multiple threads and multiple sockets and testing all connections on one machine, I was simply running into a problem where the client (which has both the client and server code in it) would connect with itself instead of the other client. Changing the file transfer port for the different running clients got this all to work. Thanks for everyone who had a look at this and gave me some suggestions.

  • 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-27T03:21:01+00:00Added an answer on May 27, 2026 at 3:21 am

    In addition to what everyone else has said, you are ignoring the result of bis.read(). It isn’t guaranteed to fill the buffer. See the Javadoc.

    The correct way to copy streams in Java, which you should use at both ends, is this:

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

Sidebar

Related Questions

I'm working with a GPS module that is transferring data to my mac over
I am transferring different xml files via ftp and it's working fine. But one
Working on a somewhat complex page for configuring customers at work. The setup is
I am working in LaTeX, and when I create a pdf file (using LaTeX
I tried transferring a working login system from one domain to another. It works
I'm working on client/server application which uses AsyncSocket. For transferring data, it uses NSData
I'm working on a very basic scheduled FTP program, but I don't want to
I'm working on a GPGPU application that transfers data between the cpu and gpu
Working with dates in ruby and rails on windows, I'm having problems with pre-epoch
Working with a SqlCommand in C# I've created a query that contains a IN

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.