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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:01:35+00:00 2026-05-17T19:01:35+00:00

I have build a simple application that opens a ServerSocket, and on connection, it

  • 0

I have build a simple application that opens a ServerSocket, and on connection, it connects itself to another server socket on a remote machine. To implement port forwarding, I use two threads, one that reads from the local inputstream and streams to the remote sockets outputstream, and vice versa.

The implementation feels a bit inperformant, and so I ask you if you know a better implementation strategy, or even have some code lying around to achive this in a performant way.

PS: I know I could use IPTables on Linux, but this has to work on Windows.

PPS: If you post implementations for this simple task, I will create a benchmark to test all given implementations. The solution should be fast for many small (~100bytes) packages and steady data streams.

My current implementation is this (executed on each of the two threads for each direction):

public static void route(InputStream inputStream, OutputStream outputStream) throws IOException {
    byte[] buffer = new byte[65536];
    while( true ) {
        // Read one byte to block
        int b = inputStream.read();
        if( b == - 1 ) {
            log.info("No data available anymore. Closing stream.");
            inputStream.close();
            outputStream.close();
            return;
        }
        buffer[0] = (byte)b;
        // Read remaining available bytes
        b = inputStream.read(buffer, 1, Math.min(inputStream.available(), 65535));
        if( b == - 1 ) {
            log.info("No data available anymore. Closing stream.");
            inputStream.close();
            outputStream.close();
            return;
        }
        outputStream.write(buffer, 0, b+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-17T19:01:35+00:00Added an answer on May 17, 2026 at 7:01 pm

    A couple of observations:

    • The one byte read at the start of the loop does nothing to improve performance. Probably the reverse in fact.

    • The call to inputStream.available() is unnecessary. You should just try to read to “buffer size” characters. A read on a Socket streamwill return as many characters as are currently available, but won’t block until the buffer is full. (I cannot find anything in the javadocs that says this, but I’m sure it is the case. A lot of things would perform poorly … or break … if read blocked until the buffer was full.)

    • As @user479257 points out, you should get better throughput by using java.nio and reading and writing ByteBuffers. This will cut down on the amount of data copying that occurs in the JVM.

    • Your method will leak Socket Streams if a read, write or close operation throws an exception. You should use a try ... finally as follows to ensure that the streams are always closed no matter what happens.


    public static void route(InputStream inputStream, OutputStream outputStream) 
    throws IOException {
        byte[] buffer = new byte[65536];
        try {
            while( true ) {
                ...
                b = inputStream.read(...);
                if( b == - 1 ) {
                    log.info("No data available anymore. Closing stream.");
                    return;
                }
                outputStream.write(buffer, 0, b+1);
            }
        } finally {
            try { inputStream.close();} catch (IOException ex) { /* ignore */ }
            try { outputStream.close();} catch (IOException ex) { /* ignore */ }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I develop tools in Autodesk Maya. Many of the tools I build have simple
How to build a simple recommendation system? I have seen some algorithms but it
I have a simple NAnt build file, which attempts to perform an update from
I have build server inside our domain (and it needs to be because it
I have a build script and as part of that script it copies a
I have a build server running CruiseControl.NET. It works well for the 7 projects
I have an ASP.Net application running under IIS 6. A simple page has two
I have to build an application in .Net (3.5) to pick up a TIFF
I have a simple document-based Cocoa app that acts as a viewer for .wav
I have been asked to build an application where I can drag and drop

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.