2011-10-27 47 views
2

サーバとして機能するカメラから内部温度などの特定の情報を要求するプログラムを作成しようとしています。用語は使われていませんが、基本的には、HTTP取得要求によってカメラとの通信が実現されるということです。このプログラムは基本的にわずかに適合したバージョンasync_clientをブーストライブラリから別のパスをたどって別のパスに渡して呼び出すと、別のパスを渡す前に戻り値を比較してチェックします。"エラー:既に接続されているソケットで接続要求が行われました"

コードが正しくラウンド最初の時間を動作するようですが、2番目の試みで、それはhandle_connect機能と出力になる「エラー:接続要求がすでに接続されたソケット上で行われた」

をここでするものです関数は次のようになります。

void Async_client::handle_connect(const boost::system::error_code& err, tcp::resolver::iterator endpoint_iterator) 
    { 
     http_deadline_timer_.cancel(); 
     if (!err) 
     { 
      // The connection was successful. Send the request. 
      boost::asio::async_write(socket_, request_, boost::bind(&Async_client::handle_write_request, this, 
       boost::asio::placeholders::error)); 
      //deadline timer stuff 
      http_deadline_timer_.expires_from_now(::boost::posix_time::seconds(1)); 
      http_deadline_timer_.async_wait(::boost::bind(&Async_client::call_handle_deadline_timeout, this, boost::asio::placeholders::error)); 
     } 
     else if (endpoint_iterator != tcp::resolver::iterator()) 
     { 
      // The connection failed. Try the next endpoint in the list. 
      socket_.close(); 
      tcp::endpoint endpoint = *endpoint_iterator; 
      socket_.async_connect(endpoint, boost::bind(&Async_client::handle_connect, this, 
       boost::asio::placeholders::error, ++endpoint_iterator)); 
      //deadline timer stuff 
      http_deadline_timer_.expires_from_now(::boost::posix_time::seconds(1)); 
      http_deadline_timer_.async_wait(::boost::bind(&Async_client::call_handle_deadline_timeout, this, boost::asio::placeholders::error)); 
     } 
     else 
     { 
      LOG_WARN("Async_client.cpp: Error: " << err.message()); 
     } 
    } 

私は、else文に文は同様にそれはソケットを閉じて、再実行handle_connect機能だろうと思った場合、他のコードのこの部分をコピーしようとしたが、それだけでクラッシュしているようですソフトウェアは、endpoint = * endpoint_iterator行にあります。

socket_.close(); 
      tcp::endpoint endpoint = *endpoint_iterator; 
      socket_.async_connect(endpoint, boost::bind(&Async_client::handle_connect, this, 
       boost::asio::placeholders::error, ++endpoint_iterator)); 

誰もこの状況で何をすべきか考えていますか?ソケットを外して再接続しますか?もしそうなら、どうすればいいのですか?

+0

最初の関数の開始時にsocket_.close()を追加しました。 async_clientが使用されるたびにソケットを閉じます。ありがたいことに、これは、async_clientが初めて呼び出されたときに何かを混乱させるようには見えなかったし、2回目にはhandle_connect関数を越えるように見えたが、handle_read_status_lineまでは、response_streamを0、これは無効な応答です。プログラムの最初の実行では、response_streamは0x23f350でした。これらの問題は関連していますか?私はそれについて何ができるか考えていますか? – TheMeerkat

+0

私は "response_.consume(response_.size());"を追加して問題を解決できたと考えます。 socket_.close();の直後の最初の関数の開始時。ライン。これにより、boost :: asio :: streambufであるクリアレスポンスがクリアされます。 @Sam Miller:私の投稿について何を編集しましたか?それは本当に問題ではありませんが、もし私が私の質問に尋ねたやり方について何か間違っていたら、次回のために知っておくとよいでしょう。ありがとう。 – TheMeerkat

+0

@meerkat変更ログを見てください。あなたの評価を取り除き、boost-asioタグを追加しました。あなたの質問を解決したら、答えを加えてください。 –

答えて

0

SO_REUSEADDRオプションを使用してsetsockoptと表示される可能性があります。

関連する問題