2014-11-15 93 views
0

感謝Boost,我用C++知道更多,我有兩個問題。 第一:在面向對象編程中,「:」代表什麼?我的意思是:Boost Ping示例

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_() 

此代碼是在升壓ICMP爲例。 我的第二個問題是,爲什麼這個代碼繼續無限?

class pinger 
{ 
    public: 
     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) 
     { 
      icmp::resolver::query query(icmp::v4(), destination, ""); 
      destination_ = *resolver_.resolve(query); 
      start_send(); 
      start_receive(); 
     } 

    private: 
     void start_send() 
     { 
      std::string body("\"Hello!\" from Asio ping."); 
      // Create an ICMP header for an echo request. 
      icmp_header echo_request; 
      echo_request.type(icmp_header::echo_request); 
      echo_request.code(0); 
      echo_request.identifier(get_identifier()); 
      echo_request.sequence_number(++sequence_number_); 
      compute_checksum(echo_request, body.begin(), body.end()); 
      // Encode the request packet. 
      boost::asio::streambuf request_buffer; 
      std::ostream os(&request_buffer); 
      os << echo_request << body; 
      // Send the request. 
      time_sent_ = posix_time::microsec_clock::universal_time(); 
      socket_.send_to(request_buffer.data(), destination_); 
      // Wait up to five seconds for a reply. 
      num_replies_ = 0; 
      timer_.expires_at(time_sent_ + posix_time::seconds(5)); 
      timer_.async_wait(boost::bind(&pinger::handle_timeout, this)); 
     } 

     void handle_timeout() 
     { 
      if (num_replies_ == 0) 
      std::cout << "Request timed out" << std::endl; 

      // Requests must be sent no less than one second apart. 
      timer_.expires_at(time_sent_ + posix_time::seconds(1)); 
      timer_.async_wait(boost::bind(&pinger::start_send, this)); 
     } 

     void start_receive() 
     { 
      // Discard any data already in the buffer. 
      reply_buffer_.consume(reply_buffer_.size()); 

      // Wait for a reply. We prepare the buffer to receive up to 64KB. 
      socket_.async_receive(reply_buffer_.prepare(65536), 
      boost::bind(&pinger::handle_receive, this, _2)); 
     } 

     void handle_receive(std::size_t length) 
     { 
      // The actual number of bytes received is committed to the buffer so that we 
      // can extract it using a std::istream object. 
      reply_buffer_.commit(length); 

      // Decode the reply packet. 
      std::istream is(&reply_buffer_); 
      ipv4_header ipv4_hdr; 
      icmp_header icmp_hdr; 
      is >> ipv4_hdr >> icmp_hdr; 

      // We can receive all ICMP packets received by the host, so we need to 
      // filter out only the echo replies that match the our identifier and 
      // expected sequence number. 
      if (is && icmp_hdr.type() == icmp_header::echo_reply 
      && icmp_hdr.identifier() == get_identifier() 
      && icmp_hdr.sequence_number() == sequence_number_) 
      { 
       // If this is the first reply, interrupt the five second timeout. 
       if (num_replies_++ == 0) 
        timer_.cancel(); 

       // Print out some information about the reply packet. 
       posix_time::ptime now = posix_time::microsec_clock::universal_time(); 
       std::cout << length - ipv4_hdr.header_length() 
       << " bytes from " << ipv4_hdr.source_address() 
       << ": icmp_seq=" << icmp_hdr.sequence_number() 
       << ", ttl=" << ipv4_hdr.time_to_live() 
       << ", time=" << (now - time_sent_).total_milliseconds() << " ms" 
       << std::endl; 
      } 

      start_receive(); 
     } 

     static unsigned short get_identifier() 
     { 
      #if defined(BOOST_ASIO_WINDOWS) 
      return static_cast<unsigned short>(::GetCurrentProcessId()); 
      #else 
      return static_cast<unsigned short>(::getpid()); 
      #endif 
     } 

     icmp::resolver resolver_; 
     icmp::endpoint destination_; 
     icmp::socket socket_; 
     deadline_timer timer_; 
     unsigned short sequence_number_; 
     posix_time::ptime time_sent_; 
     boost::asio::streambuf reply_buffer_; 
     std::size_t num_replies_; 
}; 

感謝您的提前。 C++很開心

編輯: 沒有handle_receive中的start_receive(),程序就停下來。將顯示

void displayEntry(string adrIP) 
{ 
    cout<<"Little try"; 
    pinger p(io_service, adrIP.c_str()); 
    io_service.run(); 
    cout << "\rThis line won't happen :(" << endl; 
} 

int main() 
{ 
    string ipAdr = "192.168.1.1"; 

    while (1) 
    { 
     displayEntry(ipAdr); 
    } 
} 

「小試」,但「這條線將不會發生:(」不會

+0

在':'看書的時候!通常,C++不會來自滲透。 (Google [base/member initializer list](http://en.cppreference.com/w/cpp/language/initializer_list))。 – sehe

+0

@sehe好的,我曾經看過,但問題是我在'main()'中調用了這個函數,但是不要在那裏幹掉,程序就停下來。例如,在此代碼中void voidEntry(string adrIP) { \t cout <<「Little try」; \t pinger p(io_service,adrIP.c_str()); \t io_service.run(); \t COUT << 「\ rThis線不會發生:(」 << ENDL; } INT主() { \t串ipAdr = 「192.168.1.1」; \t而(1) \t { \t \t displayEntry(ipAdr);? \t} }'「小試」將被寫入,但車道「這條線將不會發生:(」確實的幫助不是任何想法,還是要謝謝你 – NorthernLight

+0

好,我現在看到您的評論已完成(請在下次編輯您的問題,這是不可讀的)。我已經更新了我的答案。 – sehe

回答

0

該計劃一直因爲在handle_receive末調用start_receive再次去,無條件地。

更新到註釋:代碼沒有達到後

io_service.run(); 

因爲IO服務永遠不會停止工作(每次安排新的異步操作)。只有當進程被中斷/終止時,它纔會停止,並且在缺少信號處理程序的情況下,您的「這條線不會發生」行將不會再被執行。

signal_set來處理例如Ctrl-C http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/signal_set.html