0

Objective

I'm writing an HTTP server in C++17.

For the sake of the discussion, I reduce the requirements to:

  1. Simple HTTP Echo Server.
  2. Expects multiple clients.
  3. Clients constantly send simple GET requests.
  4. Handle each client in a separate thread.
  5. Respond by 200 OK

Winsock

I read this example: Winsock Server Source Code.

I understand how to adjust this example to answer the requirements (similar to what was suggested here):

  1. Create a std::thread that listen for clients
  2. When a client is accepted, create a new thread for the client and pass the new SOCKET.

WinHTTP

I also want to experiment with WinHTTP as well. So, I read this: HTTP Server Sample Application .

But, I got a bit lost trying to apply the same "tactic" as before. There is no equivalent WinHTTP function to Winsock's accept() function that will allow me to create a thread per client.

Question

Assuming the approach I intend to apply in Winsock is valid, is there a similar approach to make WinHTTP handle each connection/client in a separate thread?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
idanshmu
  • 5,061
  • 6
  • 46
  • 92
  • 2
    Did you read the [HTTP Server API](https://learn.microsoft.com/en-us/windows/win32/http/http-api-start-page) documentation yet? For what you are attempting to do, I would suggest using the API in [*asynchronous* mode](https://learn.microsoft.com/en-us/windows/win32/http/http-completion-routines), so that you can associate an `OVERLAPPED` struct with each request, and then spin up multiple threads to service the I/O completions as needed. – Remy Lebeau Mar 02 '21 at 19:03
  • 1
    No, you are not going to be able to use a one-thread-per-client model in WinHTTP like with WinSock. But on the other hand, if you need to deal with a lot of clients simultaneously, a one-thread-per-client model is not the best choice to begin with. WinSock can do asynchronous I/O, too. – Remy Lebeau Mar 02 '21 at 19:05

0 Answers0