text via TcpSocket (encoding question)

If I send text via TcpSocket (which uses String for its handling), and the text sent is in UTF-8 encoding… it is still possible for an encoding error to occur on the receiving end (if I just immediately assigned the received data as text within the dataAvailable event) if the packet is truncated somewhere where a two-byte character didn’t fully transmit before the dataAvailable event fires, correct?

Correct. You must ensure that the entire message has been received, which could take more than one DataAvailable. And remember to DefineEncoding before you change it to a Text value.

I did a blog about this.

Thanks, I appreciate it you two. I thought so, just wasn’t for certain… and this is one of those things where 99% of the time, the dataAvailable event would fire just right for it to work… leaving me in a bind had I released it.

I know this is dirty, but wouldn’t something like this work too (would be even safer to specify the exception to catch… but as an example):

[code] dim myTextBuffer as text
dim testText as text

testText = self.lookAhead(encodings.utf8) 'if not a valid text, will throw exception
myTextBuffer = self.readAll.toText
catch 'catch exception, text not valid
end try[/code]

Do you have no way of knowing if you got the entire message?

When you use a TCP socket you know when the full data has arrived, the TCP socket doesn’t.

Either the stream coming in contains at the beginning the length of the message, or there is some termination marker at the end of the message. So you need to fill a buffer with the incoming data in each DataAvailable event until you have reached either the length or the terminator.