Jump to content

TCPTimeout question


Recommended Posts

Here the repeatable exemple :)

AutoItSetOption ("TCPTimeout", 50)

;Global $IPAddress="127.0.0.1" ;ok
;Global $IPAddress="0.0.0.0" ;ok
;Global $IPAddress="31.197.115.250"; ok
Global $IPAddress="31.19.115.250"; ko


   TCPStartup()


While 1

      Ping ( $IPAddress , 50 )
          If @error Then

         MsgBox(0,"PING",@error&" on "& $IPAddress,0)

      Else
         MsgBox(0,"PING","GOOD" &" on "& $IPAddress,0)
      EndIf




      $iSocket = TCPConnect($IPAddress, "23")
          If @error Then

         MsgBox(0,"CONNECTION",@error&" on "& $IPAddress,0)

      Else
         MsgBox(0,"CONNECTION","GOOD" &" on "& $IPAddress,0)
      EndIf


   TCPCloseSocket($iSocket)

WEnd


TCPShutdown ( )
Edited by MatteoGuallini
Link to comment
Share on other sites

try with this

; #FUNCTION# ====================================================================================================================
; Name...........: _TCPConnect
; Description ...: Triess to establishes a TCP-connection in a specified time limit
; Syntax.........: _TCPConnect($sIPAddr, $iPort, $iTimeOut = -1)
; Parameters ....: $sIpAddr - IP address to connect to (IPv4)
; $iPort - Port to use
; $iTimeOut - Timeout for connection in milliseconds (default: -1)
; |Values < 0: default timeout
; |Values 0, Keyword Default: use time from Opt("TCPTimeout")
; |Values > 0: timeout in milliseconds
; Return values .: Success - Socket to use with TCP-functions
; Failure - -1, sets @error
; |1 - $sIpAddr incorrect
; |2 - could not get port
; |3 - could not create socket
; |4 - could not connect
; |5 - could not get WSAError
; |and errors from WSAGetLastError
; Author ........: ProgAndy
; Modified.......: JScript
; Remarks .......:
; Related .......: TCPConnect, TCPCloseSocket, TCPSend, TCPRecv
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _TCPConnect($sIPAddr, $iPort, $iTimeOut = -1)
Local $hWs2 = DllOpen("Ws2_32.dll")
Local $iDllErr, $fError = False, $aRes
Local $hSock = DllCall($hWs2, "uint", "socket", "int", 2, "int", 1, "int", 6)
If @error Then
$iDllErr = 3
ElseIf $hSock[0] = 4294967295 Or $hSock[0] = -1 Then
$fError = True
Else
$hSock = $hSock[0]
$aRes = DllCall($hWs2, "ulong", "inet_addr", "str", $sIPAddr)
If @error Or $aRes[0] = -1 Or $aRes[0] = 4294967295 Then
$iDllErr = 1
Else
$iPort = DllCall($hWs2, "ushort", "htons", "ushort", $iPort)
If @error Then
$iDllErr = 2
Else
$iPort = $iPort[0]
EndIf
EndIf
If 0 = $iDllErr Then
Local $tSockAddr = DllStructCreate("short sin_family;ushort sin_port; ulong sin_addr;char sin_zero[8];")
DllStructSetData($tSockAddr, 1, 2)
DllStructSetData($tSockAddr, 2, $iPort)
DllStructSetData($tSockAddr, 3, $aRes[0])

If IsKeyword($iTimeOut) Or $iTimeOut = 0 Then $iTimeOut = Opt("TCPTimeout")

If $iTimeOut > -1 Then DllCall($hWs2, "int", "ioctlsocket", "int", $hSock, "long", 0x8004667e, "uint*", 1)
$aRes = DllCall($hWs2, "int", "connect", "int", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr))

Select
Case @error
$iDllErr = 4
Case $aRes[0] <> 0
$aRes = DllCall($hWs2, "int", "WSAGetLastError")
If Not @error And $aRes[0] = 10035 Then ContinueCase
$fError = True
Case $iTimeOut > -1
If IsKeyword($iTimeOut) Or $iTimeOut = 0 Then $iTimeOut = Opt("TCPTimeout")
Local $t = DllStructCreate("uint;int")
DllStructSetData($t, 1, 1)
DllStructSetData($t, 2, $hSock)
Local $to = DllStructCreate("long;long")
DllStructSetData($to, 1, Floor($iTimeOut / 1000))
DllStructSetData($to, 2, Mod($iTimeOut, 1000))
$aRes = DllCall($hWs2, "int", "select", "int", $hSock, "ptr", DllStructGetPtr($t), "ptr", DllStructGetPtr($t), "ptr", 0, "ptr", DllStructGetPtr($to))
If Not @error And $aRes[0] = 0 Then
$aRes = DllCall($hWs2, "int", "WSAGetLastError")
If Not @error And $aRes[0] = 0 Then
$iDllErr = 10060
Else
$fError = True
EndIf
Else
DllCall($hWs2, "int", "ioctlsocket", "int", $hSock, "long", 0x8004667e, "uint*", 0)
EndIf
EndSelect
EndIf
EndIf
If $iDllErr Then
TCPCloseSocket($hSock)
$hSock = -1
ElseIf $fError Then
$iDllErr = DllCall($hWs2, "int", "WSAGetLastError")
If Not @error Then $iDllErr = $iDllErr[0]
If $iDllErr = 0 Then $iDllErr = 5
TCPCloseSocket($hSock)
$hSock = -1
EndIf
DllClose($hWs2)
Return SetError($iDllErr, 0, $hSock)
EndFunc ;==>_TCPConnect
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
 Share

  • Recently Browsing   0 members

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