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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:43:08+00:00 2026-05-27T20:43:08+00:00

I have a Java program that mirrors a connection from a client server to

  • 0

I have a Java program that mirrors a connection from a client server to a remote server. The mirror send data find, but does not receive. I cannot for the life of me figure out why. Here is my code:

Socket client = new Socket("127.0.0.1", 42001);
System.out.println("Connected to client!");
Socket server = new Socket(serverAddress, serverPort);
System.out.println("Connected to server!");

BufferedReader clientin = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter scratchout = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));

BufferedReader serverin = new BufferedReader(new InputStreamReader(server.getInputStream()));
BufferedWriter serverout = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));

int i;
boolean serverNeedsFlush = false;
boolean clientNeedsFlush = false;
while (true)
{
    while (clientin.ready())
    {
        i = clientin.read();
        serverout.write(i);
        serverNeedsFlush = true;
    }
    if(serverNeedsFlush)
    {
        serverout.flush();
        serverNeedsFlush = false;
    }
    while (serverin.ready())
    {
        i = serverin.read();
        System.out.print((char)i);
        scratchout.write(i);
        clientNeedsFlush = true;
    }
    if(clientNeedsFlush)
    {
        scratchout.flush();
        clientNeedsFlush = false;
    }
}
  • 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-27T20:43:09+00:00Added an answer on May 27, 2026 at 8:43 pm

    If your trying to forward data from one socket to another it would probably be a better idea to use the socket streams directly rather than decorating them.

    As other posters have suggested you should use threads to do this. It will make life easier. You can then use the threads to do a basic in to out stream copy like below.

    public static void streamCopy(InputStream in, OutputStream out)
           throws IOException{
    
            byte[] data = new byte[1024];
            int length;
            do{
                length = in.read(data);
                if(length > 0){
                    out.write(data, 0, length);
                    out.flush();
                }
            }while(length != -1);
    
    }
    

    When the method above returns you will have read the entire in stream and written it in to the out stream. Your run method for your thread or runnable could look something like this.

    public void run() {
    
        Socket inSock = null;
        Socket outSock = null;
        try{
            inSock = new Socket(inHost, inPort);
            outSock = new Socket(inHost, inPort);
            /* Set up some socket options here (timeouts, buffers etc)*/
    
            /* Insert pre copy actions */
    
            /* This method won't return until inSock's inputStream hits end of stream. 
             * and all the data has been written to outSock's outputStream and flushed. */
            streamCopy(inSock.getInputStream(), outSock.getOutputStream());
    
            /* In order to really do this correctly you should create an 
             * application protocol that verifies the upstream receiver 
             * is actually getting the data before you close the socket. */
    
            /* Insert post copy actions */
    
        }catch(Exception e){
            /* Corrective action or logging here */
        }finally{
            /* Don't forget to close the sockets. */
            if(inSock != null){
                try{
                    inSock.close();
                }catch(Exception e){
                    /* Don't care */
                }
            }
            if(outSock != null){
                try{
                    outSock.close();
                }catch(Exception e){
                    /* Don't care */
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java program that opens a socket connection to a server that
I have a simple java program that runs fine in eclipse but cannot find
I have a Java program that creates a server socket and accepts connections from
actually i have a java program that reads the data from a specific XML
I have a Java program that is constantly being sent UDP data from an
I have a Java program that executes from Spring Qquartz every 20 seconds. Sometimes
I have a Java program that is mostly GUI and it shows data that
I have a java program that acts as a POP3 client using javax.mail. I
I have a Java program that uses OAuth for communication with a server to
If I have a Java program that listens to multicast data, how can I

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.