0

I am reading data from a TCP port in TCL using a socket. The messages do not end with any newline, but they do container a header containing the number of bytes of data.

I have the following code to read two byte of data from the socket (16bit little endian) and convert that into an integer I can then use in a loop to read the rest of the data:

  binary scan [read $Socket 2] s* length

In this case $Socket is my socket and it has been configured to use binary encoding.

This works well except where either the upper or lower byte is 0x0D. It appears TCL reads 0x0D and 0x0A both as '\n', which then defaults to 0x0A, so the code does work correctly. For example 13 is read as 10. How do I stop this from happening?

2 Answers 2

1

The socket should be placed into binary mode if you're moving binary data across it.

chan configure $Socket -translation binary
# Use [fconfigure] instead of [chan configure] in older Tcl versions

This disables all the automatic processing that Tcl usually does — your description says you're having a problem with end-of-line conversion — and makes it so that read will just deliver a string of the bytes (formally a string of characters between U+000000 and U+0000FF, and internally using an efficient in-memory encoding scheme).

For files, you can include b in the control mode when opening to get this done for you. For sockets, you need to do this yourself.

Sign up to request clarification or add additional context in comments.

4 Comments

If the rest of the message is a string, you might need to then further decode it; it's normal to use encoding convertfrom for that sort of thing.
Thanks - I got this working in Tcl 8.3 using: fconfigure $Socket -translation binary
8.3? Holy unsupported Tcl version from the 1990s, Batman!
Yeah, it's some pretty old code I'm updating! Is there only a syntax difference between chan configure and fconfigure?
0

In addition to configuring binary encoding, you also need to set the translation to 'lf'. As this is a frequently occurring situation, there is a shorthand for making these two settings:

fconfigure $Socket -translation binary

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.