sendcomplete yet not firing!!
this is the only way i can think of using sendcomplete to send data via winsock.my programming language is vb 6 . here is my code
vb Code:
public SendComplete as boolean
Public Sub WaitForWinsock()
While Not SendComplete
DoEvents
Wend
End Sub
Private Sub sock1_SendComplete()
SendComplete = True
End Sub
private sub CmdSend_click()'an example of how i send data
dim strData as string
strData = "Hello"
sendComplete = False
winsock.senddata strData
WaitForWinsock
End Sub
is there anybetter way to code ..?:confused: coz my data are not being send properly
Re: sendcomplete yet not firing!!
Why not just use the SendComplete event to begin with?
Re: sendcomplete yet not firing!!
Quote:
Originally Posted by Atheist
Why not just use the SendComplete event to begin with?
a small example to get me started would be highly appreciated , and it would be helpful to others aswell :D:thumb:
Re: sendcomplete yet not firing!!
The SendComplete event doesn't really mean the previous send completed, it means that you can do another SendData without it blocking. In other words it means there is room to buffer more outgoing data.
Effectively though this is the same thing. Thus you can generally treat it as meaning the previous send completed... and thus the name.
It is my opinion that using DoEvents() calls in a Form with a Winsock control leads to programs that just don't always work. You never need DoEvents() with Winsock unless you are doing something wrong.
In the most general case you would use some data structure as a queue of outgoing messages. Each SendComplete event you would take the next queued message and send it. To queue a new message add it to the queue structure and check for some "sending" Boolean. This variable would be set False in your SendComplete handler and True after any SendData. If when you queue a new message "sending" is False, SendData with your new message and set sending=True.
The SendComplete handler would go something like:
Code:
sending = False
If Queue is not Empty Then
msg = Queue.Remove() 'Remove 1st msg from Queue
wsk.SendData msg
sending = True
End If
The QueueMsg routine would go like:
Code:
If not sending Then
wsk.SendData msg
sending = True
Else
Queue.Insert msg
End If
For Queue you could use something as simple as a Collection.
Now most programs don't require this generality. Instead of an explicit queue they use some other global indicators of state to decide what to do in the SendComplete handler.
If sending a file in chunks, the SendComplete might check for the file number variable <> 0 and if so read another chunk from disk and then call SendData with it. Upon EOF it might close the file and set the file number variable to 0.
Sending array elements or anything else can be handled in a similar manner.
Writing a bunch of straight-line code to send stuff is where people go wrong. You can't send over Winsock synchronously because it is not an instantaneous process. If you did your program would hang.
This is where using a queue can help. Instead of "send, send, send" you can just "queue, queue, queue" and let the SendComplete process parcel things out over the Winsock control.
Re: sendcomplete yet not firing!!
while we are at this post ,i would like to ask one more thing. when i am just using winsock.senddata it works fine but when i am using it under if and else statement the data does not get sent immediately instead it fires only after i receive a message or send another data . why is it so ? and how can i overcome it ?