lib_netsockets

Pedro Vicente
lib_netsockets is a set of C++ classes that use POSIX/Winsock sockets with implementation of TCP client/server using JSON messages, and HTTP, FTP clients. The following steps shown an example of a client/server exchange using JSON.

TCP client/server exchange using JSON


A TCP server listening on port 2000 can be made with

tcp_server_t server(2000);
while (true)
{
  socket_t socket = server.accept_client();
  handle_client(socket);
  socket.close();
}
server.close();

A TCP client that connects to the server listening at address "127.0.0.1" on port 2000 is made with

tcp_client_t client("127.0.0.1", 2000);

JSON message requests are made using the jansson JSON library. An example that defines a JSON object defined as

{"start_year": 2016}

is

json_t *request = json_object();
json_object_set_new(request, "start_year", json_integer(2016));

The connection and message transmission to the server is made with

client.open();
client.write(request);

The server handles the client request in the function handle_client(), that uses the socket_t class read() function to parse the JSON message. In this case, the object with JSON key "start_year" is obtained, then a JSON response is made with and object with key "next_year", that is written to the socket, using the socket_t class write()

void handle_client(socket_t& socket_client)
{
  json_t *response = NULL;
  json_t *request = socket_client.read();

  //get dates
  json_t *json_obj;
  json_obj = json_object_get(request, "start_year");
  json_int_t start_year = json_integer_value(json_obj);
  std::cout << "server received: " << std::endl;
  std::cout << "start_year: " << start_year << std::endl;

  //do response
  response = json_object();
  json_object_set_new(response, "next_year", json_integer(start_year + 1));
  socket_client.write(response);
}

The client in turn receives the server response, and closes the opened socket, with

json_t *response = client.read();
json_t *json_obj;
json_obj = json_object_get(response, "next_year");
json_int_t next_year = json_integer_value(json_obj);
std::cout << "client received: " << std::endl;
std::cout << "next_year: " << next_year << std::endl;
client.close();

HTTP client


HTTP (Hypertext Transfer Protocol) is a request and response protocol, where a request is sent by a client to a server, and a response is sent from a server to a client. One of the reasons for its widespread success is probably its simplicity.

HTTP request

An HTTP request consists English based sequences of characters, divided in: An example of a GET request, that requests a retrieval of the file located at the server path /data/hello.txt, using the HTTP/1.1 protocol, to a host with domain at 127.0.0.1 (i.e localhost) is

GET /data/hello.txt HTTP/1.1
Host: 127.0.0.1
Connection: close

HTTP response

Running the above request on a Microfost Windows IIS web server returns a response similar to

HTTP/1.1 200 OK
Content-Type: text/plain
Last-Modified: Thu, 25 Aug 2016 04:19:56 GMT
Accept-Ranges: bytes
ETag: "288e59ef87fed11:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Thu, 25 Aug 2016 04:20:21 GMT
Connection: close
Content-Length: 5

hello


where the contents of the file /data/hello.txt can be seen on the response (the 5 byte sequence of characters 'hello').

HTTP client usage

A usage of the get() method implemented in lib_netsockets is

http_t client(host_name, 80);
client.get(path_name, verbose);

FTP client


The File Transfer Protocol (FTP) is a network protocol used to transfer files between a client and server. FTP uses uses two separate control and data connections between client and server. An FTP conversation consists of requests sent by the client and responses sent by the server. An FTP request consists of a request verb, optionally, a space followed by a parameter, and CRLF characters (i.e ASCII values 13 and 10). The server responds over the control connection with three-digit status codes in ASCII with an optional text message.

Following, examples are shown for a request (color red) and response (color green) exchange for a USER, PASS, PASV, NLST, SIZE, RETR and QUIT requests.

Connection

After a connection is made, the server sends an initial response, called the greeting.

220 (vsFTPd 3.0.2)


The USER verb

A USER request has a parameter with a username. In this case, the response asks for a password.

USER anonymous



331 Please specify the password.


The PASS verb

A PASS request has a parameter with a password.

PASS password



230 Login successful.


The PASV verb

A PASV request asks the server to accept a data connection on a new TCP port selected by the server.

PASV



227 Entering Passive Mode (100,200,300,400,202,12).


The NLST verb

The NLST request asks the server to send the contents of a directory over the data connection already established by the client.

NLST



150 Here comes the directory listing.



226 Directory send OK.


The SIZE verb

The SIZE request asks the size of a file. The parameter is the file name. The response sends the size in bytes after the 3 digit response code 213.

SIZE filename.txt



213 2


The RETR verb

A RETR request asks the server to send the contents of a file over the data connection already established by the client. The RETR parameter is a file name.

RETR filename.txt



150 Opening BINARY mode data connection for filename.txt (2 bytes).



226 Transfer complete.


The QUIT verb

A QUIT request terminates the connection.

QUIT



221 Goodbye.


FTP client usage

lib_netsockets FTP client usage that retrieves a file list from a FTP server, and retrieves the first file from the list is

ftp_t ftp(host_name, 21);
ftp.login(user_name, pass);
ftp.get_file_list();
if (ftp.m_file_nslt.size())
{
 ftp.get_file(ftp.m_file_nslt.at(0).c_str());
}
ftp.logout();

References