Rick Nijhuis
Rick Nijhuis

Reputation: 446

Receiving http requests with winsock

For educational purposes I am trying to make a web api in c++. the web api needs to be able to listen for http requests(GET, POST etc.), when it receives a http request it needs to be able to send data back to the client. Because it is for educational purposes I would like to do it without unnecessary libraries. Now the first thing I need to do is make the api able to receive requests and respond on that, after some research on google I found out that winsock is probably the most basic way to setup sockets for windows but I could find very little on receiving http requests.

My question is: Is it possible with winsock to receive a http request from the browser, and send data back to the browser?.

Upvotes: 1

Views: 1511

Answers (3)

verdery
verdery

Reputation: 531

You can check this page https://github.com/ReneNyffenegger/cpp-webserver to see simple winsock server implementation for HTTP. Web server implementation is not so difficult. Of course you should have time for it.

Upvotes: 0

David Haim
David Haim

Reputation: 26486

It is, Because HTTP is a protocol that (usually) uses TCP as the underlying transportation protocol.

But trying to build a real HTTP layer on top of a simple win32 socket is a bit too much even for an experienced C++ developer.

Many un-experienced C++ developers would probably dismiss this task as a "well, you just need to read some data, parse the headers, assemble your own HTTP response and send it back".

but.. You will have to support

  • TLS, with all the nasty private keys/public keys implementation
  • Redirection
  • Chunked Transfer
  • G-Zip transfer

and the list goes on and on..

So practically speaking, if you just want to to accept a socket, read some data and send some basic HTTP response than yes. If you want a reliable, professional HTTP library - probably no.

Upvotes: 0

Swordfish
Swordfish

Reputation: 13134

My question is: Is it possible with winsock to receive a http request from the browser, and send data back to the browser?

Yes. ^^

Upvotes: 1

Related Questions