Hey all. I am completely stuck here. I want to implement a round robin based service capable of load balancing http requests.

ATM I have a thread with an infinite loop which listens for TCP connections. When it finds a connection, it runs another thread which will process the request.

I guess I'm just not sure what way round robin is supposed to actually be implemented, I understand that it should pass each new connection request to the next server in line, eventually distributing connections evenly across the array of machines being load balanced.

But I don't know how I go about it. Do I create a new webserver each time I find a connection? If I am already running a thread that is processing a HTTP request, what the hell is the point of creating more servers when the program will have to wait for the thread to finish anyway?

Pseudo-code would be a big help for this.

Recommended Answers

All 3 Replies

creating more servers when the program will have to wait for the thread to finish anyway?

What program would be waiting? Are all the requests coming from one thread in one program?

If each request is given a thread to service it, a limiting factor would be the number of concurrent threads your system would support. Some requests might have to wait for an available thread.

Well, one of my problems is, I don't know how to make multiple threads do the same thing, I have a while(true) loop which loops until a TCP connection is made, and then handles the http request. The request is simply me typing localhost:1234 into the browser. I need to be able to have more than 1 HTTP request each time.

I am sorry If I am not explaining this well, The main reason why I can't do this is because I don't understand it very well.

I have a while(true) loop which loops until a TCP connection is made

How does it detect a connection? Usually the code waits in ServerSocket accept method until a connection comes.
When there is a connection you get a socket that is passed to a method in a thread to handle that request and then the code with the accept() method loops back to wait for the next connection.

have more than 1 HTTP request each time.

Write a simple client program that attempts to connect to the server.
You could use multiple threads and loops in the client code to send lots of requests to the server.

how to make multiple threads do the same thing,

Each thread uses a new object to handle the request.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.