不知解决没。
Const $MIB_TCP_STATE_DELETE_TCB = 12
Const $tagMIB_TCPROW = "dword State;dword LocalAddr;dword LocalPort;dword RemoteAddr;dword RemotePort"
; 删除本机1234端口 与远程主机65.55.57.252 80端口的连接。
If _DeleteTCPEntry(@IPAddress2, 1234, "65.55.57.252", 80) Then
; Succeed.
Else
; Fail.
EndIf
Func _DeleteTCPEntry($sLocalAddr, $iLocalPort, $sRemoteAddr, $iRemotePort)
Local $iResult, $tMibRow
$tMibRow = DllStructCreate($tagMIB_TCPROW)
DllStructSetData($tMibRow, "State", $MIB_TCP_STATE_DELETE_TCB)
DllStructSetData($tMibRow, "LocalAddr", _inetaddr($sLocalAddr))
DllStructSetData($tMibRow, "LocalPort", _htons($iLocalPort))
DllStructSetData($tMibRow, "RemoteAddr", _inetaddr($sRemoteAddr))
DllStructSetData($tMibRow, "RemotePort", _htons($iRemotePort))
$iResult = DllCall("iphlpapi.dll", "long", "SetTcpEntry", "ptr", DllStructGetPtr($tMibRow))
Return SetError($iResult[0], 0, $iResult[0] = 0)
EndFunc ;==>_DeleteTCPEntry
Func _inetaddr($sIPAddress)
Local $iResult
$iResult = DllCall("Ws2_32.dll", "long", "inet_addr", "str", $sIPAddress)
Return $iResult[0]
EndFunc ;==>_inetaddr
Func _htons($wPort)
Local $iResult
$iResult = DllCall("Ws2_32.dll", "word", "htons", "word", $wPort)
Return $iResult[0]
EndFunc ;==>_htons
另外,iphlpapi.GetTcpTable可以检测出所有连接和其状态,在调用_DeleteTCPEntry函数之前,先检查下连接状态是否为CLOSE_WAIT。 |