Binary format errors
Closed / Fixed218435ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 00:57:34
OriginalBugID: 6156 Bug
Version: 8.3.2
SubmitDate: '2000-08-23'
LastModified: '2000-08-25'
Severity: SER
Status: Closed
Submitter: techsupp
ChangedBy: ericm
OS: Windows 95
FixedDate: '2000-10-25'
ClosedDate: '2000-08-25'
Name:
Brent Iwanoczko
Comments:
Help, ASAP :) Theres a emulator thats waiting to be built, failure here
for any reason means we cant use TK/Tcl, and the intention to purchase a
TCLPRO support license will be null and void.
ReproducibleScript:
Try the following 2 commands in your console.
EXPECTED RESULT - When you put the below output down a socket, the filevent on the client will get called once, and a gets on the socket will get 32 bytes of data.
set x [binary format Ia8IIIcccB8cxxx 124 WxYz0000 967004765 967004765 32 0 0 13 01010101 1]
UNEXPECTED RESULT - When you put the below output down a socket, the filevent on the client will get called twice, the first time getting 28 bytes, the second time getting 3 bytes. So not only is the gets split, you also lose a byte.
set x [binary format Ia8IIIcccB8cxxx 124 WxYz0000 967002665 967002665 32 0 0 8 01010101 10]
This will display a binary string that appears to be split over 2 lines, altho the length registers as 32 bytes as expected.
The sockets in all cases are fconfigure -translation binary -encoding binary
As it stands, the binary format command is not able to be used reliably, as every now and then with different data combinations, you get unexpected results.
ObservedBehavior:
Try the following 2 commands in your console.
EXPECTED RESULT - When you put the below output down a socket, the filevent on the client will get called once, and a gets on the socket will get 32 bytes of data.
set x [binary format Ia8IIIcccB8cxxx 124 WxYz0000 967004765 967004765 32 0 0 13 01010101 1]
UNEXPECTED RESULT - When you put the below output down a socket, the filevent on the client will get called twice, the first time getting 28 bytes, the second time getting 3 bytes. So not only is the gets split, you also lose a byte.
set x [binary format Ia8IIIcccB8cxxx 124 WxYz0000 967002665 967002665 32 0 0 8 01010101 10]
This will display a binary string that appears to be split over 2 lines, altho the length registers as 32 bytes.
The sockets in all cases are fconfigure -translation binary -encoding binary
As it stands, the binary format command is not able to be used reliably, as every now and then with different data combinations, you get unexpected results.
DesiredBehavior:
I expect to see the binary format command to always construct a string that doesnt have some kind of line break imbedded in it. So that a gets on
a socket gets all the data sent with the puts command, not bits of it.
From comp.lang.tcl (thanks to Dan Kuchler):
If you use 'gets' to read from a socket that has binary data placed
on it, and one of the characters in the data (when interpretted as
a string) is an end-of-line character (character codes 10 and 13
I believe) then that character will be swallowed as an end-of-line
character, which is what you are observing. The solution is to use
'read' which doesn't look for end-of-line.
On server (process 1):
---------------------------------------------------------
set server [socket -server [list foo] 9000]
proc foo {channel clientaddr clientport} {
fconfigure $channel -translation binary
fileevent $channel readable [list bar $channel]
}
proc bar {channel} {
if {[eof $channel]} {
close $channel
} else {
set res [read $channel]
puts "string length [string length $res]"
binary scan $res c* result
puts "number of bytes: [llength [split $result]]"
}
return
}
vwait forever
---------------------------------------------------------
On the client (process 2):
---------------------------------------------------------
# First try the string from your 'expected result' section:
set client [socket localhost 9000]
fconfigure $client -translation binary
set x [binary format Ia8IIIcccB8cxxx 124 WxYz0000 967002665 967002665
32 0 0 8 01010101 10]
puts -nonewline $client $x
flush $client
close $client
# Then try the string from your 'unexpected result' section:
set client [socket localhost 9000]
fconfigure $client -translation binary
set x [binary format Ia8IIIcccB8cxxx 124 WxYz0000 967002665 967002665
32 0 0 8 01010101 10]
puts -nonewline $client $x
flush $client
close $client
--------------------------------------------------------
output:
---------------------------------------------------------
string length 32
number of bytes: 32
string length 32
number of bytes: 32
When I change the bar procedure to:
proc bar {channel} {
if {[eof $channel]} {
close $channel
} else {
gets $channel res
puts "string length [string length $res]"
binary scan $res c* result
puts "number of bytes: [llength [split $result]]"
}
return
}
And retry I get:
string length 28
number of bytes: 28
string length 3
number of bytes: 3
string length 28
number of bytes: 28
string length 3
This is because (according to the man page):
NAME
gets - Read a line from a channel
SYNOPSIS
gets channelId ?varName?
_________________________________________________________________
DESCRIPTION
This command reads the next line from channelId, returns
everything in the line up to (but not including) the end-
of-line character(s), and discards the end-of-line characĀ
ter(s).
--
Eric Melski The Other Tcl Guy
ericm at ajubasolutions.com Ajuba Solutions
-- 08/25/2000 ericm
welch Closed ticket [218435ffff].
[History diff]2000-10-26 08:22:15
dgp Ticket [218435ffff]: 3 changes
[History diff]2001-04-20 02:40:46
Log in (anonymous is fine) to add a comment.