Photo by Ben Griffiths

Socket Protocols

In the previous articles, I used a very simplistic protocol. In real-world situations, this protocol is not sufficient. A communications protocol is required to provide a more robust connection between client and server. This protocol allows us to validate that messages are sent correctly and generate appropriate responses that can also be validated. Designing a communication protocol is a nontrivial task. Rather than creating a new protocol from scratch, I would look for an existing protocol that matches your use case. ...

May 29, 2016 · 6 min · Loki Astari (C)2016
Photo by Oscar Nilsson

C++ Wrapper for Socket

The last two articles examined the “C Socket” interface provided by the OS. In this article, I wrap this functionality in a simple C++ class to provide guaranteed closing and apply a consistent exception strategy. The first step is to rewrite the client/server code with all the low-level socket code removed. This will help identify the interface that the wrapper class needs to implement. The client code becomes trivial. Create a ConnectSocket specifying the host and a port. Then, communicate with the server using the putMessage() and getMessage(). Note: I am continuing to use the trivial protocol that was defined in the last article: putMessage() writes a string to the socket then closes the connection; getMessage() reads a socket until it is closed by the other end (I will cover more sophisticated protocols in a subsequent article). ...

May 26, 2016 · 4 min · Loki Astari (C)2016
Photo by Roman Synkevych

Socket Read/Write

Checking read/write success The read() and write() commands can fail in several ways but can also succeed without reading/writing all the data. A common mistake is not checking the amount of data read/written from/to a stream. Interestingly, not all error conditions are fatal, and reading/writing can potentially be resumed after an error. Read To determine whether you have read all the information available on a stream, you need to define a communication protocol (like HTTP). The protocol for the first version of this server is very simple. Messages are passed as strings (not null-terminated), and the end of the message is marked by closing the write stream. Thus, a client can send one message and receive one reply with each connection it makes. ...

April 9, 2016 · 8 min · Loki Astari (C)2016
Photo by Ken Blode

Socket Programming in C

Building a simple client/server application is the standard first internet-based application developers attempt. These applications are built on top of the socket communication library, but socket programming in C++ is not obvious as there are no standard libraries, and thus, you have to fall back to the C API. The closest “standardish” sort of thing we have is Boost.asio, which is at the other end of the spectrum in terms of API and involves a cognitive leap to understand what is happening underneath (or you can trust the library maintainers). The other alternative is libcurl; the “easy curl” layer is an abstraction of the socket() API, while the “multi curl” layer is an abstraction of the pselect() API that allows multiple sockets to be handled in a single thread. ...

April 8, 2016 · 4 min · Loki Astari (C)2016