Jump to content
Sign in to follow this  
Iczer

Disable TCP connection in the CLOSE-WAIT state

Recommended Posts

Iczer

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 ****
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

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

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

If _DeleteTCPEntry(@IPAddress1, 4561, $sFeedIP, 80) Then
   ConsoleWrite("Success"&@crlf)
Else
   ConsoleWrite("Fail"&@crlf&"@error = "&@error&@crlf)
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", "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:

Finished!
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...

 

Share this post


Link to post
Share on other sites
Iczer

well... bump...

Share this post


Link to post
Share on other sites
MikahS

maybe..

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

 

Share this post


Link to post
Share on other sites
Iczer

it makes no difference - still 87 (and why should - all work going on inside external DLLs...)

Share this post


Link to post
Share on other sites
ripdad

What is preventing you from opening another socket?


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

Share this post


Link to post
Share on other sites
Iczer

as i said in the first post - connection is opened by RSS reader. It's standalone program and it not script written by me.

Share this post


Link to post
Share on other sites
ripdad

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

Share this post


Link to post
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
Sign in to follow this  

×