Real time Network Monitor

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
celtic88
Messages : 309
Inscription : sam. 12/sept./2015 14:31
Localisation : Alger

Real time Network Monitor

Message par celtic88 »

Bonjour ,
comme son nom l'indique ,ce code permet d'afficher les statistiques de trafic réseau (Envoi / Recevoir ) en temps réel

Image
le code

Code : Tout sélectionner

EnableExplicit

;Coder celtic88 2015(c)
#IF_TYPE_SOFTWARE_LOOPBACK = 24

Structure MIB_IFROW Align #PB_Structure_AlignC
  wszName.w[256]
  dwIndex.l
  dwType.l
  dwMtu.l
  dwSpeed.l
  dwPhysAddrLen.l
  bPhysAddr.a[8]
  dwAdminStatus.l
  dwOperStatus.l
  dwLastChange.l
  dwInOctets.l
  dwInUcastPkts.l
  dwInNUcastPkts.l
  dwInDiscards.l
  dwInErrors.l
  dwInUnknownProtos.l
  dwOutOctets.l
  dwOutUcastPkts.l
  dwOutNUcastPkts.l
  dwOutDiscards.l
  dwOutErrors.l
  dwOutQLen.l
  dwDescrLen.l
  bDescr.a[256]
EndStructure

Structure Network_Meter_info
  DevName.s
  DevAddr.s
  DevIndex.l
EndStructure

Structure Network_Meter_Rate
  dwInOctets.l
  dwOutOctets.l
EndStructure

Global iNetwork_Meter_Rate.Network_Meter_Rate

Global NewList iNetwork_Meter_info.Network_Meter_info()
Global Window_0,Combo_0, Button_0, Button_0_Copy1, Text_0

Procedure.l  __GetNumberOfInterfaces()
  Protected GetNumberOfInterfaces.l
  GetNumberOfInterfaces_(@GetNumberOfInterfaces)
  ProcedureReturn GetNumberOfInterfaces
EndProcedure

Procedure.s _Getdisplaysize(isize.f)
  Dim  abytes.s(4):abytes(0) =" Bytes":abytes(1) =" Kb":abytes(2) =" Mb":abytes(3) =" Go":abytes(4) =" Tb"
  Protected i.b,p.q
  For i.b = 4 To 1 Step -1
    p=Pow(1024, i)
    If isize >= p
        ProcedureReturn StrF((isize / p),Bool(Mod(isize ,p)>0)) + abytes(i)
    EndIf
  Next
  ProcedureReturn Str(isize) + abytes(0)
EndProcedure

