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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:18:47+00:00 2026-06-17T18:18:47+00:00

I need to write a quick and easy port forwarder. At the moment it

  • 0

I need to write a quick and easy port forwarder. At the moment it accepts connections and can read from clients just fine, but it doesn’t get anything from the local machine it is running on. (FYI this was made to forward a webserver, because I can’t find the app I usually use to forward VS2010/2012 to the network so I made my own).

I know the exception swallowing and such is bad but that’s not important right now, I can deal with that later.

import java.io.*;
import java.net.*;

public class SocketForward {
  int src = 0;
  int dest = 0;
  public static void main(String args[]) throws Exception {
    if (args.length < 2) {
      System.out.println("Usage: java SocketForward <source port> <destination port>");
      System.exit(1);
    } else {
      new SocketForward().startRun(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
    }
  }
  public void startRun(int src, int dest) throws Exception {
    try {
      this.src = src;
      this.dest = dest;
    } catch (NumberFormatException e) {
      System.out.println("Usage: java SocketForward <source port> <destination port>");
      System.exit(1);
    }
    ServerSocket server = new ServerSocket(dest);
    System.out.println("Waiting for connections...");
    while (!server.isClosed()) {
      new ConnHandler(src, server.accept());
    }
  }
}

class ConnHandler implements Runnable {
  Socket clientSocket;
  Socket hostSocket;
  int hostport;
  Thread thread;
  BufferedOutputStream hostOut;
  BufferedOutputStream clientOut;
  BufferedInputStream hostIn;
  BufferedInputStream clientIn;
  public ConnHandler(int hostport, Socket clientSocket) {
    System.out.println("Connected to " + clientSocket.getInetAddress());
    this.clientSocket = clientSocket;
    this.hostport = hostport;
    this.thread = new Thread(this);
    thread.start();
  }
  @Override
  public void run() {
    try {
      hostSocket = new Socket("", hostport);
      System.out.println("Connected to localhost:" + hostport);
      hostOut = new BufferedOutputStream(hostSocket.getOutputStream());
      clientOut = new BufferedOutputStream(clientSocket.getOutputStream());
      hostIn = new BufferedInputStream(hostSocket.getInputStream());
      clientIn = new BufferedInputStream(clientSocket.getInputStream());
      new Thread() {
        public void run() {
          while (this == Thread.currentThread()) {
            byte[] buf = new byte[8192];
            try {
              int len = 0;
              while ((len = ConnHandler.this.clientIn.read(buf)) > 0) {
                System.out.println("Client: " + new String(buf, 0, len));
                ConnHandler.this.hostOut.write(buf, 0, len);
              }
            } catch (Exception e) {
              e.printStackTrace();
              break;
            }
          }
        }
      }.start();
      new Thread() {
        public void run() {
          while (this == Thread.currentThread()) {
            byte[] buf = new byte[8192];
            try {
              int len = 0;
              while ((len = ConnHandler.this.hostIn.read(buf)) > 0) {
                System.out.println("Host: " + new String(buf, 0, len));
                ConnHandler.this.clientOut.write(buf, 0, len);
              }
            } catch (Exception e) {
              e.printStackTrace();
              break;
            }
          }
        }
      }.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
  • 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-06-17T18:18:49+00:00Added an answer on June 17, 2026 at 6:18 pm

    You are not doing any flushing, thereby introducing non-transparent man-in-the-middle behavior. This may well disturb the data flow as expected by the client and server.

    A side note on code style: it is more convenient to have try-catch outside the while loop, so it breaks out automatically:

    try {
      while (...) ...
    } catch (Throwable t) {
       t.printStackTrace();
    }
    

    Note also Throwable instead of Exception—you’ll want to know about any error that breaks your code, not just Exceptions.

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

Sidebar

Related Questions

I need to write a script in Matlab, which will read some data from
I need a quick easy way to get a string from a file in
I need to write a quick bash script that asks the user which mac
I need to write effective and quick method to search byte array for given
I hope this is a quick question I hope. I need to write some
The quick overview is this: for my web app I can write most of
I need do some quick and dirty logging and I was asked to write
Hopefully just a quick question :) I'm trying to write little jQuery that will
Need to write a quick method static boolean varTest(String id) that tests of the
I love the windows 7 sticky note app and need to write a quick

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.