Results 1 to 7 of 7

Thread: Help! on vbArray Winsock DataArrival

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    148

    Post Help! on vbArray Winsock DataArrival

    Code:
    Private Sub Socket_DataArrival(ByVal bytesTotal As Long)
    Dim Buffer() As Byte, I%, Y%, X%, FirstPos As Boolean, ParseString$
    Socket.GetData Buffer, vbByte + vbArray
    
    For I = LBound(Buffer) to UBound(Buffer)
         IF FirstPos =True then
                IF Buffer(I) = 53 and Buffer(I+1) = 192 and Buffer(I+2) = 128 then
                          Y = I
                          FirstPos = False
                          Exit For
                End If
         IF Buffer(i) = 192 and Buffer(I+1)=128 Then
                X = I + 2
                FirstPos = True
         End IF
    Next I       
           For I = X to Y
                   ParseString = ParseString & chr(Buffer(i))
           Next I
    End Sub

    Is There any Better and Faster Way to Do this?

    Thankx for the Help

    P.S. Some People might ask why don't use String instead of Byte?
    Coz in some Lang. Don't support string with chr(129 to 254) it will return chr(0).

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Help! on vbArray Winsock DataArrival

    First off declare your variables properly

    vb Code:
    1. Dim Buffer() as Byte
    2. Dim I as Integer, X as Integer, Y as Integer
    3. Dim FirstPos as Boolean
    4. Dim ParseString as String

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    148

    Re: Help! on vbArray Winsock DataArrival

    Ok sorry
    is that the only way?

  4. #4
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Help! on vbArray Winsock DataArrival

    Well it would help possibly. Seeing as some of them were declared as variants. Otherwise if everything works it looks all good

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Help! on vbArray Winsock DataArrival

    Putting the symbols (% $ etc) in front of the variable names is the same as saying " as integer or as string" so they're not variants.

    What exactly is your code doing in the data arrival event? Working with byte arrays is much faster than strings so you're good there.

    But you have a 2nd loop after all that, using string concatenation (string = string & ...). If there is a lot of data that can slow things down a lot.

    But I'm wondering what your code is doing so maybe it can be optimized by putting everything in one loop.

    More details please.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    148

    Post Re: Help! on vbArray Winsock DataArrival

    Well I'm trying to Write small yahoo client to Learn so later I can write my own
    because sometime yahoo packet from server to oversize.

    There is 2 byte to define the Packet size
    everything a oversize packet come in my program hang.

    Here is the dataarrival I got

    Code:
    Private Sub wsYMSG_DataArrival(ByVal bytesTotal As Long)
    Dim Buffer() As Byte, TrueBuffer() As Byte, lenPacket%, I%, SubBuffer() As Byte, LPacket%
    Static subX%
    On Error Resume Next
    wsYMSG.GetData Buffer, vbByte + vbArray
    reCheck:
    Buffer = InsertArray(Buffer, SubBuffer, 0)
    lenPacket = (Buffer(8) * 256) + Buffer(9) + 19
    If UBound(Buffer) = lenPacket Then Debug.Print lenPacket Else Debug.Print lenPacket & ":" & UBound(Buffer)
    If lenPacket <= UBound(Buffer) Then
        ReDim TrueBuffer(lenPacket)
            For I = 0 To lenPacket
                TrueBuffer(I) = Buffer(I)
            Next I
                Call Chat_Handle(TrueBuffer)
                ReDim SubBuffer(UBound(Buffer) - lenPacket)
                If UBound(SubBuffer) = 0 Then Exit Sub
            For I = lenPacket To UBound(Buffer)
                SubBuffer(X) = Buffer(I)
                subX = subX + 1
            Next I
                subX = 0
            GoTo reCheck
            DoEvents
    End If
    End Sub
    
    Public Function InsertArray(ByRef FstArray() As Byte, ByRef SecArray() As Byte, InsertPos As Integer) As Byte()
        Dim InsArray()           As Byte
        Dim FstLen%, SecLen%, SecPos%, ArraySize%, TailLen%
        FstLen = UBound(FstArray) + 1
        SecLen = UBound(SecArray) + 1
    
        ArraySize = FstLen + SecLen - 1
        SecPos = Insert + SecLen
        TailLen = FstLen - InsertPos
    
        ReDim InsArray(ArraySize)
        CopyMemory InsArray(0), FstArray(0), CLng(InsertPos)
        CopyMemory InsArray(InsertPos), SecArray(0), CLng(SecLen)
        CopyMemory InsArray(SecPos), FstArray(InsertPos), TailLen
        InsertArray = InsArray
    End Function
    So what am i doing wrong here?
    Thankx for big help
    Last edited by lmf; Jul 16th, 2007 at 09:48 PM.

  7. #7
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    Re: Help! on vbArray Winsock DataArrival

    Quote Originally Posted by lmf
    So what am i doing wrong here?
    For starters, you aren't redimming Buffer and SubBuffer before you put data into them.

    Have you looked at the value of lenPacket the first time you put a value in it (you are doing this on every DataArrival when you should only be doing it once for each record) to see if the size is reasonable? Windows is little-endian and the size field is most likely in network byte-order or big-endian. You may have to use the ntohs API to convert the value to little-endian.

    Take a look at this thread for some code that receives very large records. The first 4 bytes contain the record length. Bytes 53 & 54 contain the record identifier. Both fields are converted to little-endian. There are a few other things going on in the code that you can disregard, but do look at the use of the 2 globals gbReplyBuff and copyPtr.

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