Get NetworkStatistics(TCP/UDP-Traffic)

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Get NetworkStatistics(TCP/UDP-Traffic)

Post by BackupUser »

Code updated For 5.20+

Restored from previous forum. Originally posted by Rings.

time to post some stuff (Windows only),
Get more Info/statistics about you Network right here:

Code: Select all

#NO_ERROR = 0; Time-out algorithms
#MIB_TCP_RTO_OTHER = 1          ; Unknown?
#MIB_TCP_RTO_CONSTANT = 2    ; Constant
#MIB_TCP_RTO_RSRE = 3             ; MIL-STD-1778 Appendix B
#MIB_TCP_RTO_VANJ = 4             ; Van Jacobson´s Algorithm
Structure PMIB_TCPSTATS
    dwRtoAlgorithm.l 
    dwRtoMin.l 
    dwRtoMax.l 
    dwMaxConn.l 
    dwActiveOpens.l 
    dwPassiveOpens.l 
    dwAttemptFails.l 
    dwEstabResets.l 
    dwCurrEstab.l 
    dwInSegs.l 
    dwOutSegs.l 
    dwRetransSegs.l 
    dwInErrs.l 
    dwOutRsts.l 
    dwNumConns.l 
EndStructure
Structure PMIB_UDPSTATS
    dwInDatagrams.l 
    dwNoPorts.l 
    dwInErrors.l 
    dwOutDatagrams.l 
    dwNumAddrs.l 
EndStructure
Structure MIBICMPSTATS
    dwMsgs.l 
    dwErrors.l 
    dwDestUnreachs.l 
    dwTimeExcds.l 
    dwParmProbs.l 
    dwSrcQuenchs.l 
    dwRedirects.l 
    dwEchos.l 
    dwEchoReps.l 
    dwTimestamps.l 
    dwTimestampReps.l 
    dwAddrMasks.l 
    dwAddrMaskReps.l 
EndStructure
Structure MIBICMPINFO
    icmpInStats.MIBICMPSTATS
    icmpOutStats.MIBICMPSTATS
EndStructure
Structure PMIB_IPSTATS
    dwForwarding.l 
    dwDefaultTTL.l 
    dwInReceives.l 
    dwInHdrErrors.l 
    dwInAddrErrors.l 
    dwForwDatagrams.l 
    dwInUnknownProtos.l 
    dwInDiscards.l 
    dwInDelivers.l 
    dwOutRequests.l 
    dwRoutingDiscards.l 
    dwOutDiscards.l 
    dwOutNoRoutes.l 
    dwReasmTimeout.l 
    dwReasmReqds.l 
    dwReasmOks.l 
    dwReasmFails.l 
    dwFragOks.l 
    dwFragFails.l 
    dwFragCreates.l 
    dwNumIf.l 
    dwNumAddr.l 
    dwNumRoutes.l 
EndStructure


OpenConsole()
Again:

ClearConsole() 

TCP.PMIB_TCPSTATS
If GetTcpStatistics_(TCP)= #No_Error
 Print("GetTcpStatistics(TCP):")
 Select TCP\dwRtoAlgorithm
    Case #MIB_TCP_RTO_CONSTANT
     Algorithm.s = "Constant time-out"
    Case #MIB_TCP_RTO_OTHER
     Algorithm.s = "Other algorithm"
    Case #MIB_TCP_RTO_VANJ
     Algorithm.s = "Van Jacobson´s algorithm"
    Case #MIB_TCP_RTO_RSRE
     Algorithm.s = "MIL-STD-1778 Appendix B"
 EndSelect
 PrintN(Algorithm.s+" "+Str(TCP\dwRtoMin)+"/"+Str(TCP\dwRtoMax))
        
 ; If this returns -1, its variable (dynamic)
 If TCP\dwMaxConn = -1 
   Print("Dynamic ")
 Else
   Print(Str(TCP\dwMaxConn))
 EndIf
 PrintN(Str(TCP\dwActiveOpens)+"/"+Str(TCP\dwPassiveOpens)+"/"+Str(TCP\dwAttemptFails)+"/"+Str(TCP\dwEstabResets)+"/"+Str(TCP\dwCurrEstab))
 PrintN(" InSegs="+Str(TCP\dwInSegs) +" OutSegs="+Str(TCP\dwOutSegs) +" RetransSegs="+Str(TCP\dwRetransSegs))
 PrintN(" InErrs="+Str(TCP\dwInErrs)+ "OutRsts="+Str(TCP\dwOutRsts)+" NumConns="+Str(TCP\dwNumConns))
EndIf

PrintN("")

UDP.PMIB_UDPSTATS
If GetUdpStatistics_(UDP) = #NO_ERROR 
 PrintN("UDP Packets IN="+Str(UDP\dwInDatagrams)+ " OUT="+Str(UDP\dwOutDatagrams)+ " Ports="+Str(dwNoPorts)+ " ERR=" +Str(UDP\dwInErrors)+" NumAddrs="+Str(UDP\dwNumAddrs))
EndIf

