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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:02:31+00:00 2026-05-26T05:02:31+00:00

I am currently implementing a multithreaded proxy server in java which will accept messages

  • 0

I am currently implementing a multithreaded proxy server in java which will accept messages from clients and forward them to another server which will then acknowledge the reception of the message. However, i’m having trouble doing so. Could someone point out what i am doing wrong? Thanks.

ProxyApp:

public class ProxyApp {

    public static ServerSocket server = null;
    public static Socket client = null;


    public static void main(String[] args)
    {
        try
        {
            server = new ServerSocket(6789);
            Socket clientsocket = null;

            while(true)
            { 
                client = server.accept();      

                if(client.isConnected())
                {
                    System.out.println("Proxy is currently listening to client on port 6789");
                }
                Thread t1 = new ProxyHandler(client);
                t1.start();

                clientsocket = new Socket(InetAddress.getLocalHost(), 6780);
                if(clientsocket.isBound())
                {
                    System.out.println("Clientsocket successfully connected on port 6780");
                }

                ProxyHandler t2 = new ProxyHandler(clientsocket);
                t2.start();                                                               
            }
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
        }
    }
}

ProxyHandler:

public class ProxyHandler extends Thread {

    private Socket socket;
    private String message;

    public ProxyHandler(Socket socket)
    {
        this.socket = socket;        
    }

    @Override
    public void run()
    {       
        message = "";
        try
        {
            DataInputStream in = new DataInputStream(socket.getInputStream());
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            while(true)
            {
                message = in.readUTF();
                out.writeUTF(message);

                System.out.println(message);
            }
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
            System.exit(2);
        }
    }
}

ClientClass:

public class ClientClass {

    public static void main(String[] args)
    {
        try
        {
            Socket client = new Socket(InetAddress.getLocalHost(), 6789);

            if(client.isBound())
            {
                System.out.println("Successfully connected on port 6789");
            }

            Scanner scanner = new Scanner(System.in);

            DataInputStream inFromProxy = new DataInputStream(client.getInputStream());
            DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream());

            while(true)
            {
                String message;

                System.out.print("Enter your message: ");
                message = scanner.next();

                outToProxy.writeUTF(message);
                System.out.println(inFromProxy.readUTF());
            }
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
            System.exit(2);
        }
    }
}

ServerClass:

public class ServerClass {

    public static void main(String[] args)
    {
        try
        {
            ServerSocket server = new ServerSocket(6780);

            if(server.isBound())
            {
                System.out.println("Server successfully connected on port 6780");
            }

            Socket client = null;
            while(true)
            {
                client = server.accept();

                if(client.isConnected())
                {
                    System.out.println("Proxy is connected");
                }

                DataInputStream inFromProxy = new DataInputStream(client.getInputStream());
                DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream());

                System.out.println(inFromProxy.readUTF());

                outToProxy.writeUTF("Message has been acknowledged!");
            }    
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
            System.exit(2);
        }
    }
}
  • 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-26T05:02:32+00:00Added an answer on May 26, 2026 at 5:02 am

    The in and out in the following code represent message from and to the same machine:

    DataInputStream in = new DataInputStream(socket.getInputStream());
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    

    so when you read from in and write to out, all you are doing is creating an echo service.

    message = in.readUTF();
    out.writeUTF(message);
    

    For a proxy, you want to read from the client and write to the server, and vice versa.

    It seems you want something closer to this:

        client = server.accept();      
        clientsocket = new Socket(InetAddress.getLocalHost(), 6780);
        Thread t1 = new ProxyHandler(client, clientsocket );
        t1.start();
        Thread t2 = new ProxyHandler(clientsocket, client);
        t2.start();
    

    where the job of the first thread is to send data from the client to the server and the second’s job is to send from the server to the client.

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

Sidebar

Related Questions

I'm currently implementing a .NET wrapper for a Java library by using JNI to
I'm currently implementing a Monotouch application that will eventually be ported to Monodroid. The
I'm currently implementing a historical report store where users will store only 1 report
I am currently implementing SiteMinder for the site, which looks for a key called
I'm currently implementing a raytracer. Since raytracing is extremely computation heavy and since I
I'm currently implementing a JavaScript library that keeps track of the history of changes
I'm currently implementing a very complex tree structure to allow for near-instant data access,
We are currently implementing MOSS 2007 to replace an older portal system (Plumtree) and
I'm currently implementing a RESTful API (nothing serious, just for a blog engine i'm
i am currently implementing a binary tree in c++ and i want to traverse

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.