Jump to content

Disable TCP connection in the CLOSE-WAIT state


Recommended Posts

My RSS reader sometimes gets it connection to feed-site hung in CLOSE-WAIT state. After that the only way to make feed update again is to break this connection via firewall.

MIB_TCP_STATE_CLOSE_WAIT = 8 - The TCP connection is in the CLOSE-WAIT state waiting for a connection termination request from the local user.

so function:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Local $sFeedIP = "***.***.***.***"

Const $tagMIB_TCPROW = "dword State;dword LocalAddr;dword LocalPort;dword RemoteAddr;dword RemotePort"

If _DeleteTCPEntry(@IPAddress1, 4561, $sFeedIP, 80) Then
   ConsoleWrite("Fail"&@crlf&"@error = "&@error&@crlf)

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", "DWORD", "SetTcpEntry", "ptr", DllStructGetPtr($tMibRow))
        Return SetError($iResult[0], 0, $iResult[0] = 0)
EndFunc        ;==>_DeleteTCPEntry

Func _inetaddr($sIPAddress)
        Local $iResult
        $iResult = DllCall("Ws2_32.dll", "DWORD", "inet_addr", "str", $sIPAddress)
        ConsoleWrite("$sIPAddress = " & $sIPAddress & "/" & $iResult[0] & @CRLF)
        Return $iResult[0]
EndFunc        ;==>_inetaddr

Func _htons($wPort)
        Local $iResult
        $iResult = DllCall("Ws2_32.dll", "DWORD", "htons", "DWORD", $wPort)
        ConsoleWrite("$wPort = "  & $wPort & "/" & $iResult[0] & @CRLF)
        Return $iResult[0]
EndFunc        ;==>_htons

but it always fail with @error = 87 : Specified port is not in state to be closed down

the only info i found on this issue is:

The problem was in creating the MIB_TCPROWEX connection info!
I was using int portnumbers : must use  UInt32 instead of uint LocalPort :
returns ((dwLocalPort >> 8) & 0xff) + ((dwLocalPort & 0xff) << 8)

but still cannot make function work...


Link to comment
Share on other sites


Opt("TCPTimeout", 5000) ; 5000 milliseconds = 5 seconds
You can adjust the timeout to your own accord. Let me know if this makes any difference.

Snips & Scripts

My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ


Link to comment
Share on other sites

Well then, your RSS reader has a problem with detecting a closed socket.

Perhaps you can file a bug report with the author.

- or -

Code one yourself.

You can do a quick search for one in the Examples section.

Might help you get started.


You can close a socket before it goes into WAIT State.

But afterwards, Windows handles it through the system,

since it no longer has a socket assigned to the port.

There may be a way to do it, but I have not found any.

Not even with Tcpview, a program by SysInternals.

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...