Solution: Winsock Does Not Flush Buffer Correctly 
Author Message
 Solution: Winsock Does Not Flush Buffer Correctly

Hi,

    This is for those who, like me, have encountered problems using the
Winsock control with multiple clients, and who, like me, went "DOH!"
when they saw the "Resolution" field of the bug report on MSDN was
blank.  Somebody probably already posted this, but just in case, here is
a solution I found.

   Looping to send all the messages out to all the client-connected
controls, like below, causes the problem:

For i = 1 To numSockets
    sckSocket(i).SendData textToSend
Next i

    Only socket number "numSockets" (the last one) will receive
textToSend.  All the other clients will be left with nothing (their
transmission buffer will not be flushed).  My impression of what's going
on is that SendData buffers everything, because it does not have to time
to send it all between two (or more) very close calls.  Then, it just
never gets around to flushing the buffers (unless the last client
connection is killed, in which case the second last client will now
receive the data).  This is correlated by the fact that if you insert a
MsgBox function between each SendData, the data DOES gets to each client
(because there is a time lapse between the calls to SendData), and
SendData works like it should (or at least, like we would like it to :)
).

    Luckily, the Winsock control implements a SendComplete event.
Unfortunately, it's just that, an event, and not a function you can
query to wait until the stream is flushed.  Basically, the solution goes
like this:  the program sets a module-level variable to the value of
textToSend, and sends out its contents to the first client socket.  That
socket's SendComplete event will send out the module-level variable's
contents to the next socket, and so on.  The idea can be adapted to byte
buffers and pretty much anything else.

Dim sendBuffer as String

Private Sub SendToClients (caption as String)
    sendBuffer = caption
    'Make sure the socket is valid before doing this
    sckSocket(1).SendData sendBuffer
End Sub

Private Sub sckSocket_SendComplete (index as Integer)
    If index < numSockets Then
        'Add any other code here
        sckSocket(index + 1).SendData sendBuffer
    End If
End Sub

    Of course, the actual code might be a bit longer.  First thing to
add is to make sure the sockets are actually connected before using
SendData on them, or else VB will raise a run-time error.

    That's it!

-----



Sun, 24 Nov 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. winsock control: buffer flushing?

2. winsock control: buffer flushing?

3. "Object Server Not Correctly Registered" - Solution

4. Winsock not flushing data

5. Flushing buffer?

6. Qbasic- Flushing the buffer

7. Buffer Flush

8. serial port buffer flushing ?

9. Flushing keyboard buffer

10. Q: How to Flush VB File buffers

11. Need to flush keyboard buffer somehow

12. SendKeys for Dos and Buffer flushing

 

 
Powered by phpBB® Forum Software