Results 1 to 6 of 6

Thread: VB6 - Proper sending/receiving data with Winsock (tutorial & code)

  1. #1

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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.
    Attached Files Attached Files
    Last edited by DigiRev; Aug 30th, 2007 at 03:06 AM.

  2. #2
    Junior Member
    Join Date
    Sep 2007
    Posts
    29

    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.

  3. #3

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: VB6 - Proper sending/receiving data with Winsock (tutorial & code)

    Quote 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.

  4. #4
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    327

    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

  5. #5
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: VB6 - Proper sending/receiving data with Winsock (tutorial & code)

    so what would the corrected code be now.
    can u please attache the corrected version

  6. #6
    Addicted Member
    Join Date
    May 2011
    Posts
    230

    Re: VB6 - Proper sending/receiving data with Winsock (tutorial & code)

    Quote Originally Posted by coolcurrent4u View Post
    so what would the corrected code be now.
    can u please attache the corrected version

    I know... probably dead thread... but for thoses who are looking at the solution TODAY, like I am...

    I don't think the line should be commented out...

    The lines:
    Code:
        'Append data to buffer.
        'Byte_Append bytBuffer, bytData
    are there for documentation purpose. it say: 'This is how it is done!"

    further below in the code of sckServer_DataArrival, you got 3 times the same line but for different data arrival method.

    thoses are the real lines.


    The author can correct me if I'm wrong.

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