VB6 - Proper sending/receiving data with Winsock (tutorial & code)
I think this is information every Winsock or client/server developer should know. It is something I wish I had figured out soon after I first started. While it has been common knowledge to me for some time now (and for many others), without it, your client/server programs will be very fragile and bugs can pop up at any time.
The tutorial is meant to be as short and straightforward as possible. I wrote this in about an hour or maybe a little more.
Please let me know if it's helped, if you have any questions, if there's a bug, or anything. And read the tutorial before looking at the code.
This article explains (and gives code for) how to handle 2 or more packets arriving at the same time, or "together". But unlike other tutorials, it also explains another critical problem, which is one packet getting split in the middle. So I think even if you know most (or possibly all) of this stuff it is still useful information.
Last edited by DigiRev; Aug 30th, 2007 at 03:06 AM.
Re: VB6 - Proper sending/receiving data with Winsock (tutorial & code)
Hey i'd like to start out by saying nice example. Very easy to follow. I havn't tested it fully yet, but I was looking through the code for the byte arrays code that you included. I'm looking in the DataArrival on the server winsock control.
Code:
Private Sub sckServer_DataArrival(ByVal bytesTotal As Long)
Dim bytData() As Byte, lonLoop As Long
Dim lonUB As Long, bytCheck() As Byte
Dim lonPrevEOP As Long, bytPacket() As Byte
Dim bytTemp() As Byte, bytTruncated() As Byte
Dim bolTruncated As Boolean, lonLastPos As Long
Dim bytHeader() As Byte, strHeader As String
'Get received data and put it into bytData().
sckServer.GetData bytData(), vbByte + vbArray, bytesTotal
Debug.Print StrConv(bytData, vbUnicode)
'Append data to buffer.
'Byte_Append bytBuffer, bytData
You have the Byte_Append bytBuffer, bytData commented out. This will effectively elimenate the buffer. Is this a mistake? or am i just reading it wrong? Again i havn't had time to check it i was just reading through.
Re: VB6 - Proper sending/receiving data with Winsock (tutorial & code)
Originally Posted by itzviper
Hey i'd like to start out by saying nice example. Very easy to follow. I havn't tested it fully yet, but I was looking through the code for the byte arrays code that you included. I'm looking in the DataArrival on the server winsock control.
Code:
Private Sub sckServer_DataArrival(ByVal bytesTotal As Long)
Dim bytData() As Byte, lonLoop As Long
Dim lonUB As Long, bytCheck() As Byte
Dim lonPrevEOP As Long, bytPacket() As Byte
Dim bytTemp() As Byte, bytTruncated() As Byte
Dim bolTruncated As Boolean, lonLastPos As Long
Dim bytHeader() As Byte, strHeader As String
'Get received data and put it into bytData().
sckServer.GetData bytData(), vbByte + vbArray, bytesTotal
Debug.Print StrConv(bytData, vbUnicode)
'Append data to buffer.
'Byte_Append bytBuffer, bytData
You have the Byte_Append bytBuffer, bytData commented out. This will effectively elimenate the buffer. Is this a mistake? or am i just reading it wrong? Again i havn't had time to check it i was just reading through.
Wow, what a terrible mistake. Thanks for pointing that out, yes that should not be commented.
Re: VB6 - Proper sending/receiving data with Winsock (tutorial & code)
hey digiRev thank you for the code i was using your project byte array and i removed the comment on
Code:
Byte_Append bytBuffer, bytData
..yet, i get error on your code on local error handling ..
Code:
Private Function SafeUBByte(ByteArray() As Byte) As Long
On Error GoTo ErrorHandler
SafeUBByte = UBound(ByteArray()) + 1 'error
Exit Function
ErrorHandler:
SafeUBByte = 0
Exit Function
End Function
lastly thank you very much ..i was indeed in search of this you rock