PrintN("")

ICMP.MIBICMPINFO
If GetIcmpStatistics_(ICMP) = #NO_ERROR 
 PrintN("GetIcmpStatistics")
 ;In    
 Info.s=" " +Str(ICMP\icmpInStats\dwMsgs)+"/"+Str(ICMP\icmpInStats\dwErrors)+"/"+Str(ICMP\icmpInStats\dwDestUnreachs)
 Info.s=Info.s + "/" + Str(ICMP\icmpInStats\dwTimeExcds)+"/"+Str(ICMP\icmpInStats\dwParmProbs)
 Info.s=Info.s + "/" + Str(ICMP\icmpInStats\dwSrcQuenchs) + "/" + Str(ICMP\icmpInStats\dwRedirects)
 Info.s=Info.s + "/" + Str(ICMP\icmpInStats\dwEchos) + "/"+Str(ICMP\icmpInStats\dwEchoReps)
 Info.s=Info.s + "/" + Str(ICMP\icmpInStats\dwTimestamps) + "/" + Str(ICMP\icmpInStats\dwTimestampReps)
 Info.s=Info.s + "/" + Str(ICMP\icmpInStats\dwAddrMasks)+"/"+Str(ICMP\icmpInStats\dwAddrMaskReps)
 PrintN(Info.s)

 ;Out    
 Info.s=" " + Str(ICMP\icmpOutStats\dwMsgs)+"/"+Str(ICMP\icmpOutStats\dwErrors)+"/"+Str(ICMP\icmpOutStats\dwDestUnreachs)
 Info.s=Info.s + "/" + Str(ICMP\icmpOutStats\dwTimeExcds)+"/"+Str(ICMP\icmpOutStats\dwParmProbs)
 Info.s=Info.s + "/" + Str(ICMP\icmpOutStats\dwSrcQuenchs) + "/" + Str(ICMP\icmpOutStats\dwRedirects)
 Info.s=Info.s + "/" + Str(ICMP\icmpOutStats\dwEchos) + "/"+Str(ICMP\icmpOutStats\dwEchoReps)
 Info.s=Info.s + "/" + Str(ICMP\icmpOutStats\dwTimestamps) + "/" + Str(ICMP\icmpOutStats\dwTimestampReps)
 Info.s=Info.s + "/" + Str(ICMP\icmpOutStats\dwAddrMasks)+"/"+Str(ICMP\icmpOutStats\dwAddrMaskReps)
 PrintN(Info.s)

EndIf

PrintN("")

IP.PMIB_IPSTATS
If GetIpStatistics_(IP)=#NO_ERROR
 If IP\dwForwarding=1
  Info.s="Enabled"
 Else
  Info.s="Disabled"
 EndIf
 PrintN("IP-Forwarding="+Info.s)

 Info.s=""
 Info=info+" DefaultTTL="+Str(IP\dwDefaultTTL)
 Info=info+" InReceives="+Str(IP\dwInReceives)
 Info=info+" InHdrrErrors="+Str(IP\dwInHdrErrors)
 Info=info+" InAddrErrors="+Str(IP\dwInAddrErrors)
 PrintN(Info.s)
 Info.s=""
 Info=info+" ForwDatagrams ="+Str(IP\dwForwDatagrams)
 Info=info+" InUnknowProtos="+Str(IP\dwInUnknownProtos)
 Info=info+" InDiscards="+Str(IP\dwInDiscards)
 Info=info+" InDelivers="+Str(IP\dwInDelivers)
 PrintN(Info.s)
 Info.s=""
 Info=info+" OutRequests="+Str(IP\dwOutRequests)
 Info=info+" RoutingDiscards="+Str(IP\dwRoutingDiscards)
 Info=info+" OutDiscards="+Str(IP\dwOutDiscards)
 Info=info+" OutNoRoutes="+Str(IP\dwOutNoRoutes)
 PrintN(Info.s)
 Info.s="" 
 Info=info+" ReasmTimeout="+Str(IP\dwReasmTimeout)
 Info=info+" ReasmReqs="+Str(IP\dwReasmReqds)
 Info=info+" ReasmOKs="+Str(IP\dwReasmOks)
 Info=info+" ReasmFails="+Str(IP\dwReasmFails)
 PrintN(Info.s)
 Info.s="" 
 Info=info+" FragOks="+Str(IP\dwFragOks)
 Info=info+" FragFails="+Str(IP\dwFragFails)
 Info=info+" FragCreates="+Str(IP\dwFragCreates)
 PrintN(Info.s)
 Info.s="" 
 Info=info+" NumIf="+Str(IP\dwNumIf)
 Info=info+" NumAddr="+Str(IP\dwNumAddr)
 Info=info+" NumRoutes="+Str(IP\dwNumRoutes)
 PrintN(Info.s)
EndIf
PrintN("")
PrintN("Press key to abort")
String$ = Inkey() 
If String$="" 
 Delay(250)
 Goto Again
EndIf

CloseConsole()
Its a long way to the top if you wanna .....CodeGuru