Results 1 to 4 of 4

Thread: [RESOLVED] Winsock closing before data gets sent

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    Resolved [RESOLVED] Winsock closing before data gets sent

    I have two PCs connected using winsock (TCP), and before one of them disconnects my program sends a message to let the other end know that the connection is being killed. To do this, I have a very simple bit of code:

    Code:
    Private Sub BtnDisconnect_Click()
        If Sock1.State = sckConnected Then
            Sock1.SendData ("~disconnecting") 
        End If
        Sock1.Close
    End Sub
    However, the other computer never receives the message. If I remove "Sock1.Close" it works fine. I don't really have a clue what is happening. Is there any simple way to fix this problem?

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Winsock closing before data gets sent

    have you tried waiting until the Send_Complete triggers? eg:
    Code:
    '/// Boolean handler to determine when to disconnect.
    Private DC_Sent As Boolean
    
    Private Sub Command1_Click()
    DC_Sent = False
    '/// connect your socket
    
    End Sub
    
    Private Sub btnDisconnect_Click()
    If sock1.State = sckConnected Then
     sock1.SendData ("~disconnecting")
     DC_Sent = True
    End If
    End Sub
    
    Private Sub sock1_SendComplete()
    If DC_Sent = True Then
        sock1.Close
    End If
    End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

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

    Re: Winsock closing before data gets sent

    As dynamic said, use the SendComplete() event. I've often written a WaitForSend() sub to help keep things simple if you have a lot of code that sends something before disconnecting.

    vb Code:
    1. Option Explicit
    2.  
    3. 'Used so DoEvents is only called when needed.
    4. Private Declare Function GetInputState Lib "user32" () As Long
    5.  
    6. Private bolSending As Boolean 'Sending data?
    7.  
    8. Private Sub WaitForSend()
    9.     bolSending = True
    10.     Do
    11.         If GetInputState Then DoEvents
    12.     Loop Until Not bolSending Or Winsock1.State = sckClosed Or Winsock1.State = sckError
    13. End Sub
    14.  
    15. Private Sub Winsock1_SendComplete()
    16.     bolSending = False
    17. End Sub
    18.  
    19. 'Then you can use the code like this:
    20. Private Sub cmdSendBeforeClose_Click()
    21.     Winsock1.SendData "~disconnecting"
    22.     WaitForSend
    23.     Winsock1.Close
    24. End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    Re: Winsock closing before data gets sent

    Thank you both. The program is working properly now

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