On Jul 7, 2012, at 1:57 AM, Arnaud Nicolet wrote:
> You're removing my last hope of understanding what's wrong with my code.
> Ok, I'll try to re-create the picture locally each time. If it fails on both
> sides, then it means the converted string can become bad. If it fails only on
> the receiving side, then it's indeed a TCP problem.
Once more. I strongly suggest you look at the data reception framing in your
TCPsocket.
If one naively treats the DataAvailable event firing as meaning the entire
package has arrived, you WILL corrupt your data package because you either get
too much or too little from the data buffer.
DataAvailable event firing DOES NOT mean your entire incoming data package has
arrived. It merely means SOME material is available in the buffer. For small
packages of information this often fires when the entire package has arrived,
but for anything large (like your big incoming picture package), the
dataAvailable event may fire BEFORE the entire package has actually arrived.
Set up your data transmissions to either indicate the Content-Length of each
message or include a specific marker to delimit your packages.
In your DataAvailable event, check LookAhead() to see if your entire package
has arrived. Once you see the entire content-length is in buffer, read the
right number of bytes to get your entire inbound package.
I use something like the below in a DataAvailable event…...
'data was sent using
'contentLength = lenB(theMessage)
'theIPCsocket.write "Content-length: " + str(contentLength) + crlfcrlf +
theMessage
dim crlfcrlf, lookaheadstring, lengthString as string
dim crlfcrlfPos, contentLength, totalLength as integer
dim theMessage, junk as string
const kMaxContentLength = 100000000 'a large number of bytes that we'll never
transmit
crlfcrlf = chr(13) + chr(10) + chr(13) + chr(10)
lookaheadstring = LookAhead()
crlfcrlfPos = InStrB( lookaheadstring, crlfcrlf )
if crlfcrlfPos = 0 then return 'because the length header hasn't yet arrived
'header contains length of content
'The message has arrived. What about message contents?
if InStrB( lookaheadstring, "Content-length: ") = 0 then
'length head has not arrived yet.
return
else
'determine the size of the incoming message
lengthString = NthField(lookaheadstring, "Content-length: ", 2)
if InStrB(lengthString, crlfcrlf) = 0 then return 'length data has not yet
arrived
lengthString = NthField(lengthString, crlfcrlf, 1)
contentLength = val(lengthString)
totalLength = crlfcrlfPos + 3 + contentLength
'now see if they are all in the buffer
if BytesAvailable < crlfcrlfPos + 3 + contentLength then
'not all arrived yet
return
end
end
'all arrived. Read in just the current message bytes
theMessage = Read(totalLength, encodings.UTF8)
theMessage = midB(theMessage, crlfcrlfPos + 4)
'safely dispose of leftover bytes to ensure synchronization self heals
while BytesAvailable > 0
call ReadAll
wend
'now you can process the inbound theMessage content
_______________________________________________
Mbsplugins_monkeybreadsoftware.info mailing list
[email protected]
https://ml01.ispgateway.de/mailman/listinfo/mbsplugins_monkeybreadsoftware.info