CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Attempt reconnection to winsock server

    Greetings and salutations Gurus

    I am trying (in vain) to write a routine that will continuously attempt to reconnect my client program to the server when it disconnects. Could be because the server program is not running, crashed or whatever. Here's what I've got so far:

    Code:
      ' Attempt connection to server
      Do Until Winsock1.State = sckConnected
        If Winsock1.State = sckError Or sckConnecting Then
          Winsock1.Close
          DoEvents
        End If
        
        If Winsock1.State = sckClosed Then
          Winsock1.Connect sSvrName, 4500
          DoEvents
        End If
      Loop
    This works if I put a breakpoint at the connect statement, and allow it the time to connect. I dont think the connect command has enough time to actually connect before it reaches the 'If Winsock1.State=SckError Or sckConnecting Then' line.

    Any suggestions would be received gratefully, thanks.

  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Attempt reconnection to winsock server

    First of, the First If statement will always be True, You cannot pass more than 1 value to an =, so the second sckConnecting will be evaluated by itself, and since it's not 0, this will always be True;
    It should be:
    Code:
    If Winsock1.State = sckError Or Winsock1.State = sckConnecting Then
    Secondly, the code will take only a few ms to complete, meaning it will get to the If again before it will be able to connect to the server.

    What I suggest, is to trap the Connect and Error events of the winsock control. If Connect fires, you know that the connection has been established correct, if the Error event fires, you know something went wrong
    Code:
    Private Sub Winsock1_Connect()
        ' Do stuff when connected
    End Sub
    
    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)
    
        If Winsock1.State <> sckClosed Then Winsock1.Close
        Winsock1.Connect sSrvName, 4500
        
    End Sub
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  3. #3
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Attempt reconnection to winsock server

    Thanks Cakkie... That works nicely...

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

Featured