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();
Header: value
GET /data/hello.txt HTTP/1.1
Host: 127.0.0.1
Connection: close
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_t client(host_name, 80);
client.get(path_name, verbose);
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.
220 (vsFTPd 3.0.2)
USER anonymous
331 Please specify the password.
PASS password
230 Login successful.
PASV
227 Entering Passive Mode (100,200,300,400,202,12).
NLST
150 Here comes the directory listing.
226 Directory send OK.
SIZE filename.txt
213 2
RETR filename.txt
150 Opening BINARY mode data connection for filename.txt (2 bytes).
226 Transfer complete.
QUIT
221 Goodbye.
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();