工作原理
---- 现在一般个人上网,都是运行Windows 95/98平台,采用Modem拨号上网方式,在Windows 95/98中,配置“拨号服务器”时,拨号服务器有一个属性,就是是否记录日志文件,其默认值是“否”。对于它,平常我们可能都没太注意。我们把它改成“是”,这样,在每次上网后,Windows 95就会在其安装目录下(通常就是C盘的Windows目录),把上网连通的时间以及上网过程中发生的事件记录在ppplog.txt日志文件中。
---- ppplog.txt是一个标准的文本文件,在此文件中,上网发生的每个事件记录占有一行,格式为:
日期 时间 发生的事件
|---------|-----------|-|---------------------|
12-19-1998 21:54:31.25
- Microsoft 拨号网络适配器日志已打开。
12-19-1998 21:54:31.25 - 服务器类型是 PPP (点到点协议)。
12-19-1998 22:44:49.08 - Microsoft 拨号网络适配器日志已关闭。
– 上述内容是简体中文Windows 98的ppplog.txt文件格式,对于Windows 95,基本格式是一样的,只是事件部分是英文的。
日期 时间 发生的事件
|---------|-----------|-|---------------------|
12-15-1998 16:34:27.71 - Remote access driver log opened.
12-15-1998 16:34:27.71 - Installable CP VxD SPAP is loaded
12-15-1998 16:34:27.71 - Server type is PPP (Point to Point Protocol).
.12-15-1998 16:58:30.05
-Remote access driver log closed.
---- 对应于一次正常的上网过程,Windows 95/98必定会在ppplog.txt文件中记录下包括什么时候日志文件已打开以及什么时候日志文件已关闭的完整过程,这样,我们每次上网后读取ppplog.txt文件,把含有“日志已打开”字符串的事件记录的时间作为我们计时的起点,读取日期、时间,把含有“日志已关闭”字符串的事件记录的时间作为我们计时的结束(对于Windows 95,字符串分别取”log opened”和”log closed”),读取日期、时间,算出其时间差,以该时间差作为时长,按照市话费每三分钟算一次,每次0.24元,Internet费,每一分钟算一次,每次0.10元(我们这的收费标准),便可以算出每次的花费费用了。我们可以把每次计算的结果保存在一个数据库中,那么就可以随时查询自己的花费情况了。
具体实现
---- 下面是我的具体实现过程,程序用Delphi编程实现。
---- 1、配置拨号服务器以便记录日志文件
---- 1.1、从任务栏上选取“开始”-〉“设置”-〉“控制面板”。
---- 1.2、点击“网络”图标,出现网络配置对话框。
---- 1.3、在网络组件中选择“拨号服务器”,按“属性”按钮,出现“拨号服务器”属性对话框。
---- 1. 4、选择其“高级”选项,然后选择“记录日志文件”项,在其右边的设置值中选择 “是”。按“确定”按钮,接着系统提示需要重新启动机器,重新启动后,设置就起作用了,以后每次上网,都会把上网时间记录在日志文件ppplog.txt文件中。
---- 2、 建立一数据库internet.db
---- 数据库用来保存每次的计费数据,方便查询。
---- 利用Delphi软件包中的DataBase DeskTop程序建立
---- 数据库中包含有下列字段:
字段名 类型 说明
Begindate Date 起始日期
Begintime Time 起始时间
Enddate Date 结束日期
Endtime Time 结束时间
Timelen Number 时长
TelCost Currency 电话费用
Ispcost Currency Internet费用
---- 3、从日志文件ppplog.txt文件中读取数据
---- 为了避免重复读取数据,可以每次从ppplog.txt文件中读完数据后,把ppplog.txt中的内容复制到ppplog.bak文件中,以便想查看日志文件时可以查看,然后,把ppplog.txt文件置空。读取的数据存放到数据库中。具体程序如下:
procedure TForm1.readlogExecute(Sender: TObject);
var
logfile: TextFile;
logbak: TextFile;
Str1: string;
datestr,timestr:string;
begin
AssignFile(logbak,‘c:\windows\ppplog.bak’);
if not FileExists(‘c:\windows\ppplog.bak’) then
Rewrite(logbak)
else
Append(logbak);
if not FileExists(‘c:\windows\ppplog.txt’) then
MessageDlg(‘日志文件不存在’, mtInformation,[mbOk],0)
else begin
AssignFile(logfile, ‘c:\windows\ppplog.txt’);
Reset(logfile);
if Eof(logfile) then
begin
ShowMessage(‘日志文件已为空’);
Exit;
end;
Table1.Open;
while not Eof(logfile) do
begin
Readln(logfile, Str1); {‘log opened’作为计时的起点}
if Pos(‘log opened’,Str1)< >0 then
begin
datestr:=copy(str1,1,10);
timestr:=copy(str1,12,8);
Table1.Append;
Table1.FieldValues[‘begindate’] := StrToDate(datestr);
Table1.FieldValues[‘begintime’] := StrToTime(timestr);
Table1.Post;
end;
if Pos(‘log closed’,str1)< >0 then {‘log closed’作为计时的结束}
begin
datestr:=copy(str1,1,10);
timestr:=copy(str1,12,8);
table1.Last;
table1.Edit;
Table1.FieldValues[‘enddate’] := StrToDate(datestr);
Table1.FieldValues[‘endtime’] := StrToTime(timestr);
Table1.Post;
end;
Writeln(logbak,str1);
end;
Table1.close;
Rewrite(logfile);
CloseFile(logfile);
CloseFile(logbak);
end;
end;
---- 4、计算费用的过程
---- 在该过程中,对于Internet计费的半价问题(市话没有半价),只考虑到了晚上九点以后早上七点以前,对于节假日、星期六、星期天没有考虑,有兴趣的朋友可自我完善此程序。
procedure TForm1.calcostExecute(Sender: TObject);
var
Year, Month, Day, Hour,
Min, Sec, MSec: Word;
Year1, Month1, Day1, Hour1,
Min1, Sec1, MSec1: Word;
tlen : integer;
tcost : Real;
begin
Table1.open;
while not Table1.Eof do
begin
{计算时长,以分钟为单位}
if Table1Enddate.IsNull then
tlen:=1
else begin
DecodeDate(Table1.FieldValues [‘begindate’],year,month,day);
DecodeDate(Table1.FieldValues [‘enddate’],year1,month1,day1);
DecodeTime(Table1.FieldValues[‘begintime’],hour,min,sec,msec);
DecodeTime(Table1.FieldValues [‘endtime’],hour1,min1,sec1,msec1);
tlen:=((((((day1-day)*24+hour1) -hour)*60+min1)-min)*60+sec1)-sec;
end;
if (tlen mod 60) > 0 then
tlen:=tlen div 60 +1
else
tlen := tlen div 60;
Table1.edit;
Table1.FieldValues[‘timelen’] := tlen;
if (tlen mod 3) > 0 then {市话三分钟算一次}
tcost := (tlen div 3 + 1)0.24
else
tcost := (tlen div 3)0.24;
Table1.FieldValues[‘telcost’] := tcost;
if (hour >=21) or (hour< =7) then
Table1.FieldValues[‘ispcost’] := tlen0.05
else
Table1.FieldValues[‘ispcost’] := tlen0.1;
Table1.Next ;
end;
DbGrid1.Visible := True;
end;