简体   繁体   English

C ++ boost.asio服务器和客户端连接欠载

[英]C++ boost.asio server and client connection undersanding

i started learning boost.asio and i have some problems with undersanding tcp connections. 我开始学习boost.asio,并且我在解决tcp连接时遇到了一些问题。 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 有完整的客户代码:www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial/tutdaytime1.html感谢提前解释

白天只是另一种协议(如FTP等),它使用端口13.如果要在特定端口号上连接到服务器,那么您的代码将如下所示:

tcp::resolver::query query(host_ip, "5678"); // 5678 is the port number

daytime is the service name, this is well described in the tcp::resolver::query documentation daytime是服务名称,这在tcp::resolver::query 文档中有详细描述

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. 可以是空字符串,在这种情况下,所有已解析的端点的端口号都为0。

"daytime" means the port used for daytime services. "daytime"表示用于日间服务的端口。 It's the 13 you see at the acceptor. 这是你在接受者身上看到的13 Here is a list of well-known ports (whatever that means): 这是一个众所周知的端口列表(无论这意味着什么):

http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM