Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ boost.asio server and client connection undersanding

i started learning boost.asio and i have some problems with undersanding tcp connections. There is example from official boost site:

  #include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

std::string make_daytime_string()
{
  using namespace std; // For time_t, time and ctime;
  time_t now = time(0);
  return ctime(&now);
}

int main()
{
  try
  {
    boost::asio::io_service io_service;

    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));

    for (;;)
    {
      tcp::socket socket(io_service);
      acceptor.accept(socket);

      std::string message = make_daytime_string();

      boost::system::error_code ignored_error;
      boost::asio::write(socket, boost::asio::buffer(message),
          boost::asio::transfer_all(), ignored_error);
    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

there is question, why if i want to connet to this server via client i have t write:

    boost::asio::io_service io_service;

tcp::resolver resolver(io_service);
tcp::resolver::query query(host_ip, "daytime"); //why daytime?
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;

why daytime?, what it meant and where it is inicialized in server, or i just doesn't missed somefing?

there is full client code : www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial/tutdaytime1.html thanks for explanation in advance

like image 247
Edgar Buchvalov Avatar asked Nov 18 '11 17:11

Edgar Buchvalov


People also ask

What is ASIO boost?

Boost. Asio is a cross-platform C++ library for network and low-level I/O programming that provides developers with a consistent asynchronous model using a modern C++ approach. Overview. An overview of the features included in Boost. Asio, plus rationale and design information.

What does ASIO stand for C++?

Asio stands for asynchronous input/output. This library makes it possible to process data asynchronously. Asynchronous means that when operations are initiated, the initiating program does not need to wait for the operation to end.

Who is using Boost ASIO?

The project is sponsored by Automatak. The stack uses a completely asynchronous design. The core library is written in C++11 and has abstracted execution services, timers, and physical layers called the Platform Abstraction Layer (PAL).

Is boost ASIO thread safe?

Like a regular Boost. Asio socket, a stream is not thread safe. Callers are responsible for synchronizing operations on the socket using an implicit or explicit strand, as per the Asio documentation.


2 Answers

Daytime is simply another protocol (like FTP, etc) and it uses port 13. If you want to connect to the server on a specific port number, then your code would look like this:

tcp::resolver::query query(host_ip, "5678"); // 5678 is the port number
like image 161
Marlon Avatar answered Sep 23 '22 13:09

Marlon


daytime is the service name, this is well described in the tcp::resolver::query documentation

service_name

A string identifying the requested service. This may be a descriptive name or a numeric string corresponding to a port number. May be an empty string, in which case all resolved endpoints will have a port number of 0.

like image 23
Sam Miller Avatar answered Sep 22 '22 13:09

Sam Miller