2009-04-04 12 views
2

ブーストを学習し、昼間サーバークライアントexampleをコンパイルしました。私はこの例ではポート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; 
} 
+0

サーバーとは別にクライアントを試しましたか? –

答えて

1

いくつかの事はあなたのためにこれをデバッグするために役立つだろう:

  1. あなたはあなたが
を使用しているブーストのバージョンは何バージョン
  • 含めて、使用しているどのようなコンパイラ
  • を何プラットフォームを実行しています

    また、サーバーが127.0.0.1にバインドしているか、外部インターフェイスにバインドしているかどうかを確認することも重要です。 127.0.0.1の代わりに外部インターフェイスのIPアドレスを使用してみてください。 ipconfigを使用してWindowsでこれをチェックし、ifconfigを使用してLinuxでこれを確認してください。

  • +0

    はい、外部インターフェイスをバインドしていました...ありがとう – Milan

    1

    うーん、1_36ブーストバージョンとMSVC 2005 compiller上のすべての作品。
    ファイアウォールの設定を確認してください。

    1

    ポートオプションは、「昼」として、サービスの名前かもしれ文字列を取り、その後、それは対応するポート、または明示的にポートを検索しますが、それは文字列でなければなりません:

    tcp :: iostream s(argv [1]、 "8087");私の仕事は何

    2

    私は

    tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), port); 
    

    第1の方法と

    tcp::endpoint(tcp::v4(), port); 
    

    からエンドポイントを作成する方法を変更したのMac OS X上で正常に動作0.0.0.0のエンドポイントを作成しますしかし、Windows(XP、MSVC 2008を使用して構築)上で「有効でない」メッセージを表示します。

    私はその違いは何か分かりませんが、少なくともそれは機能します。

    関連する問題