[RESOLVED] Winsock Error Dealing
You chap have been very helpfull, and I have yet another request.
Oh how I wish I was good at VB. lol
Right I am using Winsock control to connect to an IP interface. Then sending a data string to that interface to login.
Now IF I was not able to connect to that IP interface, I get an error, what I would like to do, is control this. So I would like to retry the connection for say 3mins, then if it still cant connect after that, display a Error Box.
At the moment, on my From Load all I do is.
sock1.Connect
Cheers,
Will
Re: Winsock Error Dealing
Try this. Needs a timer on the form named tmrRetry with its Enabled property set to FALSE and it's interval set to 1000.
This will retry once every second for 3 minutes. You can change the RETRY_TIME_SECONDS to whatever you want, and also the timer's interval to retry less often or more often...
Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Const RETRY_TIME_SECONDS As Long = 180 '3 minutes
Private lonFirstTry As Long
'Try to connect (will fail)
Private Sub Form_Load()
With Winsock1
.Close
.RemoteHost = "www.google12345345345345345435.com"
.RemotePort = 80
.Connect
End With
End Sub
'Keep trying
Private Sub tmrRetry_Timer()
Dim lonNow As Long
lonNow = GetTickCount
'Only retry if we haven't gaven up yet (seconds passed < RETRY_TIME_SECONDS)
If (lonNow - lonFirstTry) / 1000 <= RETRY_TIME_SECONDS Then
With Winsock1
.Close
.Connect
Debug.Print "Retrying..."
End With
Else
'RETRY_TIME_SECONDS seconds has passed
tmrRetry.Enabled = False
MsgBox "Gave up! :(", vbExclamation
End If
End Sub
'Connected!
Private Sub Winsock1_Connect()
tmrRetry.Enabled = False
MsgBox "Connected", vbInformation
End Sub
'Error connecting
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Winsock1.Close
If Not tmrRetry.Enabled Then
lonFirstTry = GetTickCount
tmrRetry.Enabled = True
End If
End Sub
Re: Winsock Error Dealing
Thank you so much, will give that a go this morning.
Will report back very soon.
Will
Re: Winsock Error Dealing
FANTASTIC, worked a treat.
Thank You
Re: [RESOLVED] Winsock Error Dealing
Very timely for me!!!! I've spent the last 2 days searching the web for something like this...Works well