SimpleSocket  0.1
Some wrappers that make using sockets easier in C++
Simple Sockets Intro

Introduction

From C++ the full POSIX and OS socket functions are available. These are very powerful, offer well known semantics, but are somewhat of a pain to use.

Various libraries have attempted to offer "C++ native" socket environments, but most of these offer different semantics that are less well known, or frankly, obvious.

This set of files offers the BSD/POSIX socket API relatively unadorned, with the original semantics, and fully interoperable with the usual system calls.

To find out more about Simple Sockets, please also consult its GitHub page.

Basic example code

Note that the 'raw' socket code below is just as reliable as the original socket API. In other words, it is recommended to use higher level functions.

#include "swrappers.hh"
int main()
{
int s = SSocket(AF_INET, SOCK_STREAM);
ComboAddress webserver("52.48.64.3", 80);
SConnect(s, webserver);
SWrite(s, "GET / HTTP/1.1\r\nHost: ds9a.nl\r\nConnection: Close\r\n\r\n"); // will throw on partial write
std::string response = SRead(s); // will read the entire universe into RAM
cout<< response << endl;
close(s);
}

Some sample code using the higher-level classes

auto addresses=resolveName("ds9a.nl"); // this retrieves IPv4 and IPv6
for(auto& a : addresses) {
a.setPort(80);
cout << "Connecting to: " << a.toStringWithPort() << endl;
RAIISocket rs(a.sin.sin_family, SOCK_STREAM);
sc.connect(a);
sc.writen("GET / HTTP/1.1\r\nHost: ds9a.nl\r\nConnection: Close\r\n\r\n");
std::string line;
while(sc.getLine(line)) {
cout<<"Got: "<<line;
}
}