Procedure.b __GetIfEntry(iindex.l)
  Protected sMIB_IFTABLE.MIB_IFROW
  sMIB_IFTABLE\dwIndex=iindex
  GetIfEntry_(@sMIB_IFTABLE)
  Protected	iRecived.l = sMIB_IFTABLE\dwInOctets - iNetwork_Meter_Rate\dwInOctets
  Protected	iSent.l = sMIB_IFTABLE\dwOutOctets - iNetwork_Meter_Rate\dwOutOctets
  SetGadgetText(Text_0,"Down Rate : "+_Getdisplaysize(iRecived) + " \s"  + #CRLF$ + "Send Rate : "+_Getdisplaysize(iSent) + " \s" + #CRLF$ +"Down Total : "+_Getdisplaysize(iNetwork_Meter_Rate\dwInOctets)+ #CRLF$ +"Send Total : "+_Getdisplaysize(iNetwork_Meter_Rate\dwOutOctets))
  iNetwork_Meter_Rate\dwInOctets=sMIB_IFTABLE\dwInOctets
  iNetwork_Meter_Rate\dwOutOctets=sMIB_IFTABLE\dwOutOctets
EndProcedure

Procedure.b __GetIfTable()
  ClearList(iNetwork_Meter_info()) 
  Protected GetNumberOfInterfaces.l = __GetNumberOfInterfaces()
  Protected Size_MIB_IFTABLE.l=((GetNumberOfInterfaces+1)*SizeOf(MIB_IFROW))+4
  Protected tMIB_IFTABLE=AllocateMemory(Size_MIB_IFTABLE)
  GetIfTable_(tMIB_IFTABLE,@Size_MIB_IFTABLE,1)
  Protected *sMIB_IFTABLE.MIB_IFROW
  tMIB_IFTABLE+4
  Protected iAddr.s,Srh.b,n.l,zz.l
  For n=0 To GetNumberOfInterfaces-1
    *sMIB_IFTABLE=tMIB_IFTABLE+(SizeOf(MIB_IFROW)*n)
    If *sMIB_IFTABLE\dwType <> #IF_TYPE_SOFTWARE_LOOPBACK And *sMIB_IFTABLE\dwPhysAddrLen And *sMIB_IFTABLE\dwPhysAddrLen < 8
      iAddr=""
      For zz=0 To *sMIB_IFTABLE\dwPhysAddrLen-2
        iAddr +  Hex(*sMIB_IFTABLE\bPhysAddr[zz],#PB_Byte) + "-"
      Next
      iAddr +  Hex(*sMIB_IFTABLE\bPhysAddr[*sMIB_IFTABLE\dwPhysAddrLen-1],#PB_Byte)
      Srh = 0
      ForEach iNetwork_Meter_info()
        If iAddr = iNetwork_Meter_info()\DevAddr
          Srh=1 
        EndIf
      Next
      If Srh=0
        AddElement(iNetwork_Meter_info())
        iNetwork_Meter_info()\DevName = PeekS(@*sMIB_IFTABLE\bDescr,*sMIB_IFTABLE\dwDescrLen,#PB_UTF8)
        iNetwork_Meter_info()\DevAddr = iAddr
        iNetwork_Meter_info()\DevIndex = *sMIB_IFTABLE\dwIndex
      EndIf
    EndIf
  Next
  FreeMemory(tMIB_IFTABLE-4)
EndProcedure

LoadFont(0,"Arial", 18, #PB_Font_Italic)

Procedure OpenWindow_0(x = 0, y = 0, width = 310, height = 230)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Real time Network Monitor", #PB_Window_SystemMenu| #PB_Window_ScreenCentered)
  Combo_0 = ComboBoxGadget(#PB_Any, 10, 10, 290, 25)
  Button_0 = ButtonGadget(#PB_Any, 170, 45, 60, 25, "Get Device")
  Button_0_Copy1 = ButtonGadget(#PB_Any, 240, 45, 60, 25, "Start")
  Text_0 = TextGadget(#PB_Any, 10, 75, 290, 130, "")
  SetGadgetColor(Text_0, #PB_Gadget_FrontColor,RGB(255,255,255))
  SetGadgetColor(Text_0, #PB_Gadget_BackColor,RGB(0,0,0))
  SetGadgetFont(Text_0, FontID(0))
  AddWindowTimer(Window_0, 0, 1000)
  StickyWindow(Window_0, 1) 
EndProcedure

OpenWindow_0()

Procedure __WinRef()
  __GetIfTable()
  ClearGadgetItems(Combo_0)
  ForEach iNetwork_Meter_info()
    AddGadgetItem(Combo_0, -1,iNetwork_Meter_info()\DevName)
  Next
  If GetGadgetState(Combo_0)  = -1
    SetGadgetState(Combo_0, 0)
  EndIf
EndProcedure

Global event.l,Selitem.s,iindex.L=-1
Repeat
  event = WaitWindowEvent()
  Select Event
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_0 
          If iindex > -1
            SetGadgetText(Button_0_Copy1,"Start")           
            iindex=-1
          EndIf
          __WinRef()
        Case Button_0_Copy1
          If iindex > -1
            SetGadgetText(Text_0,"")
            SetGadgetText(Button_0_Copy1,"Start")           
            iindex=-1
          Else
            Selitem=GetGadgetItemText(Combo_0, GetGadgetState(Combo_0) )
            If Selitem
              SetGadgetText(Button_0_Copy1,"Stop")
              ForEach iNetwork_Meter_info()
                If iNetwork_Meter_info()\DevName=Selitem
                  iindex=iNetwork_Meter_info()\DevIndex
                EndIf
              Next    
            EndIf
          EndIf
      EndSelect
    Case  #PB_Event_Timer
      If iindex > -1
        __GetIfEntry(iindex)
      EndIf
  EndSelect
Until event = #PB_Event_CloseWindow


.....i Love Pb :)
Avatar de l’utilisateur
Micoute
Messages : 2522
Inscription : dim. 02/oct./2011 16:17
Localisation : 35520 La Mézière

Re: Real time Network Monitor

Message par Micoute »

Merci pour le partage.
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce RTX 3080 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 5.73 PB 6.00 LTS (x64)
Un homme doit être poli, mais il doit aussi être libre !
Avatar de l’utilisateur
GallyHC
Messages : 1703
Inscription : lun. 17/déc./2007 12:44

Re: Real time Network Monitor

Message par GallyHC »

Bonjour,

Merci pour ce partage, c'est bien sympa.

Cordialement,
GallyHC
Configuration : Tower: Windows 10 (Processeur: i7 "x64") (Mémoire: 16Go) (GeForce GTX 760 - 2Go) - PureBasic 5.72 (x86 et x64)
Avatar de l’utilisateur
venom
Messages : 3071
Inscription : jeu. 29/juil./2004 16:33
Localisation : Klyntar
Contact :

Re: Real time Network Monitor

Message par venom »

Merci du partage celtic :wink:






@++
Windows 10 x64, PureBasic 5.73 x86 & x64
GPU : radeon HD6370M, CPU : p6200 2.13Ghz
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: Real time Network Monitor

Message par Kwai chang caine »

Ca faisait longtemps qu'on t'avait pas vu :|
Ca fait plaisir de te relire, et comme d'habitude tu viens toujours les mains pleines de beaux cadeaux :D
Merci de ce nouveau partage 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Avatar de l’utilisateur
celtic88
Messages : 309
Inscription : sam. 12/sept./2015 14:31
Localisation : Alger

Re: Real time Network Monitor

Message par celtic88 »

Kwai chang caine a écrit :Ca faisait longtemps qu'on t'avait pas vu :|
Ca fait plaisir de te relire, et comme d'habitude tu viens toujours les mains pleines de beaux cadeaux :D
Merci de ce nouveau partage 8)
Remerci !

et content qu'il vous ai plu ;)
.....i Love Pb :)
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: Real time Network Monitor

Message par Kwai chang caine »

content qu'il vous ai plu
Il a pas des aussi belles lunettes de soleil que toi..mais il est aussi beau :D
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Répondre