|
发表于 2008-10-27 13:15:04
|
显示全部楼层
给你一个检测的,懒得多找了
;~ _GetTCPtable( [optional handle to "ws2_32.dll" [, optional handle to "iphlpapi.dll" ] ] )
;~
;~ Return Value
;~ Success: TCPtable[][] = 2-D array
;~ [0][0] = number of connections
;~ for connection n:
;~ [n][0] = connection state (integer)
;~ [n][1] = local IP
;~ [n][2] = local port
;~ [n][3] = remote IP
;~ [n][4] = remote port
;~ [n][5] = connection state (informative text)
;~ Failure: TCPtable[0][0] = -1
Func _GetTCPtable($WSdll = "ws2_32.dll", $IHdll = "iphlpapi.dll")
Local Const $connState[12] = ["CLOSED", "LISTENING", "SYN_SENT", "SYN_RCVD", "ESTABLISHED", "FIN_WAIT1", _
"FIN_WAIT2", "CLOSE_WAIT", "CLOSING", "LAST_ACK", "TIME_WAIT", "DELETE_TCB"]
Local $TCPtable[1][1] = [[ -1]] ; preset to "failed"
$dwSize = DllStructCreate("dword") ; for MIB_TCPTABLE buffer size
$MIB_TCPTABLE = DllStructCreate("dword") ; nominal struct initially
DllStructSetData($dwSize, 1, 0) ; force zero size
$ret = DllCall($IHdll, "int", "GetTcpTable", "ptr", DllStructGetPtr($MIB_TCPTABLE), "ptr", DllStructGetPtr($dwSize), "int", 1) ; get size
If @error Or $ret[0] <> 122 Then Return $TCPtable ; dllCall error or RC is *not* ERROR_INSUFFICIENT_BUFFER = 122
$MIB_TCPTABLE = ""
For $i = 1 To DllStructGetData($dwSize, 1) / 4 ; make to requested size of buffer
$MIB_TCPTABLE &= "dword;"
Next
$MIB_TCPTABLE = DllStructCreate(StringTrimRight($MIB_TCPTABLE, 1)) ; requested struct
DllStructSetData($dwSize, 1, DllStructGetSize($MIB_TCPTABLE)) ; recheck its size
$ret = DllCall($IHdll, "int", "GetTcpTable", "ptr", DllStructGetPtr($MIB_TCPTABLE), "ptr", DllStructGetPtr($dwSize), "int", 1) ; get data
If @error Or $ret[0] Then Return $TCPtable ; dllCall error or RC is Error
$numTCPentries = DllStructGetData($MIB_TCPTABLE, 1) ; number of entries
ReDim $TCPtable[$numTCPentries + 1][6]
For $i = 1 To $numTCPentries
$offset = ($i - 1) * 5 + 1 ; dword offset into struct
$TCPtable[$i][0] = DllStructGetData($MIB_TCPTABLE, $offset + 1) ; integer connection state
$TCPtable[$i][5] = $connState[$TCPtable[$i][0] - 1] ; connection state text
$ret = DllCall($WSdll, "str", "inet_ntoa", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 2)) ; local IP / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i][1] = $ret[0]
$ret = DllCall($WSdll, "ushort", "ntohs", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 3)) ; local port / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i][2] = $ret[0]
If $TCPtable[$i][0] <= 2 Then ; CLOSED or LISTENING state
$TCPtable[$i][3] = "0.0.0.0"
$TCPtable[$i][4] = 0
Else
$ret = DllCall($WSdll, "str", "inet_ntoa", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 4)) ; remote IP / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i][3] = $ret[0]
$ret = DllCall($WSdll, "ushort", "ntohs", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 5)) ; remote port / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i][4] = $ret[0]
EndIf
Next
$dwSize = 0
$MIB_TCPTABLE = 0
$TCPtable[0][0] = $numTCPentries ; success
Return $TCPtable
EndFunc ;==>_GetTCPtable
;~ _CloseTCPconnection( LocalIP, LocalPort, RemoteIP, RemotePort, [optional handle to "ws2_32.dll" [, optional handle to "iphlpapi.dll"]] )
;~
;~ Return Value
;~ Success: 1
;~ Failure: 0
Func _CloseTCPconnection($localIP, $localPort, $remoteIP, $remotePort, $WSdll = "ws2_32.dll", $IHdll = "iphlpapi.dll")
$MIB_TCPROW = DllStructCreate("dword;dword;dword;dword;dword") ; connection struct
DllStructSetData($MIB_TCPROW, 1, 12) ; set to DELETE_TCB state = 12
$ret = DllCall($WSdll, "uint", "inet_addr", "str", $localIP) ; local IP / translate
If Not @error Then DllStructSetData($MIB_TCPROW, 2, $ret[0])
$ret = DllCall($WSdll, "uint", "htons", "ushort", $localPort) ; local port / translate
If Not @error Then DllStructSetData($MIB_TCPROW, 3, $ret[0])
$ret = DllCall($WSdll, "uint", "inet_addr", "str", $remoteIP) ; remote IP / translate
If Not @error Then DllStructSetData($MIB_TCPROW, 4, $ret[0])
$ret = DllCall($WSdll, "uint", "htons", "ushort", $remotePort) ; remote port / translate
If Not @error Then DllStructSetData($MIB_TCPROW, 5, $ret[0])
$ret = DllCall($IHdll, "int", "SetTcpEntry", "ptr", DllStructGetPtr($MIB_TCPROW)) ; close connection
If @error Or $ret[0] Then Return 0 ; dllCall error or RC is Error
$MIB_TCPROW = 0
Return 1 ; success
EndFunc ;==>_CloseTCPconnection |
|