単純なサーバー/クライアント ブーストの例が機能しない
-
23-08-2019 - |
質問
学習ブーストを強化し、昼間のサーバー クライアントをコンパイルしました 例. 。例にあるポート 13 は使用できないため、サーバーとクライアントの例ではポート番号のみを変更しました。サーバーは正常に動作しますが、クライアントが接続していないようで、エラーは表示されません。
クライアントの入力データは「127.0.0.1」です。
サーバ:
#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::endpoint endpoint(tcp::v4(), 8087);
tcp::acceptor acceptor(io_service, endpoint);
for (;;)
{
tcp::iostream stream;
acceptor.accept(*stream.rdbuf());
stream << "test" << make_daytime_string();
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
そしてクライアントはこう言いました。
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: daytime_client <host>" << std::endl;
return 1;
}
tcp::iostream s(argv[1], 8087);
std::string line;
std::getline(s, line);
std::cout << line << std::endl;
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
}
return 0;
}
解決
これをデバッグするには、次のことが役立ちます。
- どのプラットフォームを実行していますか
- 使用しているコンパイラ (バージョンを含む)
- 使用しているブーストのバージョン
また、サーバーが 127.0.0.1 にバインドされているか、外部インターフェイスにバインドされているかを確認する必要があります。127.0.0.1 の代わりに外部インターフェイスの IP アドレスを使用してみてください。Windows では ipconfig を使用し、Linux では ifconfig を使用してこれを確認します。
他のヒント
どのような私のために働いたことは、私はからエンドポイントを作成する方法を変更しました。
tcp::endpoint( tcp::v4(), port );
タグに
tcp::endpoint( boost::asio::ip::address::from_string("127.0.0.1"), port );
最初の方法は、Mac OS X上で正常に動作0.0.0.0のエンドポイントを作成しますが、Windows上で「有効でない」メッセージ(XP、MSVC 2008付の建物)を与えます。
私がなぜ違いを知って気にしないだろうが、少なくともそれが動作します。
うーん、1_36ブーストバージョンとMSVC 2005 compiller上のすべての作品。
ファイアウォールの設定を確認してください。
ポートオプションは、「昼」として、サービスの名前かもしれ文字列を取り、そしてそれは、対応するポート、または明示的にポートを検索しますが、それは文字列である必要があります:
TCP ::入出力ストリームS(ARGV [1]、 "8087");