2

I am trying to get a client which tries all 5 seconds to connect to a server which doesn't need to be online. Only if it is online it should connect. Well if the server is already online and the client starts then, the message will be sent without any problem. but if the client starts first it waits a certain time until timeout and stops trying to connect. So I am trying to get a Loop with the command:

Client = New TCPControl2(ip,64555)

I tried to do this:

Try
Client = New TCPControl2(ip, 64555)
Catch ex As Exception
MsgBox(ex.Message)
End Try

It could me in the MsgBox about Timeout, but I don't know how to do a kind of Try Until it is connected or just set up the timeout time but i don't know that either.

Private Client As TCPControl2
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
skudnu
  • 51
  • 1
  • 6

4 Answers4

1

I think what you are trying to achieve can be done with a do while loop. You can read more here: https://msdn.microsoft.com/en-us/library/eked04a7.aspx

Dim isConnected As Boolean = false
Do
   Try
        Client = New TCPControl2(ip, 64555)
        ' Condition changing here.
       if Client.IsConnected = true ' <-- example!
           ' it's connected
           isConnected=true            
       end if
   Catch ex As Exception
        MsgBox(ex.Message)
   End Try
Loop Until isConnected = true
Karlta05
  • 165
  • 10
0

Keep trying until client has connected to the server? How about using a while loop:

while(notConnected)
    Try
        Client = New TCPControl2(ip, 64555)
        notConnected= connectedState!="success"
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
end while
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
kingecg
  • 1
  • 2
  • tried is out, that doesnt work for some reason.. i dont know why but i isnt beeing executed i think. i started at first the server then the client to check if the code is even beeing executed.. which is not for some reson. – skudnu Jul 29 '15 at 08:57
0

The following code will try to connect for specified number of seconds and return TCPControl2 object if successful.

Function TryConnect(ByVal ip As String, ByVal port As Integer) As TCPControl2
    While 1 = 1
        Try
            Dim client As New TCPControl2(ip, port)
            Return client
        Catch
        End Try
        Threading.Thread.Sleep(100)
    End While
    Return Nothing
End Function

Usage:

    ' -- try to connect.. wait indefinitely
    Client = TryConnect(ip, 64555)
    If Client Is Nothing Then
        MsgBox("Unable to connect! Please check your internet connection.. blah blah.. whatever", MsgBoxStyle.Exclamation)
    Else
        ' you connected successfully.. do whatever you want to do here..
        'Client.WhaeverMethod()
    End If
Pradeep Kumar
  • 6,836
  • 4
  • 21
  • 47
  • Thanks for the answer but thats not actually what i wanted.. im looking for a piece of code that retrys until the server is online. – skudnu Jul 29 '15 at 11:16
0

A translation of the C# answer found here (Credit to @LBushkin): Cleanest way to write retry logic?

I renamed a couple of things to make it more VB friendly. I also changed it to return the first exception found if all the retries failed as was suggested in the comments:

Public Class Retry
    Public Shared Sub Invoke(action As Action, retryInterval As TimeSpan, Optional retryCount As Integer = 3)
        Invoke(Of Object)(Function()
                              action()
                              Return Nothing
                          End Function, retryInterval, retryCount)

    End Sub

    Public Shared Function Invoke(Of T)(action As Func(Of T), retryInterval As TimeSpan, Optional retryCount As Integer = 3) As T
        Dim firstException As Exception = Nothing
        For Retry = 0 To retryCount - 1
            Try
                Return action()
            Catch ex As Exception
                If firstException Is Nothing Then firstException = ex
                Threading.Thread.Sleep(retryInterval)
            End Try
        Next
        Throw firstException
    End Function

End Class

Usage:

Retry.Invoke(Sub() SomeSubThatCanFail(), TimeSpan.FromMilliseconds(25))

or

Dim i = Retry.Invoke(Function() SomeFunctionThatCanFail(), TimeSpan.FromMilliseconds(25))
Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143