Jump to content
Sign in to follow this  
AndyS01

Set a timeout for _FTP_Connect()

Recommended Posts

AndyS01

We have several servers and I'm writing a server status monitor. I am trying to quickly determine if I can FTP to each of our local servers. When I successfully do an _FTP_Open(), then call _FTP_Connect(), the _FTP_Connect takes around 45 seconds to time out.

I want to make the 'FTP-ability' test to take no more than 1 or 2 seconds.

Is there a way to change the _FTP_Connect() timeout? Perhaps there is another call I could use?

Here is my sample code (with my server credentials removed).

#include <FTPEx.au3>
#include <array.au3>
_Main()
Exit (0)
Func _Main()
Local $server, $uname, $pwd, $hSession, $iID, $bRet
$server = "xxxxx"
$uname = "xxxxx"
$pwd = "xxxx"
$hSession = _FTP_Open('MyFTP')
If $hSession <> 0 Then
$iID = _FTP_Connect($hSession, $server, $uname, $pwd, 1, 21)
ConsoleWrite("Can connect OK" & @CRLF)
If ($iID <> 0) Then
Else
ConsoleWrite("Connect FAILED" & @CRLF)
EndIf
_FTP_Close($hSession)
Else
ConsoleWrite("+++: Openfailed" & @CRLF)
EndIf
EndFunc ;==>_Main

Share this post


Link to post
Share on other sites
Nessie

Take a look to this post:

Hi!


My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Share this post


Link to post
Share on other sites
AndyS01

Thanks, that fixed my problem.

Here's my working test code:

#include

Const $INTERNET_OPTION_CONNECT_TIMEOUT = 2

_Main()
Exit (0)
Func _Main()
Local $server, $uname, $pwd, $hSession, $iID, $bRet
$server = "xxxxx"
$uname = "xxxxx"
$pwd = "xxxx"

$hSession = _FTP_Open('MyFTP')
If $hSession <> 0 Then

_InternetSetOptions($hSession, $INTERNET_OPTION_CONNECT_TIMEOUT, 1 * 1000)

$iID = _FTP_Connect($hSession, $server, $uname, $pwd, 1, 21)
ConsoleWrite("Can connect OK" & @CRLF)
If ($iID <> 0) Then
Else
ConsoleWrite("Connect FAILED" & @CRLF)
EndIf
_FTP_Close($hSession)
Else
ConsoleWrite("+++: Openfailed" & @CRLF)
EndIf
EndFunc ;==>_Main

Func _InternetSetOptions($hInternet, $iOption, $iValue)

Local $tBuffer = DllStructCreate('int Value')
Local $WININET_HANDLE = DllOpen('wininet.dll')

DllStructSetData($tBuffer, 'Value', $iValue)

DllCall($WININET_HANDLE, _
'int', 'InternetSetOptionW', _
'hwnd', $hInternet, _ ; Handle on which to set information.
'int', $iOption, _ ; Internet option to be set.
'ptr', DllStructGetPtr($tBuffer), _ ; Pointer to a buffer that contains the option setting.
'int', DllStructGetSize($tBuffer)) ; Size of the lpBuffer buffer.

$tBuffer = 0
If (@error) Or (_WinAPI_GetLastError() > 0) Then
Return SetError(1, 0, 0)
EndIf

Return SetError(0, 0, 1)
EndFunc ;==>_InternetSetOptions
Edited by AndyS01

Share this post


Link to post
Share on other sites
Nessie

Glad to help you ;)


