找回密码
 加入
搜索
查看: 7541|回复: 13

[网络通信] 如何'准确'获取流量KB/S

 火.. [复制链接]
发表于 2013-4-9 14:36:03 | 显示全部楼层 |阅读模式
本帖最后由 lin6051 于 2013-4-9 16:44 编辑

测试多个流量监控软件,最准确的是金山卫士

AU3那个在局域网下检测流量还好, 但adsl拨号下测试,速度就很不准确了
而且在测速下载的时候,上传也会跳到10kb/s往上.
adsl拨号情况下,测速400kb/s,它会显示1m/s多

au3那个源码:
  1. #include <String.au3>
  2. #include <GUIConstantsEx.au3>
  3. $Form1 = GUICreate("Form1", 192, 67, 445, 376)
  4. $Label1 = GUICtrlCreateLabel("上传速度:", 24, 8, 55, 17)
  5. $Label2 = GUICtrlCreateLabel("0", 88, 8, 100, 17)
  6. $Label3 = GUICtrlCreateLabel("0", 88, 32, 100, 17)
  7. $Label4 = GUICtrlCreateLabel("下载速度:", 24, 32, 55, 17)
  8. GUISetState(@SW_SHOW)
  9. WinSetOnTop ($Form1,"",1)

  10. $_IfTable=_GetIfTable()
  11. Global $_Down=$_IfTable[0]
  12. Global $_UpDate=$_IfTable[1]
  13. AdlibRegister ("_Strat_Time",1000)
  14. While 1
  15.         $nMsg = GUIGetMsg()
  16.         Switch $nMsg
  17.                 Case $GUI_EVENT_CLOSE
  18.                         Exit

  19.         EndSwitch
  20. WEnd
  21. Func _Strat_Time()
  22.         $_IfTable=_GetIfTable()
  23.         GUICtrlSetData ($Label2, Round(($_IfTable[1]-$_UpDate)/1024,2)& " Kb")
  24.         GUICtrlSetData ($Label3, Round(($_IfTable[0]-$_Down)/1024,2) & " Kb")
  25.         Global $_Down=$_IfTable[0]
  26.         Global $_UpDate=$_IfTable[1]
  27. EndFunc

  28. Func _GetIfTable()
  29.         $_Return=DllCall("Iphlpapi.dll","long","GetIfTable","ptr",0,"ulong*",0,"int",0)
  30.         $_aBuffer=DllStructCreate("byte[" & $_Return[2] & "]")
  31.         $_Return=DllCall("Iphlpapi.dll","long","GetIfTable","ptr",DllStructGetPtr($_aBuffer),"ulong*",DllStructGetSize($_aBuffer),"int",0)
  32.         $_Number=DllStructCreate("dword",DllStructGetPtr($_aBuffer))
  33.         $_Number=DllStructGetData($_Number,1)
  34.         $_MIB_IFROW="dword" & _StringRepeat(";wchar[256];dword[5];byte[8];dword[16];byte[256]",$_Number)
  35.         $_MIB_IFTABLE=DllStructCreate($_MIB_IFROW,DllStructGetPtr($_aBuffer))
  36.         Dim $_Table[2]
  37.         For $i=2 To $_Number * 5 Step 5               
  38.                 $_Table[0]+=DllStructGetData($_MIB_IFTABLE,$i+3,4)
  39.                 $_Table[1]+=DllStructGetData($_MIB_IFTABLE,$i+3,10)
  40.         Next        
  41.         Return $_Table
  42. EndFunc
复制代码
-----------
如何获取宽带连接的流量?
怎么检测宽带连接是否已连接?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2013-4-9 16:04:00 | 显示全部楼层
是不是要 减去宽带连接的流量=本地连接的流量

貌似还没找到  宽带连接的 流量
发表于 2013-4-9 19:50:58 | 显示全部楼层
回复 1# lin6051
上传流量和下载流量好像正好反了吧?
发表于 2013-4-9 20:44:09 | 显示全部楼层
AdlibRegister 调用函数会不会创建新的线程
发表于 2013-4-9 23:02:25 | 显示全部楼层
路过,坐下来看看
发表于 2013-4-11 11:55:15 | 显示全部楼层
GetIfEntry()   可以得到网卡所有进入流量

