I am new at TCL and trying an example within a book called Practical Programming.
I am trying to connect to an echo server with a client within the same file. So I have made a file called EchoServer.tclsh. I then have written the echo server code within this and the echoclient code. When I run this I receive an error that shows:
couldn't open socket: connection refused
while executing
"socket $host $port"
(procedure "Echo_Client" line 2)
invoked from within
"Echo_Client localhost 2540"
invoked from within
"set s [Echo_Client localhost 2540]"
(file "echo_server.tcl" line 35)
The code for the server is:
proc Echo_server {port} {
global echo
set echo(main) {socket -server EchoAccept $port}
}
proc EchoAccept {sock addr port} {
global echo
puts "Accept $sock from $addr port $port"
set echo(addr,$sock) [list $addr $port]
f configure $sock -buffering line
fileevent $sock readable [list Echo $sock]
}
proc Echo {sock} {
global echo
if {[eof $sock] || [catch {gets $sock line}]} {
# end of file or abnormal connection drop
close $sock
puts "Close $echo(addr,$sock)"
unset echo(addr,$sock)
} else {
if {[string compare $line "quit"] ==0} {
# Prevent new connections.
# Existing connections stay open.
close $echo(main)
}
puts $sock $line
}
}
The code for the client is:
proc Echo_Client {host port} {
set s [socket $host $port]
fconfigure $s -buffering line
return $s
}
set s [Echo_Client localhost 2540]
puts $s "Hello!"
gets $s
the line
set echo(main) {socket -server EchoAccept $port}
should be
set echo(main) [socket -server EchoAccept $port]
And enter the event loop by doing a
vwait forever
at the end
Related
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?
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] } {
...
I'm writing a socket utility to communicate a client to a server. When input to the socket from the client side, the server is receiving it fine. However, when input to the socket from the server, the client can't read. When checking for fblocked $channel. It is 1. I've tried everything including adding new line, ...
Please help.
Below is my code
proc read_command { sock } {
variable self
global connected
set len [gets $sock line]
set bl [fblocked $sock]
puts "Characters Read: $len Fblocked: $bl"
if {$len < 0} {
if {$bl} {
puts "Input is blocked"
} else {
set connected 1
puts "The socket was closed - closing my end"
close $sock
}
} else {
if {!$bl} {
puts "Read $len characters: $line"
catch {uplevel #0 $line} output
puts "1==>$output<=="
puts $sock "$output"
puts $sock "\n"
flush $sock
}
}
}
proc client { host port } {
variable self
set s [socket $host $port]
set self(csock) $s
set self($s,addr) $host
set self($s,port) $port
fconfigure $s -buffering line -blocking 0
return $s
}
proc prun { sock args} {
variable self
set result [list]
set cmd $args
set cmd [regsub -all {(^\s*\{)||(\}\s*$)} $cmd ""]
set cmd [string trimleft $cmd]
set o1 [eval $cmd]
#catch {uplevel #0 $cmd} o1
puts "1_$sock ==> $o1"
lappend result $o1
#--------------
puts $sock $cmd
flush $sock
set bl [fblocked $sock]
set file [read $sock]
set bl [fblocked $sock]
puts "Fblocked: $bl"
puts "Output: $file"
puts "2_$Comm::self(csock) ==> $file ==> $bl"
lappend result $file
return $result
}
Here is how I run it.
I call server on 1 of the terminal. It will echo the ip address and the port.
Then I call client using the address and the port above to get back the client socket
Then I call prun on the client shell to get back a pair of values, one with the value of the cmd call on the client, and the other the value of the cmd call on the server. Basically I would like to get the pair of values so I can use them for correlation between the 2 set of data.
Below is the code:
1.
On server shell
$ server
2.
On client shell
$ set s [client $addr $port]
3.
Call a proc to get the value from the client shell, then send the command to the server to get the value from the server shell, and return that value back to the client.
$ set res [prun $s {set val [get_attribute [get_nets mynet] pin_capacitance_max]}]
You wrote:
puts "2_$Comm::self(csock) ==> $file ==> $bl"
and defined self with variable. Are you working with packages?. May be you forgot something related to it.
For test you can use just global but using arrays would be a little more complicated.
I'm attempting to run a shell script from TCL. I'm having a bit of trouble though as it's not working or giving me an error to troubleshoot. I'm pretty sure my issue is coming from not having "run" formatted properly. Any help is appreciated.
set run "sshpass -p 'password' ssh user#ip 'bash -s' <"
set sh "test.sh"
set cmd [list $run $sh $arg1 $arg2]
if {[catch {eval [linsert $cmd 0 "exec"]} status]} {
foreach line [split $status "\n"] {
if {[string match *text* $line]} {
//do something
}
}
}
Ended up removing the run variable and adding it directly. Works fine now.
set sh "test.sh"
set cmd [list sshpass -p 'password' ssh user#ip 'bash -s' \< $sh $arg1 $arg2]
if {[catch {eval [linsert $cmd 0 "exec"]} status]} {
foreach line [split $status "\n"] {
if {[string match *text* $line]} {
//do something
}
}
}
If the server went into a infinite loop, how we can close the server connection after sometime?
Here is the code in which I am trying:
set s [socket $host $port]
fconfigure $s -blocking 1 -buffering line
after 2000 set end 1
vwait end
if { $s != "" } {
puts -nonewline $s "$msg\n.\n"
flush $s
fileevent $s readable [set answer [read $s]]
puts "$answer"
if {[catch {close $s}]} {
puts "Server hanged"
}
This above code is working if the answer was given by the server without any problem. If the server went into infinite loop, it is keep on hanging in read $s. Please help on how to handle this read socket in a non-blocking mode as like in fconfigure.
If you're using blocking sockets, you'll have this problem: putting the channel in non-blocking mode is the fix (together with using after to write a timeout). This does mean that you'll have to deal with all the complexity of asynchronous programming, but that's the tradeoff that you need here.
The two places where things can hang are in connection establishment and in production of the data. You would therefore use asynchronous connection and non-blocking retrieval.
set s [socket -async $host $port]
fconfigure $s -blocking 0
fileevent $s writeable [list connected $s]
proc connected {s} {
global msg
fileevent $s writeable {}
puts -nonewline $s "$msg\n.\n"
flush $s
fileevent $s readable [list accumulateBytes $s]
}
set accumulate ""
proc accumulateBytes {s} {
global accumulate end
append accumulate [read $s]
if {[eof $s]} {
set end 0
}
}
# Longer *total* time to connect and communicate.
after 5000 set end 1
vwait end
catch {close $s}
if {$end} {puts "timed out"}
puts "received message: $accumulate"
I am learning TCL and trying to modify the echo server within my book to display the number of currently connected clients and also then display the number of request from all clients.
proc Echo_server {port} {
global echo
set echo(main) [socket -server EchoAccept $port]
}
proc EchoAccept {sock addr port} {
global echo
puts "Accept $sock from $addr port $port"
set echo(addr,$sock) [list $addr $port]
fconfigure $sock -buffering line
fileevent $sock readable [list Echo $sock]
}
proc Echo {sock} {
global echo
if {[eof $sock] || [catch {gets $sock line}]} {
# end of file or abnormal connection drop
close $sock
puts "Close $echo(addr,$sock)"
unset echo(addr,$sock)
} else {
if {[string compare $line "quit"] ==0} {
# Prevent new connections.
# Existing connections stay open.
close $echo(main)
} else {
if {[string compare $line "countconnections"] ==0} {
puts $sock
}
puts $sock $line
}
}
I modified the above by adding :
if {[string compare $line "countconnections"] ==0} {
puts $sock
How can I display the number of connected clients and then the amount of requests received?
Edit:
bad option "keys": must be anymore, donesearch, exists, get, names, nextelement, set, size, startsearch, statistics, or unset
while executing
"array keys echo"
proc Echo {sock} {
global echo num_req
if {[eof $sock] || [catch {gets $sock line}]} {
# end of file or abnormal connection drop
close $sock
puts "Close $echo(addr,$sock)"
unset echo(addr,$sock)
return
}
incr num_req
switch -exact $line {
"quit" {
# Prevent new connections.
# Existing connections stay open.
close $echo(main)
}
"countconnections" {
puts $sock [llength [array names echo]] ;# number of connections
puts $sock $num_req ;# number of requests
}
default {
puts $sock $line
}
}
}