My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

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  

  • Similar Content

    • Blueman
      By Blueman
      Hi all,
      I was wondering if you can help me with the function called; InetRead().
      My scripts are using this function a lot for several conditions and everything works fine!
      But sometimes when the server is a little bit buggy of simply not available my script is hanging.
      It takes about 90sec before this function returns a Timeout, when i adjust the parameter it still is hanging about 90sec.
      The following script is a example where the script is hanging for aprox. 90sec;
      ; Set Timeout to 2sec AutoItSetOption ("TCPTimeout", 2000) ; Read Website InetRead("http://www.geenverbinding.nl/",1) ; Show Msgbox before Ending Script. Msgbox(64,"","Finished")  
      The following script is a example where the script show the Msgbox pretty fast;

       
      ; Set Timeout to 2sec AutoItSetOption ("TCPTimeout", 2000) ; Read Website InetRead("http://www.google.nl/",1) ; Show Msgbox before Ending Script. Msgbox(64,"","Finished")  
      My question now is, what am i doing wrong and/or is there a other way to prevent Hanging the script?
      Thanks all
       
    • ur
      By ur
      I have automated an install process by using the below code.
       
      Func _WinWaitActivate($title,$text,$timeout=0) WinWait($title,$text,$timeout) If Not WinActive($title,$text) Then WinActivate($title,$text) WinWaitActive($title,$text,$timeout) EndFunc $win1 = "Text 1" $win2 = "Text 2" _WinWaitActivate($win1,"WARNING: This progra") Send("{ALTDOWN}n{ALTUP}");Send("{SPACE}") _WinWaitActivate($win1,"I DISAGREE and &do n") Send("{UP}n") _WinWaitActivate($win1,"Customer Information") Send("{ALTDOWN}n{ALTUP}");Send("{TAB}{TAB}n") _WinWaitActivate($win1,"Click Next to instal") Send("{ALTDOWN}n{ALTUP}");Send("n") _WinWaitActivate($win1,"This feature require") Send("{ALTDOWN}n{ALTUP}");Send("{TAB}n") _WinWaitActivate($win1,"Ready to Install the") Send("{ALTDOWN}i{ALTUP}");Send("i") _WinWaitActivate($win1,"The InstallShield Wi") Send("{ALTDOWN}f{ALTUP}");Send("f") _WinWaitActivate($win2,"You must restart you") Send("{ALTDOWN}n{ALTUP}");Send("n") Now if any new screen comes in the middle of installation, like some VC++ installation error or like that.
      The AutoIT is waiting in the background.
      Is there any way to skip the new screens with just "enter" and continue the installation??
    • Spyderco
      By Spyderco
      Hi everyone

      I'll explain what I'm trying to achieve briefly:

      I run several websites and am running pilots for a new framework on a few server.
      I have a certain script set us like a bunch of checkboxes, one of which checks a URL for it's status response set to a $ClientURL variable.
      So I have something like the following:

      Set includes
      Set variables (one of which is $ClientURL = ("www.example.com").
      Start loop
         Main script
            If $ClientURL = ("www.example.com") Then
                $ClientURL = ("www.a.com")
            If $ClientURL = ("www.a.com") Then
               $ClientURL = ("www.b.com")
            ElseIf $ClientURL = ("www.b.com") Then
               $ClientURL = ("www.c.com")
            ElseIf $ClientURL = ("www.c.com") Then
               $ClientURL = ("www.d.com")
            ElseIf $ClientURL = ("www.c.com") Then
               $ClientURL = ("www.example.com")
            Sleep until the script restarts the loop.
      End loop

      This only uses one main script and changes the variables needed to run in by the following value every time.
      My problem is this...
      Everything runs smoothly, but if a webserver takes too long to respond, the program will timeout.
      This doesn't give a certain status, so it doesn't get logged by the script so it can move on to the next one.
      It just freezes and crashes on it eventually.
      Is there a way to add a certain timeout to a line?
      Lets say something like this:
      Func A ()
      $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
      $oHTTP.Open("GET", $ClientURL, False)
      $oHTTP.Send()
      If the time it takes = >10 seconds
         Move on to the next line
      Else
         Do nothing
      EndFunc

      I thought about making a variable that tracks the system time in seconds that has passed by and resets it at the beginning of every new function,
      but I was hoping there'd be something more efficient...
      Thanks in advance guys!
    • Gibbo
      By Gibbo
      Hi All, 
      I went searching for something to speed up checking for files on the network (UNC Paths)
      Most examples were rather complicated but someones multi-threaded solution gave me an idea.
      Hope it is useful to someone.
      Seems to work well so far. 
       
      Func _FileExistsTimeout($sPath, $iTimeout = 1000) Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable. Local $iPID = Run(@AutoItExe & ' /AutoIt3ExecuteLine "ConsoleWriteError(FileExists(""' & $sPath & '""))"', @ScriptDir, @SW_HIDE, $STDERR_CHILD) Local $sOutput = "" Local $iDiff = 0 While $iDiff < $iTimeout $iDiff = TimerDiff($hTimer) $sOutput &= StderrRead($iPID) If @error Then ; Exit the loop if the process closes or StderrRead returns an error. ExitLoop EndIf WEnd ProcessClose($iPID) Switch StringStripWS($sOutput, 8) Case 1 Return True Case Else Return False EndSwitch EndFunc ;==>_FileExistsTimeout
    • GreenCan
      By GreenCan
      Edit 2: Complete rewrite of the function, simplified version using a single function and without use of GUICtrlSetOnEvent or AutoItSetOption. Correct centering of message.
      Edit 1: Updated function to correct possible misbehaving AutoItSetOption.
      MsgBox with Visual CountDown
      Self-explanatory I guess...
      GreenCan
      MsgBox_CountDown.au3 (Previous downloads: 53)
×