3
所以我正在瀏覽asio教程,我編譯了同步的白天客戶端和同步的白天服務器。我正在玩弄服務器端的代碼,通過改變端口(在站點的代碼中,他們硬編碼爲13端口),通過命令行傳入。boost-asio如何知道服務器在哪個端口上運行?
我注意到客戶端只能在服務器在端口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;
}
所以如果你用「echo」或「http」替換「daytime」,它會查找通常與echo或http服務器相關的端口? – ThatQuestionGuy 2011-02-13 00:26:31