Reputation: 717
When attempting to return a multi-line output via TCL server, the server reports a broken pipe even though the client is able to receive all the data.
Server:
proc Echo_Server {port} {
set s [socket -server EchoAccept $port]
vwait forever
}
proc EchoAccept {sock addr port} {
puts "Accept $sock from $addr port $port"
fconfigure $sock -buffering line
fileevent $sock readable [list Echo $sock]
}
proc Echo {sock} {
if { [eof $sock] || [catch {gets $sock line}] } {
puts "Close $sock"
close $sock
} else {
set returnData "
Hi
How are you?
This is test data
Close
"
puts $sock $returnData
puts $sock "EOF"
flush $sock
}
}
Echo_Server 2500
vwait forever
Client code to send/receive data:
proc Echo_Client {host port} {
set s [socket $host $port]
fconfigure $s -buffering full -buffersize 1000000
#fconfigure $s -buffering line -blocking 0
return $s
}
set s [Echo_Client localhost 2500] ; puts $s "dummy command" ; flush $s
set pp 1 ; while { $pp == 1 } { set line [gets $s] ; puts $line ; if { [regexp "EOF" $line] } { set pp 0 } } ; flush $s ; close $s
The client receives the data sent from the server but the server reports the following:
% source /edascratch/nobackup/sanjaynn/scripts/simple_server.tcl
Accept sock6 from 127.0.0.1 port 59814
error writing "sock6": broken pipe
while executing
"puts $sock $returnData"
(procedure "Echo" line 15)
invoked from within
"Echo sock6"
Is there something obvious that I am missing?
Upvotes: 0
Views: 635
Reputation: 611
The [eof $sock]
condition isn't set before the read in [gets $sock line]
, and the [gets]
call doesn't return an error here, it returns -1
to indicate that there's no data available. Just reverse the conditions order and the server will start working properly:
...
if { [catch {gets $sock line}] || [eof $sock] } {
...
Upvotes: 2