Book Image

Tcl 8.5 Network Programming

Book Image

Tcl 8.5 Network Programming

Overview of this book

Tcl (Tool Command Language) is a very powerful and easy to learn dynamic programming language, suitable for a very wide range of uses. Tcl is regarded as one of the best-kept secrets in the software industry. This book gives you a hands-on experience on Tcl, helping you develop network-aware applications using this mature yet evolving language. This book shows you how to create network-aware applications with Tcl language. Packed with practical examples, the book not only takes you through the implementation of network protocols in Tcl, but also key aspects of Tcl programming. The book starts with the basic element of Tcl programming as we take a look at the syntax and fundamental commands of the language. To get us ready for network programming, we look at important Tcl features such as object-oriented programming, accessing files, packaging in TCL, event driven programming, and multithreaded applications. To create standalone single-file executable applications with Tcl we take a look at the Starpack technology, and ensure that we’ll be able to create robust applications with a thorough coverage of troubleshooting and debugging Tcl applications. The book is really about network programming, and it will not let you down with its deep coverage of these topics. Of course we look at protocols, but there are plenty of practical examples to keep things moving along. We start with the TCP and UDP protocols, and look at some other protocols to see examples of synchronizing time with other servers, querying user information and authenticating users over LDAP and performing DNS queries. The book explains Simple Network Management Protocol (SNMP), which is often used for monitoring and gathering information from various devices, such as routers, gateways, printers and many other types of equipment. We’ll also look at web programming in Tcl; processing the requests coming from the clients via the HTTP protocol and responding to these requests. You’ll be able to create a complete solution for creating a client-server application in Tcl. To round things off, you’ll see how to secure your networked applications, build public key infrastructure into your application and use Tcl’s safe interpreter feature to reduce risk of running code from unknown source.
Table of Contents (18 chapters)
Tcl 8.5 Network Programming
Credits
About the Authors
About the Reviewers
Preface
4
Troubleshooting Tcl applications

Installation


Source releases for Tcl/Tk along with configuration, compilation, and installation instructions are available at http://www.tcl.tk/software/tcltk/download.html, so everyone can compile the interpreter for themselves. Typically, you don't want to waste your time doing this, especially when someone has done it before, and the fastest and easiest way is to get precompiled binaries for your platform. Among the various distributions of Tcl, ActiveTcl is considered as one of the best. It is available at ActiveState's site http://www.activestate.com/activetcl/. Not only does it contain Tcl and Tk, but it also offers a large number of extensions. The unique strength of the ActiveTcl distribution is that it covers a wide range of enterprise-level operating systems by offering binaries for Windows, Linux, Mac OS X, Solaris, and HP-UX. In this chapter, we will focus on installation under Windows XP and Ubuntu Linux. As almost every Linux distribution comes with its own set of Tcl binaries, users of these systems usually have more choices.

At the time of writing this book, the latest stable release of ActiveTcl is 8.5.7.0.

Windows

In the case of Microsoft Windows, ActiveTcl is substantially the only choice. Installation is simple; the installer is distributed in the form of an executable file (named as something like ActiveTcl8.5.7.0.290198-win32-ix86-threaded.exe). Once you download and run it, you will be presented with a graphical installation wizard similar to the one shown in the following screenshot:

On subsequent screens, you will be presented with a detailed ActiveTcl License. You will also be able to specify installation directory—associate the application with the .tcl extension and configure some of the other details. For the rest of this chapter, we will assume that Tcl is installed in the C:\Tcl directory.

The installer updates system variable PATH with the path to the directory where Tcl binaries are located, that is C:\Tcl\bin. It allows you to execute commands like tclsh85.exe anywhere, without specifying the directory's full path. Note that, as usual, you are advised against using a path containing whitespaces.

Unix-like systems

We will consider Unix-compatible systems using Ubuntu 9.04 Linux as a popular, easy-to-get example. In the case of these systems, you have more choices than you do with Windows. One of the choices you have is to install ActiveTcl (there is a good chance it supports your platform), another is to search for a precompiled Tcl distribution. Ubuntu, for example, comes with a significant number of optional packages of additional software prepared by the community. Among them are of course binary compilations of Tcl. It is possible that your system already has the Tcl distribution installed, but even if not, it is extremely easy to install it, because Ubuntu will be able to suggest what packages should be installed based on the commands that these packages provide for the system. The following is a screenshot of such a situation:

The user wants to execute the tclsh command to get Tcl interactive shell. The package that provides this command is not yet installed, and the system informs the user what packages are available. In this case, we have decided to choose tcl8.5 and install it using the command-line command as follows:

sudo apt-get install tcl8.5

Once installed, you can run the tclsh command to get the interactive Tcl shell as follows:

To verify the detailed version of Tcl interpreter which you have just installed, use the info patchlevel command.

Tk comes in a separate Ubuntu package. You will be notified of this if you attempt to run an application that requires it, for example wish. Similarly, it can be installed with:

% sudo apt-get install tk8.5.

Installation of additional Tcl packages

Just like core Tcl installation, extensions, that is Tcl packages, can also be installed in two ways: the first is to get the source code of an extension and build (compile) it, and the second one is to find, download, and install a pre-built package.

In order to be able to use the extension in your script, it must be available for the interpreter. There are several locations where Tcl can search for it, and these locations are kept in the special Tcl variable called auto_path. You can learn its value by starting the Tcl shell and executing the following command:

puts $auto_path

The puts command prints the content of the specified variable to standard output. In the case of Windows, the output appears as:

Now that you know this, you can put the packages you require under the C:\Tcl\lib location to make them available for the interpreter. The concept of the $auto_path variable and other packages will be described in more detail in the next chapter.

ActiveTcl comes with a handy utility called Teacup. This utility allows for easy installation of prebuilt Tcl extensions from external remote repositories called Tcl Extension Archives. These archives, also known as Teapot, are centrally managed sites. Currently, there is one public Teapot maintained by ActiveState. By using Teacup, you can install the package you need into your local repository and then use it in your scripts. It will automatically solve dependencies, localize package(s), and download and install them.