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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:38:27+00:00 2026-05-25T14:38:27+00:00

I have a homework assignment to create a simple data transfer mechanism with a

  • 0

I have a homework assignment to create a simple data transfer mechanism with a client/server TCP socket pair by redirecting standard I/O. I actually have it working, but when I try to transfer large files (say ~5g) the speed slows down dramatically. I am using BufferedInputStream and BufferedOutputStream, and I think that perhaps there is some optimization I can make there. The code for my server is:

private static final int BUF_SIZE = 2047;

public static void main(String[] args) throws IOException{
   /*
    * Attempt to parse command line arguments.
    * @require args[0] is an int
    */
   int port = 0;
   try {
       port = Integer.parseInt(args[0]);
   } catch(NumberFormatException e) {
       System.err.println("Port must be an integer in range 0 - 65535.");
       System.exit(-1);
   }

   /*
    * Bind server socket to specified port number and wait for request.
    * @require port >= 0 && port <= 65535
    */
   ServerSocket welcomeSocket = null;
   welcomeSocket = new ServerSocket(port);
   System.out.println("Now listening on port: " + port);

    /*
     * Accept connection from client socket.
     */
    Socket connectionSocket = null;
    connectionSocket = welcomeSocket.accept();
    System.out.println("Client made connection");

    BufferedInputStream input;
    BufferedOutputStream output;
    if(System.in.available() > 0) {
        input = new BufferedInputStream(System.in, BUF_SIZE);
        output = new BufferedOutputStream(
                connectionSocket.getOutputStream(), BUF_SIZE);
    } else {
        input = new BufferedInputStream(
                connectionSocket.getInputStream(), BUF_SIZE);
        output = new BufferedOutputStream(System.out, BUF_SIZE);
    }

    int place;
    while((place = input.read()) != -1)
        output.write(place);

    input.close();
    output.close();
    welcomeSocket.close();
    connectionSocket.close();
}

The client code is essentially the same. I have tried using different buffer sizes, including the default (by not specifying a buffer size), but they are all running at approximately the same speed. Any pointers on how I can increase my performance?

Thank you for your time!

  • 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-25T14:38:27+00:00Added an answer on May 25, 2026 at 2:38 pm
    while((place = input.read()) != -1)
    

    You’re reading one byte at a time from the buffer. The overhead of calling this method millions of times is rather large.

    I would suggest reading more than one byte into a buffer with the other version (and writing the same way):

    public int read(byte[] b,
                int off,
                int len)
    

    Example:

    byte[] myBuffer = new byte[BUF_SIZE];
    while((place = input.read(myBuffer, 0, BUF_SIZE)) != 1)
        output.write(myBuffer, 0, place);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As part of a homework assignment, I have to program a simple chess game
I have been given a homework assignment where I need to create a captcha
I need little help on a homework assignment. I have to create a 10
Have a homework assignment in which I'm supposed to create a vector of pointers
I have a homework assignment that asks to create an order form. The order
I'm still not very good with data structures, but I have this homework assignment
I have a homework assignment where I need to take input from a file
(this is indirectly a part of a much larger homework assignment) I have something
I have three questions regarding a homework assignment for C++. The goal was to
I have done a homework assignment, here is the problem statement: Your program should

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.