Jump to content
Sign in to follow this  
TrashMaxx

Analyze Network Traffic

Recommended Posts

Hello Friends.

I've tried to develop a program, that analyzes the network traffic.

It shows upload- and download-rates and quits an any desired program if the upload-rate has not been exceeding a defined treshold-value for a defined time (in this case 30 minutes).

Basically, the Program works fine, but there is one big problem:

After about 20 minutes it quits with an error message (there are not enough system ressources).

As you can see in the code below, the program uses the console command "netstat -e".

I had a look at the virtual memory in the task manager and i saw that the occupancy rate of my program rises for 4kb about every 20 seconds.

I think thats the reason for the error.

I NEED a solution for this problem.

I got the hint at another forum to use the following call to clear the virtual memory:

DllCall("Psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)

But it didn't work.

I hope you got another solution to me.

Please excuse my bad english :)

If you didn't understand anything, please ask.

This is the code:

#include <GUIConstants.au3>
#include <Constants.au3>
#include <array.au3>
#include <string.au3>
 
Opt("GUIOnEventMode", 1)

$begin1 = TimerInit(); Startpunkt Zeitmessung für Netstat
$start = TimerInit(); Startpunkt Zeitmessung für Tracking
 
$NetGUI = GUICreate("Tracking Guard v1.0", 294, 100, 197, 120, -1, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")

$Up = GUICtrlCreateLabel("SpeedUp", 8, 50, 100, 20, BitOR($SS_CENTER,$SS_SUNKEN,$WS_GROUP))
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

$Down = GUICtrlCreateLabel("SpeedDown", 176, 50, 100, 20, BitOR($SS_CENTER,$SS_SUNKEN,$WS_GROUP))
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

GUICtrlCreateLabel("Upload", 31, 20, 55, 20, $WS_GROUP)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")

GUICtrlCreateLabel("Download", 190, 20, 70, 20, $WS_GROUP)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")

$Progress1 = GUICtrlCreateProgress(8, 80, 100, 9)
$Progress2 = GUICtrlCreateProgress(176, 80, 100, 9)

GUISetState(@SW_SHOW)
GUISetOnEvent(-3, "_exit")
 
;Initialisieren
$UpStr = 0
$DownStr = 0
$time = 0
$UpStr_alt = 0
$DownStr_alt = 0
 
 
_netstat($UpStr, $DownStr); Up,Down das erste mal lesen um Verschiebungspunkte zu suchen
$Xup = $UpStr;Startpunkt setzen
$Xdown = $DownStr
 
 
While 1
        
    Sleep (500)
    
    _netstat($UpStr, $DownStr)
    
;Differenzen berechnen
    $UpStr = $UpStr - $Xup 
    $DownStr = $DownStr - $Xdown
    
    $Up_speed = $UpStr - $UpStr_alt
    $Down_speed = $DownStr - $DownStr_alt
    
    
;Zeitliche Differenz berechnen und Zeitmessung Neu starten (Netstat)
    $diff = TimerDiff($begin1)
    $begin1 = TimerInit()
    
;Zeitversatz abziehen oder draufrechnen (WICHTIG!)  
    $Up_speed = $Up_speed/($diff/1000) 
    $Down_speed = $Down_speed/($diff/1000)
            
    $Up_speed = Round(($Up_speed*8)/1024,2); *8 für Byte zu bit  /1024 für in kbit
    $Down_speed = Round(($Down_speed*8)/1024,2)
    
    $Up_speed_pro = $Up_speed*0.5           
    $Down_speed_pro = $Down_speed*0.05; 0,05                    0.00625
    
    GUICtrlSetData ($Progress1,$Up_speed_pro);Prozessbalken setzen
    GUICtrlSetData ($Progress2,$Down_speed_pro)
    
    GUICtrlSetData ($Up,$Up_speed&' kbit/s');Upload Daten setzen
    GUICtrlSetData ($Down,$Down_speed&' kbit/s')
    
    
    
;Alte Werte Speichern
    $UpStr_alt = $UpStr 
    $DownStr_alt = $DownStr
    
    
    
;Startpunkt auf 0 setzen, wenn getrackt wird
    If $Up_speed > '80' Then
        $start = TimerInit()
    EndIf
    
;Zeitliche Differenz berechnen (Tracking)
    $ende = TimerDiff($start)
    
;Tracking beenden, wenn seit >= 30min nicht getrackt wurde
    If  WinExists("DTrack") And $ende >= '1800000' And $Up_speed < '60' Then 
        WinActivate("DTrack")
        MouseMove(20, 50, 1)
        MouseClick("left")
        Send("!{F4}")
        MsgBox(0, "Tracking beendet", "Es gab seit über 30 Minuten keine Tracking-Aktivität." & @CRLF & "Das Tracking wurde beendet")
        Exit
    EndIf
    
WEnd
 
;netstat -e ausführen und wichtige Werte auslesen
Func _netstat(ByRef $UpStr, ByRef $DownStr)
    $NetStat = Run(@ComSpec & " /c netstat -e", @SystemDir, @SW_HIDE, 2)
    $LineDos = StdoutRead($NetStat, 400)
    
    If StringInStr($LineDos,'Bytes') = True Then
        $Line = _StringBetween ($LineDos,'Bytes','Unicastpakete')
        $Line = StringStripWS($Line[0], 7)
        $Lines = StringSplit($Line, " ")
        $UpStr = $Lines[2]
        $DownStr = $Lines[1]
    EndIf   
    
EndFunc  ;==>_netstat
 
;Func _Reset(ByRef $TUp, ByRef $TDwn)
;   _netstat($UpStr,$DownStr)
;   $Tup = $UpStr
;   $Tdwn = $DownStr
;EndFunc
 
Func _exit()
    Exit
EndFunc  ;==>_exit

Thank you for your help!!

Trashmaxx

Share this post


Link to post
Share on other sites

Hello,

I could not get it to crash, Trash :)

Some small things I saw:

- you could try to close the cmd in the _netstat function with

If ProcessExists($NetStat) Then
    ConsoleWrite("closing cmd"&@LF)
    ProcessClose($NetStat)
EndIf

at its end.

- I think you should compare numbers here:

"If $Up_speed > '80' Then"

will be

"If $Up_speed > 80 Then"

- In this line, the application title does not match:

"If WinExists("DTrack") And $ende >= '1800000' And $Up_speed < '60' Then"

additionally, I think you should compare numbers, not strings.

But as I said, I was not able to reproduce your original error report.

Edit: sorry for the WinExists("DTrack"), I came to the conclusion that you want to close another application.

ciao

Xandl

Edited by Xandl

Share this post


Link to post
Share on other sites

- you could try to close the cmd in the _netstat function with

If ProcessExists($NetStat) Then
    ConsoleWrite("closing cmd"&@LF)
    ProcessClose($NetStat)
EndIf
yeah, man, you did it!!! :)

it works fine, now!

thank you very much!

- I think you should compare numbers here:

"If $Up_speed > '80' Then"

will be

"If $Up_speed > 80 Then"

you're right! I changed it.

Thx a lot!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...