Jump to content

Set a timeout for _FTP_Connect()


Recommended Posts

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

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

×
×
  • Create New...