Results 1 to 7 of 7

Thread: Winsock won't close {RESOLVED}

  1. #1

    Thread Starter
    Lively Member vb_paladin's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    102

    Winsock won't close {RESOLVED}

    I have a listbox containing user's computer names. When I click on a name, my single winsock control connects to that user's computer fine. But when I click on a different name, I have the code
    VB Code:
    1. Winsock1.Close: DoEvents
    to close Winsock1 and enable it to connect to that user. But the event

    Private Sub Winsock1_Close()

    End Sub

    never fires and therefore Winsock1 never closes. How can I make it close??
    Last edited by vb_paladin; Jul 6th, 2003 at 10:01 AM.

  2. #2
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    I don't think that DoEvents should really be there after your Winsock1.Close.

    have a public variable holding the state of the winsock and check it before connection to another computer

    VB Code:
    1. Dim wconn As Boolean
    2.  
    3. Private Sub Winsock1_Close()
    4.      wconn = False
    5. End Sub

    then in your listbox code

    VB Code:
    1. Winsock1.Close
    2.      If wconn = False Then
    3.           wconn = True
    4.           'connect
    5.      End If
    Anticipation of death is worse than death itself

  3. #3

    Thread Starter
    Lively Member vb_paladin's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    102
    It still doesnt work. This is the code for my listbox.

    When the user clicks a different name in the listbox, sckOut should disconnect from the current user it is connected to, and reconnect to the new username that is selected.

    VB Code:
    1. Private Sub lstUsers_Click()
    2. If lstUsers.List(lstUsers.ListIndex) = strLocalName Then
    3.     Exit Sub
    4. ElseIf lstUsers.List(lstUsers.ListIndex) = strUserName Then
    5.     Exit Sub
    6. End If
    7.  
    8. lstUsers.Enabled = False
    9.  
    10. If sckOut.State = sckConnected Then
    11.     sckOut.SendData "DISCONNECTING_USER": DoEvents
    12.     sckOut.Close
    13. End If
    14.  
    15. strUserName = lstUsers.List(lstUsers.ListIndex)
    16. strUserPort = lstPorts.List(lstUsers.ListIndex)
    17. sckOut.RemoteHost = strUserName
    18. sckOut.RemotePort = strUserPort + 1
    19. sckOut.Connect
    20. End Sub

  4. #4
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068
    The Private Sub:
    VB Code:
    1. Private Sub Winsock1_Close()
    2.  
    3. End Sub
    Only fires when the connection is closed by the remote host. You can make winsock connect right after you close it in your code..

  5. #5

    Thread Starter
    Lively Member vb_paladin's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    102
    so I don't have to send a message telling the remote host to close the winsock

    VB Code:
    1. sckOut.SendData "DISCONNECTING_USER

    All I have to do is do sckOut.Close locally and then the remote host's Winsock1_Close() event will fire and disconnect, correct?

  6. #6
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068
    Should do.

  7. #7
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    Re: Winsock won't close

    Originally posted by vb_paladin
    and therefore Winsock1 never closes. How can I make it close??
    Spajeoly answered part of your question, but from the above quote, it appears that there may more to your problem.

    Do you know for a fact that Winsock1 does not close? Or was this an assumption because you didn't see the Winsock1_Close sub fire?

    If you are hardcoding a LocalPort number, the connection will remain in a TIME_WAIT mode for about 4 minutes after closing and you will not be able to connect during that time. Be sure to set LocalPort to zero and let the system select an unused port number every time you connect.

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