2014-11-13 4 views
0

도움이되는 간단한 traceroute 프로그램을 개발합니다 boost_asio. 이 example을 사용합니다. ping 대신 traceroute을 구현하기 위해이 예제를 조금 변경했습니다.소켓의 응답을 얻는 방법?

pinger(boost::asio::io_service& io_service, const char* destination) 
     : resolver_(io_service), socket_(io_service, icmp::v4()), 
     timer_(io_service), sequence_number_(0), num_replies_(0) 
{ 
    boost::asio::ip::unicast::hops option(%1%); // 1 is a param 
    socket_.set_option(option); 

    icmp::resolver::query query(icmp::v4(), destination, ""); 
    destination_ = *resolver_.resolve(query); 

    start_send(); 
    start_receive(); 
} 

문제가 있습니다. time to live이 필요한 것보다 적 으면 아무런 반응이 없습니다. 나는 이런 식으로 뭔가 얻을하고자하는 (그 문제에 대한 또는 부스트) 나는 ASIO 많은 부스트를 사용하지 않은

C:\Users\>ping 91.92.100.254 -i 2 

Pinging 91.92.100.254 with 32 bytes of data: 
Reply from 10.110.50.251: TTL expired in transit. 
Reply from 10.110.50.251: TTL expired in transit. 
Reply from 10.110.50.251: TTL expired in transit. 

답변

1

를, 그래서 TTL 만료 오류가 전송 될 핸들러하는 특정 아니지만, 이것은 당신의 handle_receive 방법에 소켓 오류를 통과

당신이 당신의 응답을 기다리는 시작

_1에게 자리 표시자를 추가 ... 바른 길에 당신을 얻을해야합니다 변화 또한

// Wait for a reply. We prepare the buffer to receive up to 64KB. 
// Note that you can use boost::asio::placeholders::error in place of _1 
// and boost::asio::bytes_transferred in place of _2. 
socket_.async_receive(reply_buffer_.prepare(65536), 
    boost::bind(&pinger::handle_receive, this, _1, _2)); 

당신의 handle_receive 헤더는

void handle_receive(const boost::system::error_code& error, std::size_t length) { 
    if (error) { 
     // Handle error 
    } 
    else { 
     // Handle normally 
    } 
} 

오류가 타임 아웃 처리기로 전달받을 수있는 오류 코드를 적용합니다. 이 경우, 당신은 거기뿐만 아니라에서 오류 코드를 확인할 수 있습니다 자세한 답변을

void handle_timeout(const boost::system::error_code& error) { 
    if (error) { 
     // Handle error 
    } 
    else { 
     // Handle normally 
    } 
} 
+0

감사 :

// Add _1 placeholder timer_.async_wait(boost::bind(&pinger::handle_timeout, this, _1)); 

마찬가지로, 당신은 당신의 핸들러에 그 인수를 추가해야합니다 – Denis