Results 1 to 5 of 5

Thread: [RESOLVED] Winsock Error Dealing

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    21

    Resolved [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

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    21

    Re: Winsock Error Dealing

    Thank you so much, will give that a go this morning.

    Will report back very soon.

    Will

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    21

    Re: Winsock Error Dealing

    FANTASTIC, worked a treat.

    Thank You

  5. #5
    Addicted Member aa9gg's Avatar
    Join Date
    Apr 2009
    Location
    Chicagoland
    Posts
    184

    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
    FCC Section 97.313(a) “At all times, an amateur station must use the minimum transmitter power necessary to carry out the desired communications.”

    I'd rather run a "Killer-Watt" than a KiloWatt - QRP Rules!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width