« Away or home - which do I prefer? | Main | Wireless hotel tips - FTP and Skype connections failing »

October 26, 2007

Reading from another process in Tcl (pipes and sockets)

If you want to read from another process on the same system that you're running your code on in Tcl can be done using a pipe - you can use open with the command name instead of the file name, preceeded by a pipe character (|) - thus:

set said [open |df r]
 
while {[gets $said this] >= 0} {
  puts $this
}

You can also talk to a process on another system if it's running as a server using sockets. In Tcl all you need to do in the simplest case is to use a socket command instead of an open, and you can use puts and gets on the resource that's returned. Here's an example of a Tcl script that connects to a web server and downloads a page of html:

set talkto [socket 192.168.200.67 80]
 
puts $talkto "GET /index.html HTTP/1.0\n"
flush $talkto
set lines 0
 
while {[gets $talkto stuffing] > -1} {
  incr lines
  puts $stuffing
}
puts $lines

Posted by gje at October 26, 2007 05:48 AM

Comments

Well House Consultants Ltd. Copyright 2010