Tcp/ip实验准备:一个简单的定时器——boost实现

Posted lxjshuju

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tcp/ip实验准备:一个简单的定时器——boost实现相关的知识,希望对你有一定的参考价值。

tcp/ip实验须要在指定的时间查看结果,为了实验方便,做了一个定时器。用法是:

在命令行输入:timer
输入数字之后,计时对应秒数
输入m数字之后。计时对应分钟数(支持小数分钟数)
输入q退出。
时间到了之后会有3声蜂鸣,并显示Time is up!

OK,显示一个进度条会好用一些。

程序例如以下:

timer.cpp:

//g++ timer.cpp -o timer.exe -lboost_thread-mgw48-mt-1_56 -lboost_system-mgw48-1_56 -static

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <boost/thread.hpp>
#include <boost/progress.hpp>

#ifdef WIN32
#include <fstream>
#include <unistd.h>
#define TEMPNAME "WarningTemp~~~~~~.vbs"
#endif

using namespace std;
#define MAXLEN 100

int main()
{
	printf("This is a simple timer.\n"
		   "Input sDigits to time the seconds you want.\n"
		   "Input mDigits to time the minutes you want.\n"
		   "For example input s9 to time 9 seconds and\n"
		   "input m1.5 to time 1 minutes and 30 seconds.\n"
		   "input q to quit.\n");

	char op;
	int seconds;
	double minutes;
	char beep = 7;
	char line[MAXLEN];
	int i;

#ifdef WIN32
	ofstream out;
	out.open(TEMPNAME);
	out << "MsgBox \"Time Is Up!\"";
	out.close();
#endif

	for (;;)
	{
start:
		printf(">> ");
		for (i = 0;
			 (op = getchar()) != ‘\n‘ && i < MAXLEN - 1;
			 ++i)
			line[i] = op;
		line[i] = 0;
		seconds = 0;

		for (i = 0;
			 line[i] != 0 && i < MAXLEN;
			 ++i)
		{
			op = line[i];

			if (op == ‘q‘)
				goto EndOfFile;
			if (op == ‘m‘)
			{
				sscanf(line + (++i), "%lf", &minutes);
				seconds += (int)(minutes * 60);
				while (isdigit(line[i])
						|| line[i] == ‘.‘
						|| isblank(line[i]))
					i++;
				--i;
			} else if (op == ‘s‘)
			{
				sscanf(line + (++i), "%lf", &minutes);
				seconds += minutes;
				while (isdigit(line[i])
						|| isblank(line[i]))
					i++;
				--i;
			} else if (op == ‘ ‘ || op == ‘\t‘)
			{
				continue;
			} else {
				printf("Lexical Error!!\n");
				goto start;
			}
		}
		boost::progress_display prog(seconds);

		for (i = 0; i < seconds; ++i)
		{
			boost::this_thread::sleep
				(boost::posix_time::seconds(1));
			++prog;
		}

		for (i = 0; i < 3; ++i)
			cout << beep;
		cout << "Time is up!!!\n" << endl;
#ifdef WIN32
		system("wscript " TEMPNAME);
#else
		system("gdialog --msgbox \"Time Is Up!\"");
#endif
	}
EndOfFile:
#ifdef WIN32
	system("del /s " TEMPNAME);
#endif

	return 0;
}




以上是关于Tcp/ip实验准备:一个简单的定时器——boost实现的主要内容,如果未能解决你的问题,请参考以下文章

DHCP中继服务(全程实验,可跟做)

TCP/IP详解 卷1 第二十一章 TCP的超时与重传

TCP/IP三次挥手

DHCP服务基本原理+dhcp中继实验

《TCP/IP具体解释》读书笔记(22章)-TCP的坚持定时器

《TCP/IP具体解释》读书笔记(21章)-TCP的超时与重传