Results 1 to 8 of 8

Thread: Winsock Send doesn't work on form Unload event?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    126

    Winsock Send doesn't work on form Unload event?

    Hi, the bellow code doesn't send data to server, the server doesn't receive it. please give me a solution.

    Private Sub Form_Unload(Cancel As Integer)
    sockMain.SendData "Closing"
    End Sub

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Winsock Send doesn't work on form Unload event?

    Ideally you would probably write a handler for the Query_Unload event and have a Form-level Boolean like SentClosing.

    In it you would check unloadmode, and if it is vbFormControlMenu and SentClosing is False, then do something like:
    Code:
    If unloadmode = vbFormControlMenu And Not SentClosing Then
        If sckMain.State = sckOpen Then
            sckMain.SendData "Closing"
            sckMain.Close
        End If
        SentClosing = True
        cancel = True
    End If
    Then in your SendComplete handler you'd want something like:
    Code:
    If SentClosing Then
        Unload Me
    Else
        ... other existing logic...
    End If
    You'd probably also need to manage SentClosing a bit more, such as making sure it is always True until you open your connection (where you'd set it to False).
    Last edited by dilettante; Aug 20th, 2012 at 10:43 AM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    126

    Re: Winsock Send doesn't work on form Unload event?

    Quote Originally Posted by dilettante View Post
    Ideally you would probably write a handler for the Query_Unload event and have a Form-level Boolean like SentClosing.

    In it you would check unloadmode, and if it is vbFormControlMenu and SentClosing is False, then do something like:
    Code:
    If unloadmode = vbFormControlMenu And Not SentClosing Then
        If sckMain.State = sckOpen Then
            sckMain.SendData "Closing"
            sckMain.Close
        End If
        SentClosing = True
        cancel = True
    End If
    Then in your SendComplete handler you'd want something like:
    [code]
    If SentClosing Then
    Unload Me
    Else
    You mentioned that i have to check SentClosing in the SendComplete. But in my case i have multiple Winsocks which i want them all to send some data before closing. so i don't know how to be sure that all of them send their data beore closing the form?
    Last edited by ufo973; Aug 20th, 2012 at 11:16 AM.

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Winsock Send doesn't work on form Unload event?

    What do you mean multiple winsocks? Do you have one client with multiple winsocks or multiple clients with one winsock each?

    If it a single client with one socket connected to server then dilettante's method will work except I don't think you should close the socket immediately following the

    sckMain.SendData "Closing"
    'NO--->sckMain.Close

    If you have one client with multiple sockets then isn't each socket indexed? If so, then it's the same code except you use the index to send the closing message and then in the SendComplete you use that index again to close that socket then check if that index is the last socket to send a closing message and if so then unload your Form.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    126

    Re: Winsock Send doesn't work on form Unload event?

    No unfortunately my winsocks are not indexed. Now what?

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Winsock Send doesn't work on form Unload event?

    It would be basically the same thing except each winsock will have it's own SendComplete event. Only in the last socket to send a closing message SendComplete event will you check for SentClosing.

    Code:
      '
    Dim SentClosing As Boolean
      '
      '
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
     If unloadmode = vbFormControlMenu And Not SentClosing Then
       If sckAdam.State = sckOpen Or sckAdam.State = sckConnected Then
         sckAdam.SendData "Closing"
       End If
       '
       ' etc, etc for all sockets
       '
      If sckZebra.State = sckOpen Or sckZebra.State = sckConnected Then
         sckZebra.SendData "Closing"
       End If
       
       SentClosing = True
       
       cancel = True
     End If 
    End Sub
      '
      '
    '
    ' First socket to close
    '
    Private Sub sckAdam_SendComplete()
     If SentClosing Then
       sckAdam.Close 
     Else
       '... other existing logic...
     End If
    End Sub 
      '
      ' etc, etc
      '
    '
    ' Last socket to close
    '
    Private Sub sckZebra_SendComplete()
     If SentClosing Then
       sckZebra.Close 
       Unload Me
     Else
       '... other existing logic...
     End If
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Winsock Send doesn't work on form Unload event?

    Yes, the details just need to be thought through. You manage the tracking of whether Query_Unload needs to send-and-close and when to finally do the final Form Unload that actually ends the program.

    You can defer the Winsock Close until after the "closing" SendComplete. However calling the Close method doesn't shut a TCP connection down, it just terminates sending for your end of the two-way connection. The other end can still receive the in-transit data and even send you more data.

    The real question is "Why bother?"

    The program(s) at the other end(s) of your connection(s) get notified anyway. If the other program(s) is(are) VB6 using Winsock controls they'll get Close events.

    There shouldn't be any reason for such a "Closing" message, which is why people seldom need to do all of this.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    126

    Re: Winsock Send doesn't work on form Unload event?

    Quote Originally Posted by jmsrickland View Post
    It would be basically the same thing except each winsock will have it's own SendComplete event. Only in the last socket to send a closing message SendComplete event will you check for SentClosing.

    Code:
      '
    Dim SentClosing As Boolean
      '
      '
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
     If unloadmode = vbFormControlMenu And Not SentClosing Then
       If sckAdam.State = sckOpen Or sckAdam.State = sckConnected Then
         sckAdam.SendData "Closing"
       End If
       '
       ' etc, etc for all sockets
       '
      If sckZebra.State = sckOpen Or sckZebra.State = sckConnected Then
         sckZebra.SendData "Closing"
       End If
       
       SentClosing = True
       
       cancel = True
     End If 
    End Sub
      '
      '
    '
    ' First socket to close
    '
    Private Sub sckAdam_SendComplete()
     If SentClosing Then
       sckAdam.Close 
     Else
       '... other existing logic...
     End If
    End Sub 
      '
      ' etc, etc
      '
    '
    ' Last socket to close
    '
    Private Sub sckZebra_SendComplete()
     If SentClosing Then
       sckZebra.Close 
       Unload Me
     Else
       '... other existing logic...
     End If
    End Sub
    Very nice example thank you very much. But are you sure that if i send data through multiple sockets, the vb send them in order like not messing the order?
    Like if i do....

    Winsock1.send data
    winsock2.send data
    winsock3.send data

    so here the winsock3 is the last socket which will complete the sending process?

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