Results 1 to 2 of 2

Thread: Winsock and dealing with multiple packets...

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Winsock and dealing with multiple packets...

    I am sending a large chunk of data using the winsoc control this data is being sent as a byte array. The data is too large, and so is spanned over x many packets. How do I add all the packets together???
    Remember, NOT strings, only byte arrays? Is there anyway to add them together without damamging the data???

    Woka

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    I assume that you are interested in how to "assemble" the packets as they are received. I have a number of clients that receive large (up to 18520 bytes) UDTs. This is how I do it.

    All of the variables referenced by the form's DataArrival sub are global to preserve the data until all of it has been received. The key variable is the Integer copyPtr. It is first initialized to zero and then incremented (indirectly) by the number of bytes received. It is used as a sort of pointer for all of the CopyMemory calls needed to assemble the record.

    One other thing I should point out: we use a standard 52 byte header on all of our record structures. The header contains a variety of information. Among these, the first 4 bytes is a Long that contains the size of the record (header + data). In addition, the first 2 bytes of the data is a Integer that identifies the record type. With the length, I assemble the packets until the entire record has been received. Then, with the record type, I call the appropriate handler.
    VB Code:
    1. Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
    2.    ' determine the data type and process it
    3.    
    4.    ' get the buffer contents
    5.    ReDim gbIOBuff(bytesTotal)
    6.    tcpClient.GetData gbIOBuff, vbArray + vbByte, bytesTotal
    7.    
    8.    If copyPtr = 0 Then ' we are at the beginning of a new reply
    9.       ' save size & type of reply
    10.       CopyMemory recLen, gbIOBuff(0), 4
    11.       recLen = ntohl(recLen)
    12.       CopyMemory codeCheck, gbIOBuff(52), 2
    13.       codeCheck = ntohs(codeCheck)
    14.       ReDim gbReplyBuff(bytesTotal) ' clear old data record & start new one
    15.    Else
    16.       ReDim Preserve gbReplyBuff(copyPtr + bytesTotal) ' retain data & bump the size
    17.    End If
    18.      
    19.    ' copy the received data to the work buffer at location referenced by copyptr
    20.    CopyMemory gbReplyBuff(copyPtr), gbIOBuff(0), bytesTotal
    21.    If UBound(gbReplyBuff) = recLen Then
    22.       ' we got the entire record
    23.       If recLen = gilocLen Or recLen = gijobLen Or recLen = giSockRtn Then
    24.          If codeCheck = glLocRequest Then
    25.             Process_Locations        ' 1 time only at Form_Load
    26.          Else
    27.             If codeCheck = glJobRequest Then
    28.                Process_JobStats
    29.             Else
    30.                Beep
    31.                sbStatus.Panels(2).Text = "Error: " & codeCheck & ", No text returned"
    32.                intTics = 0 ' reset display timer
    33.             End If
    34.          End If
    35.       End If
    36.       copyPtr = 0   ' reset the flag\pointer
    37.    Else
    38.       ' save the current record length\next copy location
    39.       copyPtr = UBound(gbReplyBuff)
    40.    End If
    41.      
    42. End Sub
    HTH

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