以秒为单位周期进行统计即是速率

PS:   由于统计周期不一致,所以各个软件统计的速率有小差异是正常现象
发表于 2013-4-11 13:14:57 | 显示全部楼层
好想很强大
 楼主| 发表于 2013-4-12 21:38:34 | 显示全部楼层
本帖最后由 lin6051 于 2013-4-13 16:27 编辑

找到要调用rasapi32.dll 不知道怎么搞api函数


GetIfEntry 只能显示某网卡的下行流量,没有上行么?

貌似和GetIfTable一个样
 楼主| 发表于 2013-4-13 19:31:49 | 显示全部楼层
找到了p大的udf,RAS 拨号连接UDF - rasapi
http://www.autoitx.com/thread-17179-1-3.html

修改成这样,简单用用还成
检查adsl是否连接,若连接就 下行流量/2
  1. #include <String.au3>
  2. #include <GUIConstantsEx.au3>
  3. #Include <rasapi.au3>
  4. $Form1 = GUICreate("Form1", 192, 67, 445, 376)
  5. $Label1 = GUICtrlCreateLabel("上传速度:", 24, 8, 55, 17)
  6. $Label2 = GUICtrlCreateLabel("0", 88, 8, 100, 17)
  7. $Label3 = GUICtrlCreateLabel("0", 88, 32, 100, 17)
  8. $Label4 = GUICtrlCreateLabel("下载速度:", 24, 32, 55, 17)
  9. GUISetState(@SW_SHOW)
  10. WinSetOnTop ($Form1,"",1)

  11. $_IfTable=_GetIfTable()
  12. Global $_Down=$_IfTable[0]
  13. Global $_UpDate=$_IfTable[1]
  14. AdlibRegister ("_Strat_Time",1000)
  15. While 1
  16.         $nMsg = GUIGetMsg()
  17.         Switch $nMsg
  18.                 Case $GUI_EVENT_CLOSE
  19.                         Exit

  20.         EndSwitch
  21. WEnd
  22. Func _Strat_Time()
  23. dim $ras,$notras
  24. $ras=_RasEnumConnections()
  25. if $ras[0][0]='' then
  26. $notras=1
  27. else
  28. $notras=2
  29. endif
  30.         $_IfTable=_GetIfTable()
  31.         GUICtrlSetData ($Label2, Round(($_IfTable[1]-$_UpDate)/1024*0.96,2)& " Kb")
  32.         GUICtrlSetData ($Label3, Round(($_IfTable[0]-$_Down)/1024*0.96/$notras,2) & " Kb")
  33.         Global $_Down=$_IfTable[0]
  34.         Global $_UpDate=$_IfTable[1]
  35. EndFunc

  36. Func _GetIfTable()
  37.         $_Return=DllCall("Iphlpapi.dll","long","GetIfTable","ptr",0,"ulong*",0,"int",0)
  38.         $_aBuffer=DllStructCreate("byte[" & $_Return[2] & "]")
  39.         $_Return=DllCall("Iphlpapi.dll","long","GetIfTable","ptr",DllStructGetPtr($_aBuffer),"ulong*",DllStructGetSize($_aBuffer),"int",0)
  40.         $_Number=DllStructCreate("dword",DllStructGetPtr($_aBuffer))
  41.         $_Number=DllStructGetData($_Number,1)
  42.         $_MIB_IFROW="dword" & _StringRepeat(";wchar[256];dword[5];byte[8];dword[16];byte[256]",$_Number)
  43.         $_MIB_IFTABLE=DllStructCreate($_MIB_IFROW,DllStructGetPtr($_aBuffer))
  44.         Dim $_Table[2]
  45.         For $i=2 To $_Number * 5 Step 5               
  46.                 $_Table[0]+=DllStructGetData($_MIB_IFTABLE,$i+3,4)
  47.                 $_Table[1]+=DllStructGetData($_MIB_IFTABLE,$i+3,10)
  48.         Next        
  49.         Return $_Table
  50. EndFunc
复制代码
发表于 2013-4-13 22:07:10 | 显示全部楼层
回复 9# lin6051


    谁说的,GEtIfEntry得到的是整个网卡的所有流量,包括上下

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2013-4-14 10:38:52 | 显示全部楼层
本帖最后由 lin6051 于 2013-4-14 10:40 编辑

