boost-asio 如何知道服务器正在哪个端口上运行?

发布于 2024-10-16 22:09:00 字数 2077 浏览 4 评论 0原文

因此,我浏览了 asio 教程,并编译了同步日间客户端和同步日间服务器。我通过更改通过命令行传入的端口(在站点代码中硬编码 13 作为端口)来修改服务器端的代码。

我注意到客户端只能在服务器在端口 13 上运行时才能连接,但有趣的是客户端上没有任何内容说明服务器所在的端口。

谁能向我解释一下这个程序如何知道服务器正在哪个端口上运行以及为什么它只适用于端口 13?这是服务器的代码 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;
}

So I was looking through the asio tutorials and I compiled the synchronious daytime client and the synchronious daytime server. 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.

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.

Can anyone explain to me how this program knows what port the server is running on and why it only works for port 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

//
// 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日暮斜阳 2024-10-23 22:09:00

有趣的是,上面什么都没有
客户端说服务器的端口是什么

服务器端口在这里被硬编码:

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

关键是“白天”。它是标准协议,标准端口号为 13

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文