Hi all,

Im new here - so please bear with me ;)

Im pretty new to C++, although I have previous experience in C. I created a small socket app that allowed a client to connect via telnet and send some data for it to be assessed.

I have recently been developing a project in C++ that requires a basic webserver. Ive been rooting around for some resources on socket programming in C++, and have had no luck in doing so.

If possible im looking for something using the standard stuff I used in C like listen etc

Thanks

Dani AI

Generated

Quick, practical path for a small Linux webserver (builds on what started and what noted about Winsock):

Start by learning the POSIX socket calls and common pitfalls. Beej's Guide is a concise, example-driven introduction to the classic sequence (getaddrinfo -> socket -> bind -> listen -> accept -> recv/send), and the Linux man pages give the authoritative details and error notes you will need for production code. See Beej's Guide and the socket/getaddrinfo man pages on man7 for specifics and edge cases (address families, flags, errno handling).

If you want idiomatic C++ or cross-platform support (Linux + Windows), move to Boost.Asio for I/O and Boost.Beast if you want HTTP parsing/serialization instead of writing your own. Asio handles async/threading patterns and Beast gives you an RFC-compliant HTTP layer so you can focus on request handling rather than parsing quirks. See Boost.Asio docs and Boost.Beast docs.

Short troubleshooting checklist and gotchas:

  • Use getaddrinfo (IPv4/IPv6 agnostic) and AI_PASSIVE for servers.
  • Set SO_REUSEADDR before bind() to avoid TIME_WAIT bind failures; consider SO_REUSEPORT only when your kernel supports it.
  • Handle EINTR, EAGAIN/EWOULDBLOCK correctly and close sockets on errors.
  • Prevent SIGPIPE (or use MSG_NOSIGNAL) when writing to closed peers.
  • For scale use select/poll then epoll on Linux for many connections; avoid one-thread-per-connection unless you control connection counts.
  • Test with telnet, nc and curl -v during development.

A small plan: implement a single-threaded accept loop to learn behavior; then replace with select/poll or port to Boost.Asio/Beast. This keeps progress fast while avoiding subtle production bugs.

Recommended Answers

All 4 Replies

I got a mini chat server working using winsock but this only works for windows.

Sorry, I forgot to mention, my target system is Linux/*NIX

Not sure how good this is or if it even works since I use windows.

This shows how to make a client and server socket app.
here.

That one is for C, but ill give it a shot, see if there are any salvageable parts

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.