回复 11# Qokelate

确实可以,昨天用for next 求kb/s 上传就不变
  1. $Data1 = _WinAPI_GetIfEntry($index, $Dll)
  2. for $i=1 to 1000 step 1
  3. sleep(1000)
  4. $Data2 = _WinAPI_GetIfEntry($index, $Dll)
  5. ToolTip('下行:'&Round(($Data2[8]-$Data1[8])/1024,2)&'/上行:'&Round(($Data2[14]-$Data1[14])/1024,2),120,0)
  6. $Data1[8] = $Data2[8]
  7. $Data1[14] = $Data2[14]
  8. next
复制代码
今天用while 就可以..

  1. $Data1 = _WinAPI_GetIfEntry($index, $Dll)
  2. While 1
  3. sleep(1000)
  4. $Data2 = _WinAPI_GetIfEntry($index, $Dll)
  5. ToolTip('下行:'&Round(($Data2[8]-$Data1[8])/1024,2)&' /上行:'&Round(($Data2[14]-$Data1[14])/1024,2),120,0)
  6. $Data1[14]=$Data2[14]
  7. $Data1[8]=$Data2[8]
  8. wend
复制代码
不过GetIfEntry貌似不能获取usb和虚拟网卡?
函数是论坛找的 API监视网卡流量  lpxx的
http://www.autoitx.com/forum.php ... hlight=%C1%F7%C1%BF
 楼主| 发表于 2013-4-14 10:43:42 | 显示全部楼层
  1. ;xp sp3   ;不能获取usb,虚拟,禁用的网卡

  2. #Include <Array.au3>

  3. Opt('MustDeclareVars', 1)
  4. Global Const $MIB_IF_TYPE_OTHER = 1
  5. Global Const $MIB_IF_TYPE_ETHERNET_CSMACD = 6
  6. Global Const $MIB_IF_TYPE_ISO88025_TOKENRING = 9
  7. Global Const $MIB_IF_TYPE_PPP = 23
  8. Global Const $MIB_IF_TYPE_SOFTWARE_LOOPBACK = 24
  9. Global Const $MIB_IF_TYPE_ATM = 37
  10. Global Const $MIB_IF_TYPE_IEEE80211 = 71
  11. Global Const $MIB_IF_TYPE_TUNNEL = 131
  12. Global Const $MIB_IF_TYPE_IEEE1394 = 144

  13. Global Const $MIB_IF_ADMIN_STATUS_UP = 1
  14. Global Const $MIB_IF_ADMIN_STATUS_DOWN = 2
  15. Global Const $MIB_IF_ADMIN_STATUS_TESTING = 3

  16. Global Const $MIB_IF_OPER_STATUS_NON_OPERATIONAL = 0
  17. Global Const $MIB_IF_OPER_STATUS_UNREACHABLE = 1
  18. Global Const $MIB_IF_OPER_STATUS_DISCONNECTED = 2
  19. Global Const $MIB_IF_OPER_STATUS_CONNECTING = 3
  20. Global Const $MIB_IF_OPER_STATUS_CONNECTED = 4
  21. Global Const $MIB_IF_OPER_STATUS_OPERATIONAL = 5

  22. Global Const $tagMIB_IFROW = 'wchar Name[256];dword Index;dword Type;dword Mtu;dword Speed;dword PhysAddrLen;byte PhysAddr[8];dword AdminStatus;dword OperStatus;dword LastChange;dword InOctets;dword InUcastPkts;dword InNUcastPkts;dword InDiscards;dword InErrors;dword InUnknownProtos;dword OutOctets;dword OutUcastPkts;dword OutNUcastPkts;dword OutDiscards;dword OutErrors;dword OutQLen;dword DescrLen;char Descr[256]'

  23. Global $hParent, $hForm, $Dll, $Msg, $Combo, $Label1, $Label2, $Timer, $Data, $Index = 0, $pIndex = 0, $Adapter[1][3] = [[0]]

  24. $Dll = DllOpen('IPHlpApi.dll')

  25. dim $Data1,$Data2,$index=2      ;修改$index值
  26. $Data1 = _WinAPI_GetIfEntry($index, $Dll)

  27. While 1
  28. sleep(1000)
  29. $Data2 = _WinAPI_GetIfEntry($index, $Dll)
  30. ToolTip('下行:'&Round(($Data2[8]-$Data1[8])/1024,2)&' /上行:'&Round(($Data2[14]-$Data1[14])/1024,2),120,0)
  31. $Data1[14]=$Data2[14]
  32. $Data1[8]=$Data2[8]
  33. wend


  34. Func _WinAPI_GetIfEntry($iIndex, $Dll = -1)

  35.     Local $Ret, $Data, $Addr, $Count
  36.     Local $tMIB_IFROW

  37.     If $Dll < 0 Then
  38.         $Dll = 'IPHlpApi.dll'
  39.     EndIf

  40.     $tMIB_IFROW = DllStructCreate($tagMIB_IFROW)
  41.     DllStructSetData($tMIB_IFROW, 2, $iIndex)
  42.     $Ret = DLLCall($Dll, 'dword', 'GetIfEntry', 'ptr', DllStructGetPtr($tMIB_IFROW))
  43.     If (@error) Or ($Ret[0]) Then
  44.         Return SetError($Ret[0], 0, 0)
  45.     EndIf
  46.     Dim $Data[20]
  47.     $Data[0 ] = DllStructGetData($tMIB_IFROW, 'Name')
  48.     $Data[1 ] = $iIndex
  49.     $Data[2 ] = DllStructGetData($tMIB_IFROW, 'Type')
  50.     $Data[3 ] = DllStructGetData($tMIB_IFROW, 'Mtu')
  51.     $Data[4 ] = DllStructGetData($tMIB_IFROW, 'Speed')
  52.     $Count = DllStructGetData($tMIB_IFROW, 'PhysAddrLen')
  53.     $Addr = ''
  54.     For $i = 1 To $Count
  55.         $Addr &= Hex(DllStructGetData($tMIB_IFROW, 'PhysAddr', $i), 2) & '-'
  56.     Next
  57.     $Data[5 ] = StringTrimRight($Addr, 1)
  58.     $Data[6 ] = DllStructGetData($tMIB_IFROW, 'AdminStatus')
  59.     $Data[7 ] = DllStructGetData($tMIB_IFROW, 'OperStatus')
  60.     $Data[8 ] = DllStructGetData($tMIB_IFROW, 'InOctets')
  61.     $Data[9 ] = DllStructGetData($tMIB_IFROW, 'InUcastPkts')
  62.     $Data[10] = DllStructGetData($tMIB_IFROW, 'InNUcastPkts')
  63.     $Data[11] = DllStructGetData($tMIB_IFROW, 'InDiscards')
  64.     $Data[12] = DllStructGetData($tMIB_IFROW, 'InErrors')
  65.     $Data[13] = DllStructGetData($tMIB_IFROW, 'InUnknownProtos')
  66.     $Data[14] = DllStructGetData($tMIB_IFROW, 'OutOctets')
  67.     $Data[15] = DllStructGetData($tMIB_IFROW, 'OutUcastPkts')
  68.     $Data[16] = DllStructGetData($tMIB_IFROW, 'OutNUcastPkts')
  69.     $Data[17] = DllStructGetData($tMIB_IFROW, 'OutDiscards')
  70.     $Data[18] = DllStructGetData($tMIB_IFROW, 'OutErrors')
  71.     $Data[19] = StringLeft(DllStructGetData($tMIB_IFROW, 'Descr'), DllStructGetData($tMIB_IFROW, 'DescrLen') - 1)
  72.     Return $Data
  73. EndFunc   ;==>_WinAPI_GetIfEntry
复制代码
发表于 2013-4-14 10:46:58 | 显示全部楼层
上传流量和下载流量好像正好反了
发表于 2013-4-14 11:40:44 | 显示全部楼层
回复 12# lin6051


    XP 没测试,我截图的是Win7 x64的,理论上说虚拟网卡也是可以的, 我截图里面的VMWare就是虚拟网卡而且是禁用状态,禁用的网卡肯定没数据走的(而且我的确也没有在使用), LoopBack 是回环网卡,只有使用127.0.0.1的回环时才有数据,USB没环境,无法测试了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2026-9-22 08:49 , Processed in 0.068555 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表