2011-10-27 2 views
2

서버 역할을하는 카메라에서 내부 온도와 같은 특정 정보를 요청하는 프로그램을 작성하려고합니다. 용어는 꺼져 있지만 근본적으로 의미하는 바는 HTTP get 요청을 통해 카메라와의 통신이 이루어진다는 것입니다. 이 프로그램은 기본적으로 부스트 라이브러리의 약간 수정 된 버전 async_client을 다른 경로로 전달하여 다른 값을 확인한 다음 반환 된 값을 비교하여 다른 경로로 전달한 다음 다른 값을 확인합니다."오류 : 이미 연결된 소켓에서 연결 요청이 이루어졌습니다"

코드는 라운드 올바르게 처음 작동하는 것 같다,하지만 두 번째 시도에서 그것은 handle_connect 기능 및 출력 "오류 : 연결 요청이 이미 연결된 소켓에 만들어진"에 도달 여기

은 무엇입니까 나뿐만 아니라이 소켓을 닫고 handle_connect 기능을 다시 실행 것이라고 생각 else 문에 다른 if 문에서이 부분의 코드를 복사하려

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()); 
     } 
    } 

하지만, 단지 충돌 보인다 같은 기능이 보인다 소프트웨어는 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가 처음 호출되었을 때 어떤 것도 혼란스럽게 보이지 않았고, 두 번째로는 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 변경 로그를보십시오. 나는 당신의 평결을 제거하고 부스트 - asio 태그를 추가했습니다. 질문을 해결했다면 답을 추가하십시오. –

답변

0

아마도 SO_REUSEADDR 옵션을 사용하면 setsockopt과 함께 도움이 될 수 있습니다.

관련 문제