I am attempting to implement a proxy server and a proxy client in C (separately yes!). So in this scenario, we have a client, a proxy client, a proxy server and a server.
The proxy client accepts a request for a specific host (server) and port number from a client and opens a single connection for that particular host/port pair with the proxy server.
This connection is persistent. If any more requests come for the same host/port combination, it must be sent through the same connection. The requests are numbered with id's 0,1,... in each connection to distinguish one from the other.
For every request (not connection. there may be multiple requests per connection.) received from the proxy client, the proxy server opens a connection for each request to the destination server.
However there needs to be a limit to the number of connections that can be opened to the destination server. I want to know how I can implement this limit.
Say I specify maximum number of connections that can be opened to h1 destination server to be 2. If a third connection comes in from the client proxy, what should the server proxy do?
Should I implement some kind of queue to store incoming requests, if there are already 2 open connections to the destination server?
If yes, then should there be separate queues for separate host/port combinations? And how would such a structure be implemented in C?
I'm feeling incredibly lost right now. Would really appreciate some guidance here. P.S. I'm coding in C using sockets.