简体   繁体   English

boost-asio如何知道运行服务器的端口?

[英]how does boost-asio know what port the server is running on?

So I was looking through the asio tutorials and I compiled the synchronious daytime client and the synchronious daytime server. 所以我正在浏览asio教程,并编译了同步的日间客户端和同步的日间服务器。 I was playing around with the code on the server end by changing the port (in the site's code they hard coded 13 as the port) to be passed in through the command line. 我正在玩服务器端的代码,通过命令行传入端口(在网站的代码中,他们硬编码13作为端口)。

I noticed that the client could only connect if the server was running on port 13 but interestingly enough nothing on the client said what port the server was on. 我注意到,如果服务器在端口13上运行,客户端只能连接,但有趣的是客户端上没有说服务器所在的端口。

Can anyone explain to me how this program knows what port the server is running on and why it only works for port 13? 任何人都可以向我解释这个程序如何知道服务器运行的端口以及为什么它只适用于端口13? Here's the code for the server http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/tutorial/tutdaytime2/src.html 这是服务器的代码http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/tutorial/tutdaytime2/src.html

//
// client.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

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

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: client <host>" << std::endl;
      return 1;
    }

    boost::asio::io_service io_service;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(argv[1], "daytime");
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
    tcp::resolver::iterator end;

    tcp::socket socket(io_service);
    boost::system::error_code error = boost::asio::error::host_not_found;
    while (error && endpoint_iterator != end)
    {
      socket.close();
      socket.connect(*endpoint_iterator++, error);
    }
    if (error)
      throw boost::system::system_error(error);

    for (;;)
    {
      boost::array<char, 128> buf;
      boost::system::error_code error;

      size_t len = socket.read_some(boost::asio::buffer(buf), error);

      if (error == boost::asio::error::eof)
        break; // Connection closed cleanly by peer.
      else if (error)
        throw boost::system::system_error(error); // Some other error.

      std::cout.write(buf.data(), len);
    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

interestingly enough nothing on the client said what port the server was on 有趣的是,客户端没有说服务器端口是什么

server port in client is hardcoded here: 客户端中的服务器端口是硬编码的:

tcp::resolver::query query(argv[1], "daytime");

the key is "daytime". 关键是“白天”。 it's a standard protocol and has its standard port number 13 是标准协议 ,标准端口号为13

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

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