Link to home
Start Free TrialLog in
Avatar of eric0213
eric0213

asked on

Winsock DataArrival with chunked data

I'm receiving xml files from an Apache server that is sending me chunked data -- sometimes I get Content-Length, sometimes not, so I can't use that at all.  

I get data with the following line:
Winsock1.GetData sHTML, vbString, bytesTotal

f41
<Program>
 ...
<Files>
 ...
</Fil
f  
es>
</Program1>
0

The Winsock control is getting two chunks at once.  What's the best way to parse that?  I need to grab the hex number (f41) and get that many characters, then get the next hex number and get that many characters, and repeat till I hit zero.
Avatar of NicolaCimmino
NicolaCimmino

Private Function PraseResp(Buffer As String) As String

    ' Bytes to get
    ibytes = Val("&h" & Left(Buffer, InStr(Buffer, vbCr)))
   
    ' Strip HEX
    Buffer = Right(Buffer, Len(Buffer) - InStr(Buffer, vbCr) - 1)
   
    If ibytes <> 0 Then PraseResp = Left(Buffer, ibytes) & PraseResp(Right(Buffer, Len(Buffer) - ibytes))
   
End Function

And call it from your code:

sHTML = PraseResp(sHTML)

This is assuming your server returns both CR and LF as endlines, you might otherwise (or still) find some "spare" line feeds between the chunks, should be easy to fix it. I didn't test it so there might be minor bugs, but that's the idea
Well at least the function name is spelled wrong :) U might want to call it ParseResp...but at least I have been constant and mispelled it everywhere...oh well I use lots of cut and paste.


I just tought this might be a bit cryptic:


   ibytes = Val("&h" & Left(Buffer, InStr(Buffer, vbCr)))

I works only cause HEX numbers in VB are rapresented as &h3F and Val() is actually able to convert that...been pissed off myself for a while wondering why MS implemented HEX function and not the other way around, but then I tought to this time ago :)

Rest is normal recursion.

Cheers
Avatar of eric0213

ASKER

This is what I've come up with... anybody have anything better?  (more reliable -- if there's a flaw -- or more efficient)

(Hex2Long is a function to take an ascii hex value and convert it to a long)

    Winsock1.GetData sHTML, vbString, bytesTotal
    i = 1
   
    If Left(sHTML, 15) = "HTTP/1.1 200 OK" Then ' first chunk
        i = InStr(1, sHTML, vbCrLf & vbCrLf) + 4
        strHex = Mid(sHTML, i, InStr(i, sHTML, vbLf) - i)
        lngHex = Hex2Long(strHex)
        i = InStr(i, sHTML, vbCrLf) + 2
       
        tmpstr = Mid(sHTML, i, lngHex)
        txtOutput.Text = txtOutput.Text & Replace(tmpstr, vbLf, vbCrLf)
       
        i = i + lngHex
    End If
   
    Do While i < bytesTotal
        i = InStr(i, sHTML, vbLf) + 1
       
        If i > bytesTotal Or i = 0 Then Exit Do
       
        MsgBox i, , Len(sHTML) & " " & bytesTotal
        strHex = Mid(sHTML, i, InStr(i, sHTML, vbLf) - i)
        lngHex = Hex2Long(strHex)
        i = InStr(i, sHTML, vbCrLf) + 2
       
        tmpstr = Mid(sHTML, i, lngHex)
        If tmpstr <> "" Then
            txtOutput.Text = txtOutput.Text & Replace(tmpstr, vbLf, vbCrLf)
        End If
       
        i = i + lngHex
    Loop
Mine doesn't work... I've changed it a bit, but it's still not working.

NicolaCimmino,
Your's doesn't work because I don't always have a hex number.  On static files (I'm trying to retrieve both static and dynamic pages), it gives me a content-length in the header and then bare chunks.

Also, when I ran your's, I wasn't getting the last chunk -- I only tried it with a 6k (two chunks) file.
I forgot to mention, I sometimes send a couple requests quickly in a row and the server sends them in a single chunk.  Each file with its own hex length and 0 ending character.
eric0213,
No comment has been added lately (157 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: PAQ/Refund

Please leave any comments here within 7 days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

Dabas
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post a request in Community support (with a link to this page) to refund your points. https://www.experts-exchange.com/Community_Support/
ASKER CERTIFIED SOLUTION
Avatar of SpideyMod
SpideyMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial