VB projects - Netstat 2000
Description: Shows network usage information, including active TCP and UDP connections
Minimum requirements: VB6, Windows 2000 (or Windows 98/NT4 with platform SDK installed)
Download: source code
Screenshot:

Project: Standard EXE
Controls: cmdUDP (CommandButton), cmdTCP (CommandButton), cmdStat (CommandButton), _
txtOutput (TextBox)
Code:
Dim ip As MIB_IPSTATS
Dim tcp As MIB_TCPSTATS
Dim udp As MIB_UDPSTATS
Dim icmp As MIBICMPINFO
Private Sub cmdStat_Click()
txtOutput = ""
If GetIpStatistics(ip) <> 0 Then
txtOutput = txtOutput & "Unable to retrieve IP Statistics"
Else
txtOutput = txtOutput & "IP Statistics" & vbCrLf & String(30, "=") & vbCrLf
txtOutput = txtOutput & "IP forwarding enabled or disabled:" & vbTab & ip.dwForwarding & vbCrLf
txtOutput = txtOutput & "default time-to-live:" & vbTab & ip.dwDefaultTTL & vbCrLf
txtOutput = txtOutput & "datagrams received:" & vbTab & ip.dwInReceives & vbCrLf
txtOutput = txtOutput & "received header errors:" & vbTab & ip.dwInHdrErrors & vbCrLf
txtOutput = txtOutput & "received address errors:" & vbTab & ip.dwInAddrErrors & vbCrLf
txtOutput = txtOutput & "datagrams forwarded:" & vbTab & ip.dwForwDatagrams & vbCrLf
txtOutput = txtOutput & "datagrams with unknown protocol:" & vbTab & ip.dwInUnknownProtos & vbCrLf
txtOutput = txtOutput & "received datagrams discarded:" & vbTab & ip.dwInDiscards & vbCrLf
txtOutput = txtOutput & "received datagrams delivered:" & vbTab & ip.dwInDelivers & vbCrLf
txtOutput = txtOutput & "outgoing datagrams requested:" & vbTab & ip.dwOutRequests & vbCrLf
txtOutput = txtOutput & "outgoing datagrams discarded:" & vbTab & ip.dwRoutingDiscards & vbCrLf
txtOutput = txtOutput & "sent datagrams discarded:" & vbTab & ip.dwOutDiscards & vbCrLf
txtOutput = txtOutput & "datagrams for which no route:" & vbTab & ip.dwOutNoRoutes & vbCrLf
txtOutput = txtOutput & "datagrams for which all frags didn't arrive:" & vbTab & ip.dwReasmTimeout & vbCrLf
txtOutput = txtOutput & "datagrams requiring reassembly:" & vbTab & ip.dwReasmReqds & vbCrLf
txtOutput = txtOutput & "successful reassemblies:" & vbTab & ip.dwReasmOks & vbCrLf
txtOutput = txtOutput & "failed reassemblies:" & vbTab & ip.dwReasmFails & vbCrLf
txtOutput = txtOutput & "successful fragmentations:" & vbTab & ip.dwFragOks & vbCrLf
txtOutput = txtOutput & "failed fragmentations:" & vbTab & ip.dwFragFails & vbCrLf
txtOutput = txtOutput & "datagrams fragmented:" & vbTab & ip.dwFragCreates & vbCrLf
txtOutput = txtOutput & "number of interfaces on computer:" & vbTab & ip.dwNumIf & vbCrLf
txtOutput = txtOutput & "number of IP address on computer:" & vbTab & ip.dwNumAddr & vbCrLf
txtOutput = txtOutput & "number of routes in routing table:" & vbTab & ip.dwNumRoutes & vbCrLf
txtOutput = txtOutput & vbCrLf
End If
If GetTcpStatistics(tcp) <> 0 Then
txtOutput = txtOutput & "Unable to retrieve TCP Statistics"
Else
txtOutput = txtOutput & "TCP Statistics" & vbCrLf & String(30, "=") & vbCrLf
txtOutput = txtOutput & "timeout algorithm:" & vbTab & tcp.dwRtoAlgorithm & vbCrLf
txtOutput = txtOutput & "minimum timeout:" & vbTab & tcp.dwRtoMin & vbCrLf
txtOutput = txtOutput & "maximum timeout:" & vbTab & tcp.dwRtoMax & vbCrLf
txtOutput = txtOutput & "maximum connections:" & vbTab & tcp.dwMaxConn & vbCrLf
txtOutput = txtOutput & "active opens:" & vbTab & tcp.dwActiveOpens & vbCrLf
txtOutput = txtOutput & "passive opens:" & vbTab & tcp.dwPassiveOpens & vbCrLf
txtOutput = txtOutput & "failed attempts:" & vbTab & tcp.dwAttemptFails & vbCrLf
txtOutput = txtOutput & "establised connections reset:" & vbTab & tcp.dwEstabResets & vbCrLf
txtOutput = txtOutput & "established connections:" & vbTab & tcp.dwCurrEstab & vbCrLf
txtOutput = txtOutput & "segments received:" & vbTab & tcp.dwInSegs & vbCrLf
txtOutput = txtOutput & "segment sent:" & vbTab & tcp.dwOutSegs & vbCrLf
txtOutput = txtOutput & "segments retransmitted:" & vbTab & tcp.dwRetransSegs & vbCrLf
txtOutput = txtOutput & "incoming errors:" & vbTab & tcp.dwInErrs & vbCrLf
txtOutput = txtOutput & "outgoing resets:" & vbTab & tcp.dwOutRsts & vbCrLf
txtOutput = txtOutput & "cumulative connections:" & vbTab & tcp.dwNumConns & vbCrLf
txtOutput = txtOutput & vbCrLf
End If
If GetUdpStatistics(udp) <> 0 Then
txtOutput = txtOutput & "Unable to retrieve UDP Statistics"
Else
txtOutput = txtOutput & "UDP Statistics" & vbCrLf & String(30, "=") & vbCrLf
txtOutput = txtOutput & "received datagrams:" & vbTab & udp.dwInDatagrams & vbCrLf
txtOutput = txtOutput & "datagrams for which no port:" & vbTab & udp.dwNoPorts & vbCrLf
txtOutput = txtOutput & "errors on received datagrams:" & vbTab & udp.dwInErrors & vbCrLf
txtOutput = txtOutput & "sent datagrams:" & vbTab & udp.dwOutDatagrams & vbCrLf
txtOutput = txtOutput & "number of entries in UDP listener table:" & vbTab & udp.dwNumAddrs & vbCrLf
txtOutput = txtOutput & vbCrLf
End If
If GetIcmpStatistics(icmp) <> 0 Then
txtOutput = txtOutput & "Unable to retrieve ICMP Statistics"
Else
txtOutput = txtOutput & "ICMP Statistics" & vbCrLf & String(30, "=") & vbCrLf
txtOutput = txtOutput & "***** In *****" & vbCrLf
txtOutput = txtOutput & "number of messages:" & vbTab & icmp.icmpInStats.dwMsgs & vbCrLf
txtOutput = txtOutput & "number of errors:" & vbTab & icmp.icmpInStats.dwErrors & vbCrLf
txtOutput = txtOutput & "destination unreachable messages:" & vbTab & icmp.icmpInStats.dwDestUnreachs & vbCrLf
txtOutput = txtOutput & "time-to-live exceeded messages:" & vbTab & icmp.icmpInStats.dwTimeExcds & vbCrLf
txtOutput = txtOutput & "parameter problem messages:" & vbTab & icmp.icmpInStats.dwParmProbs & vbCrLf
txtOutput = txtOutput & "source quench messages:" & vbTab & icmp.icmpInStats.dwSrcQuenchs & vbCrLf
txtOutput = txtOutput & "redirection messages:" & vbTab & icmp.icmpInStats.dwRedirects & vbCrLf
txtOutput = txtOutput & "echo requests:" & vbTab & icmp.icmpInStats.dwEchos & vbCrLf
txtOutput = txtOutput & "echo replies:" & vbTab & icmp.icmpInStats.dwEchoReps & vbCrLf
txtOutput = txtOutput & "timestamp requests:" & vbTab & icmp.icmpInStats.dwTimestamps & vbCrLf
txtOutput = txtOutput & "timestamp replies:" & vbTab & icmp.icmpInStats.dwTimestampReps & vbCrLf
txtOutput = txtOutput & "address mask requests:" & vbTab & icmp.icmpInStats.dwAddrMasks & vbCrLf
txtOutput = txtOutput & "address mask replies:" & vbTab & icmp.icmpInStats.dwAddrMaskReps & vbCrLf
txtOutput = txtOutput & vbCrLf
txtOutput = txtOutput & "***** Out *****" & vbCrLf
txtOutput = txtOutput & "number of messages:" & vbTab & icmp.icmpOutStats.dwMsgs & vbCrLf
txtOutput = txtOutput & "number of errors:" & vbTab & icmp.icmpOutStats.dwErrors & vbCrLf
txtOutput = txtOutput & "destination unreachable messages:" & vbTab & icmp.icmpOutStats.dwDestUnreachs & vbCrLf
txtOutput = txtOutput & "time-to-live exceeded messages:" & vbTab & icmp.icmpOutStats.dwTimeExcds & vbCrLf
txtOutput = txtOutput & "parameter problem messages:" & vbTab & icmp.icmpOutStats.dwParmProbs & vbCrLf
txtOutput = txtOutput & "source quench messages:" & vbTab & icmp.icmpOutStats.dwSrcQuenchs & vbCrLf
txtOutput = txtOutput & "redirection messages:" & vbTab & icmp.icmpOutStats.dwRedirects & vbCrLf
txtOutput = txtOutput & "echo requests:" & vbTab & icmp.icmpOutStats.dwEchos & vbCrLf
txtOutput = txtOutput & "echo replies:" & vbTab & icmp.icmpOutStats.dwEchoReps & vbCrLf
txtOutput = txtOutput & "timestamp requests:" & vbTab & icmp.icmpOutStats.dwTimestamps & vbCrLf
txtOutput = txtOutput & "timestamp replies:" & vbTab & icmp.icmpOutStats.dwTimestampReps & vbCrLf
txtOutput = txtOutput & "address mask requests:" & vbTab & icmp.icmpOutStats.dwAddrMasks & vbCrLf
txtOutput = txtOutput & "address mask replies:" & vbTab & icmp.icmpOutStats.dwAddrMaskReps & vbCrLf
txtOutput = txtOutput & vbCrLf
End If
End Sub
Private Sub cmdTCP_Click()
Dim pTcpTable As MIB_TCPTABLE
Dim pdwSize As Long
Dim bOrder As Long
Dim nRet As Long
Dim i As Integer, s As String
txtOutput = ""
nRet = GetTcpTable(pTcpTable, pdwSize, bOrder)
nRet = GetTcpTable(pTcpTable, pdwSize, bOrder)
For i = 0 To pTcpTable.dwNumEntries - 1
If pTcpTable.table(i).dwState - 1 <> MIB_TCP_STATE_LISTEN Then
txtOutput = txtOutput & c_ip(pTcpTable.table(i).dwLocalAddr) & ":" & _
c_port(pTcpTable.table(i).dwLocalPort) & vbTab & _
c_ip(pTcpTable.table(i).dwRemoteAddr) & ":" & _
c_port(pTcpTable.table(i).dwRemotePort) & vbTab & _
c_state(pTcpTable.table(i).dwState - 1) & vbCrLf
Else
txtOutput = txtOutput & c_ip(pTcpTable.table(i).dwLocalAddr) & ":" & _
c_port(pTcpTable.table(i).dwLocalPort) & vbTab & _
c_ip(pTcpTable.table(i).dwRemoteAddr) & ":0" & _
vbTab & _
c_state(pTcpTable.table(i).dwState - 1) & vbCrLf
End If
Next
End Sub
Private Sub cmdUDP_Click()
Dim pUdpTable As MIB_UDPTABLE
Dim i As Integer
Dim pdwSize As Long
Dim bOrder As Long
Dim nRet As Long
txtOutput = ""
nRet = GetUdpTable(pUdpTable, pdwSize, bOrder)
nRet = GetUdpTable(pUdpTable, pdwSize, bOrder)
For i = 0 To pUdpTable.dwNumEntries - 1
txtOutput = txtOutput & c_ip(pUdpTable.table(i).dwLocalAddr) & " : " & _
c_port(pUdpTable.table(i).dwLocalPort) & vbCrLf
Next
End Sub
Private Sub Form_Load()
cmdStat_Click
End Sub
API Declarations (Module1.bas):
'================= TCP things ====================
'state of the connection
Public Const MIB_TCP_STATE_CLOSED = 0
Public Const MIB_TCP_STATE_LISTEN = 1
Public Const MIB_TCP_STATE_SYN_SENT = 2
Public Const MIB_TCP_STATE_SYN_RCVD = 3
Public Const MIB_TCP_STATE_ESTAB = 4
Public Const MIB_TCP_STATE_FIN_WAIT1 = 5
Public Const MIB_TCP_STATE_FIN_WAIT2 = 6
Public Const MIB_TCP_STATE_CLOSE_WAIT = 7
Public Const MIB_TCP_STATE_CLOSING = 8
Public Const MIB_TCP_STATE_LAST_ACK = 9
Public Const MIB_TCP_STATE_TIME_WAIT = 10
Public Const MIB_TCP_STATE_DELETE_TCB = 11
Type MIB_TCPROW
dwState As Long 'state of the connection
dwLocalAddr As String * 4 'address on local computer
dwLocalPort As String * 4 'port number on local computer
dwRemoteAddr As String * 4 'address on remote computer
dwRemotePort As String * 4 'port number on remote computer
End Type
Type MIB_TCPTABLE
dwNumEntries As Long 'number of entries in the table
table(100) As MIB_TCPROW 'array of TCP connections
End Type
Declare Function GetTcpTable Lib "IPhlpAPI" _
(pTcpTable As MIB_TCPTABLE, pdwSize As Long, bOrder As Long) As Long
'================= UDP things ====================
Type MIB_UDPROW
dwLocalAddr As String * 4 'address on local computer
dwLocalPort As String * 4 'port number on local computer
End Type
Type MIB_UDPTABLE
dwNumEntries As Long 'number of entries in the table
table(100) As MIB_UDPROW 'table of MIB_UDPROW structs
End Type
Declare Function GetUdpTable Lib "IPhlpAPI" _
(pUdpTable As MIB_UDPTABLE, pdwSize As Long, bOrder As Long) As Long
'================= Statistics ====================
Type MIB_IPSTATS
dwForwarding As Long ' IP forwarding enabled or disabled
dwDefaultTTL As Long ' default time-to-live
dwInReceives As Long ' datagrams received
dwInHdrErrors As Long ' received header errors
dwInAddrErrors As Long ' received address errors
dwForwDatagrams As Long ' datagrams forwarded
dwInUnknownProtos As Long ' datagrams with unknown protocol
dwInDiscards As Long ' received datagrams discarded
dwInDelivers As Long ' received datagrams delivered
dwOutRequests As Long '
dwRoutingDiscards As Long '
dwOutDiscards As Long ' sent datagrams discarded
dwOutNoRoutes As Long ' datagrams for which no route
dwReasmTimeout As Long ' datagrams for which all frags didn't arrive
dwReasmReqds As Long ' datagrams requiring reassembly
dwReasmOks As Long ' successful reassemblies
dwReasmFails As Long ' failed reassemblies
dwFragOks As Long ' successful fragmentations
dwFragFails As Long ' failed fragmentations
dwFragCreates As Long ' datagrams fragmented
dwNumIf As Long ' number of interfaces on computer
dwNumAddr As Long ' number of IP address on computer
dwNumRoutes As Long ' number of routes in routing table
End Type
Declare Function GetIpStatistics Lib "IPhlpAPI" _
(pStats As MIB_IPSTATS) As Long
Type MIBICMPSTATS
dwMsgs As Long ' number of messages
dwErrors As Long ' number of errors
dwDestUnreachs As Long ' destination unreachable messages
dwTimeExcds As Long ' time-to-live exceeded messages
dwParmProbs As Long ' parameter problem messages
dwSrcQuenchs As Long ' source quench messages
dwRedirects As Long ' redirection messages
dwEchos As Long ' echo requests
dwEchoReps As Long ' echo replies
dwTimestamps As Long ' timestamp requests
dwTimestampReps As Long ' timestamp replies
dwAddrMasks As Long ' address mask requests
dwAddrMaskReps As Long ' address mask replies
End Type
Type MIBICMPINFO
icmpInStats As MIBICMPSTATS ' stats for incoming messages
icmpOutStats As MIBICMPSTATS ' stats for outgoing messages
End Type
Declare Function GetIcmpStatistics Lib "IPhlpAPI" _
(pStats As MIBICMPINFO) As Long
Type MIB_TCPSTATS
dwRtoAlgorithm As Long ' timeout algorithm
dwRtoMin As Long ' minimum timeout
dwRtoMax As Long ' maximum timeout
dwMaxConn As Long ' maximum connections
dwActiveOpens As Long ' active opens
dwPassiveOpens As Long ' passive opens
dwAttemptFails As Long ' failed attempts
dwEstabResets As Long ' establised connections reset
dwCurrEstab As Long ' established connections
dwInSegs As Long ' segments received
dwOutSegs As Long ' segment sent
dwRetransSegs As Long ' segments retransmitted
dwInErrs As Long ' incoming errors
dwOutRsts As Long ' outgoing resets
dwNumConns As Long ' cumulative connections
End Type
Declare Function GetTcpStatistics Lib "IPhlpAPI" _
(pStats As MIB_TCPSTATS) As Long
Type MIB_UDPSTATS
dwInDatagrams As Long ' received datagrams
dwNoPorts As Long ' datagrams for which no port
dwInErrors As Long ' errors on received datagrams
dwOutDatagrams As Long ' sent datagrams
dwNumAddrs As Long ' number of entries in UDP listener table
End Type
Declare Function GetUdpStatistics Lib "IPhlpAPI" _
(pStats As MIB_UDPSTATS) As Long
'================= Conversion ====================
Function c_port(s) As Long
c_port = Asc(Mid(s, 1, 1)) * 256 + Asc(Mid(s, 2, 1))
End Function
Function c_ip(s) As String
c_ip = Asc(Mid(s, 1, 1)) & "." & Asc(Mid(s, 2, 1)) & "." & Asc(Mid(s, 3, 1)) & "." & Asc(Mid(s, 4, 1))
End Function
Function c_state(s) As String
Select Case s
Case MIB_TCP_STATE_CLOSED: c_state = "CLOSED"
Case MIB_TCP_STATE_LISTEN: c_state = "LISTEN"
Case MIB_TCP_STATE_SYN_SENT: c_state = "SYN_SENT"
Case MIB_TCP_STATE_SYN_RCVD: c_state = "SYN_RCVD"
Case MIB_TCP_STATE_ESTAB: c_state = "ESTAB"
Case MIB_TCP_STATE_FIN_WAIT1: c_state = "FIN_WAIT1"
Case MIB_TCP_STATE_FIN_WAIT2: c_state = "FIN_WAIT2"
Case MIB_TCP_STATE_CLOSE_WAIT: c_state = "CLOSE_WAIT"
Case MIB_TCP_STATE_CLOSING: c_state = "CLOSING"
Case MIB_TCP_STATE_LAST_ACK: c_state = "LAST_ACK"
Case MIB_TCP_STATE_TIME_WAIT: c_state = "TIME_WAIT"
Case MIB_TCP_STATE_DELETE_TCB: c_state = "DELETE_TCB"
Case Else: c_state = "UNDEFINED"
End Select
End Function
|