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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:41:20+00:00 2026-06-18T07:41:20+00:00

I want to write a simple http proxy server in Java using sockets. I

  • 0

I want to write a simple http proxy server in Java using sockets. I wrote a test prototype composed of several tutorials which I had found in the internet. So long I have come with this:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleProxyServer
{
    public static final int portNumber = 55558;
    public static final int maxConnections = 100;

    public static void main( String[] args )
    {
        SimpleProxyServer proxyServer = new SimpleProxyServer();
        proxyServer.start();
    }

    public void start()
    {
        System.out.println("Starting the SimpleProxyServer ...");
        try
        {
            ServerSocket serverSocket = new ServerSocket( portNumber, maxConnections );
            byte[] buffer = new byte[10000];

            boolean run = true;
            while( run )
            {
                Socket clientSocket = serverSocket.accept();

                InputStream clientInputStream = clientSocket.getInputStream();

                // reading the request and put it into buffer
                final int readBytesCount = clientInputStream.read( buffer );
                if( readBytesCount < 0)
                    continue;

                String browserRequest = new String( buffer, 0, readBytesCount );
                System.out.println( browserRequest );

                // extract the host to connect to
                final int hostNameStart = browserRequest.indexOf( "Host: " ) + 6;
                final int hostNameEnd = browserRequest.indexOf( '\n', hostNameStart );
                final String hostName = browserRequest.substring( hostNameStart, hostNameEnd - 1 );
                System.out.println( "Connecting to host " + hostName );

                // forward the response from the proxy to the server
                Socket hostSocket = new Socket( hostName, 80 );
                OutputStream hostOutputStream = hostSocket.getOutputStream();
                System.out.println( "Forwarding request to server" );
                hostOutputStream.write( buffer, 0, readBytesCount );
                hostOutputStream.flush();

                ProxyThread thread1 = new ProxyThread( clientSocket, hostSocket );
                thread1.start();

                ProxyThread thread2 = new ProxyThread( hostSocket, clientSocket );
                thread2.start();
            }
            serverSocket.close();
        }
        catch( IOException e )
        {
            System.err.println( "IO Error: " + e.getMessage() );
            e.printStackTrace();
        }
    }
}

class ProxyThread extends Thread 
{
    private Socket incoming, outgoing;

    ProxyThread( Socket in, Socket out )
    {
        incoming = in;
        outgoing = out;
    }

    // Overwritten run() method of thread,
    // does the data transfers
    public void run()
    {
        System.out.println( "Starting proxy thread" );
        try
        {
            OutputStream toClient = outgoing.getOutputStream();
            InputStream fromClient = incoming.getInputStream();

            int numberRead = 0;
            byte[] buffer = new byte[10000];
            do
            {
                numberRead = fromClient.read( buffer );
                System.out.println( "Read " + numberRead + " bytes" );
                System.out.println( "Buffer: " + buffer );
                if( numberRead > 0 )
                {
                    toClient.write( buffer, 0, numberRead );
                    System.out.println( "Sent " + numberRead + " bytes" );
                }
            }
            while( numberRead > 0 );

            System.out.println( "Closing all streams and sockets" );
            toClient.flush();
            incoming.close();
            outgoing.close();
        }
        catch( IOException e ) 
        {
            System.err.println( "IO Error: " + e.getMessage() );
        }
        catch( ArrayIndexOutOfBoundsException e )
        {
            System.err.println( "Index error: " + e.getMessage() );
        }
    }
}

While testing with a browser (Firefox if it helps) it hangs and freezes on the fromClient.read(buffer) call for a long time and returns -1, but the browser shows a connection refuse much earlier. What’s the cause of it? Is it caused by the InputStream.read() blocking or it’s a kinda race? Maybe the whole approach is wrong? Or it’s some snag with threads?

P.S. My “native” language is C++. Though I have some occasional experience with the Java programming language, I still don’t have enough experience with it’s libs and particularly with sockets.

P.P.S. I thought that such thing as an http proxy server has multiple tutorials and how-to on the internet, but all I’ve found is single threaded or don’t read the destintion URL and just transmits the request to the next proxy. So maybe the post will help someone in writing own one. If it ever gets fixed…

  • 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-18T07:41:22+00:00Added an answer on June 18, 2026 at 7:41 am

    You must process each accepted socket in a separate thread, and that includes reading the CONNECT command. Otherwise a holdup reading it will block the accept thread from accepting new connections.

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

Sidebar

Related Questions

I want to create a simple http proxy server that does some very basic
I'm trying to build a simple HTTP Server using Java, using java.net.ServerSocket = new
I am using http://code.google.com/p/feedparser/ to write a simple news integrator. But I want pure
I'm using HTTP::Server::Simple::CGI to write a simple webserver. Within my handler, I have to
I want to write a simple P2P test app which uses HTTP as underlying
I want to write simple script to copy/backup directory then remove on server startup.
I'm new to Java programming. I want to write simple Java network protocol. I
I want to write a simple chat application (for test use). The users and
I want to write a simple web framework myself using WSGI, Python. I am
I want to write a simple web proxy, for exercise. Here's the code 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.