tcp/ip实验需要在指定的时间查看结果,为了实验方便,做了一个定时器。使用方法是:
在命令行输入:timer 分钟数
时间到了之后会有3声蜂鸣,并显示Time is up!
程序如下:
timer.cpp:
// g++ timer.cpp -o timer.exe -lboost_system-mgw48-1_56 -lws2_32 -static #include <iostream> #include <boost/asio.hpp> #include <boost/lexical_cast.hpp> int main(int argc, char* argv[]) { if (argc < 2) { std::cout << "Usage: timer.exe minutes." << std::endl; return 1; } double minutes = boost::lexical_cast<double>(argv[1]); int sec = (int)(minutes * 60); boost::asio::io_service io; boost::asio::deadline_timer timer(io, boost::posix_time::seconds(sec)); timer.wait(); char beep = 7; for (int i = 0; i < 3; ++i) std::cout << beep; std::cout << "Time is up!!!\n"; return 0; }
原文地址:http://blog.csdn.net/pdcxs007/article/details/41113665