Socket programming examples in C++
Simple examples that show how to do socket programming in C++. Use
to compile all the code using the supplied
Simple Echo Server and Client
a basic echo server and client. The server handles only one client at
a time and simply sends back to the client whatever it receives.
echo-client-simple and see what
Caution: both the client and the server do not properly check the return values of send() and recv(). This means that long messages may not be echoed properly. Better code is in subsequent examples. This code is here just to show the basics.
Slow Echo Server
To illustrate the poor quality of the code in the simple echo server
and client, the file
echo-server-slow.cc is an echo server that
sends replies only one character at a time. While this is not how any
server is written, it illustrates what can happen when sending larger
messages across the network. Those messages can be split into smaller
pieces by TCP, so the a developer cannot assume that a single call to
recv() will get the entire message.
echo-client-simple and see what happens.
Echo Server and Client
A more functional and complete echo server and client. The client sends messages terminated by a newline, and the server parses until it receives a newline. Both client and server check the return value of send() and recv() and use loops to be sure the entire message is sent or received in each case.
The main programs are in
bulk of the server code is in
server.h. The bulk of
the client code is in