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

Additional tools


In these days of rapid application development, using plaintext editors like notepad or vi can be really inconvenient and time consuming. Luckily, there is far better alternative— an Integrated Development Environment (IDE). IDEs are tools that allow efficient code creation and editing, support testing your code from within the editor, and usually also offer debugging capabilities.

At the time of writing this book, there are two IDEs worth mentioning: Eclipse and Komodo. Both support syntax highlighting and coloring, auto completion, creating launch configurations, managing projects and dependencies, and have many other useful features.

Eclipse

It is hardly possible that you have not heard about Eclipse nowadays. An open source IDE supporting a wide variety of languages, extensible with plugins is considered as one of the best programming environments, and it is all free. Due to the fact that Eclipse was developed in Java, the CPU/memory requirements may be slightly higher than in case of native applications, but in return, it is available for the Windows, Linux, and Mac platforms.

First you have to install the Java Runtime Environment (JRE), we strongly suggest installing latest version available (at the moment it is version 6 Update 17), you can get it from http://java.com. We will discuss Eclipse Classic 3.5.0 available for download at http://www.eclipse.org/downloads/. The installation process is simple—just download and unzip the file to a location of your choice and run eclipse.exe. The full description of Eclipse is out of scope of this book, we will present only the necessary steps to get it running for Tcl development purposes.

By default, Eclipse supports only Java development, so we have to install an additional extension called the Dynamic Languages Toolkit (DLTK) for Tcl support. In order to do this it, you have to navigate to the Help | Install New Software… menu. A new window will show up, as shown in the following screenshot:

First, select the Galileo site from the list at the top. Eclipse will download a list of all available extensions from this site; this may take a while. Once the list is visible, expand Programming Languages and select Dynamic Languages Toolkit TCL Development Tools. Once you click on Next, Eclipse will automatically resolve dependencies and select all the necessary plugins to install. In this case, it will choose to install the DLTK Core and Eclipse Modelling Framework (EMF) extensions, as shown in the following screenshot:

Following this, Eclipse will download and install all the necessary files, and ask you to restart. Once it is done, you have to configure installed Tcl interpreters. Eclipse requires at least one interpreter in order to be able to run scripts. To configure it, navigate to Window | Preferences to open the preferences dialog, and then go to Tcl | Interpreters. Click the Add… button on this pane, and then Browse… and navigate to the interpreter binary, which is installed at C:\Tcl\bin\tclsh85.exe. Once done, you should have configured the default interpreter like here, as shown in the following screenshot:

Now you have your Eclipse fully configured to create and run Tcl scripts! To get a taste of working with Eclipse, let's create the same Hello World example. Navigate to File | New | Project… and choose Tcl Project from the Tcl category and then create a project named TclHelloWorld—it will be shown in the Script Explorer view (by default, on the left of the Eclipse window). The next step is to right-click on the project name in this view and select Tcl File, and name it main.tcl. As a template, select Tcl File. Eclipse will create the file with a specific header that we will explain later. Add the puts command to that script, just as you did in the previous example. Now it is time to execute the script—right-click on it and select Run As | Tcl Script. The output of this execution will be visible in the Console view. The following screen illustrates it:

Komodo

Komodo, available for download at http://www.activestate.com/komodo, comes in two flavors: free Komodo Edit and paid Komodo IDE. Both support a wide range of scripting languages, including PHP, Ruby, Python, and of course Tcl. The first one is a simple editor with syntax checking and coloring, and also Project Management abilities. The second one extends the offered functionality as it comes with a debugger.

Similar to Eclipse, you can create a project (using File | New | New Project…) and a file inside it (right-click on project name in the left pane and select Add | New file…). The new file is created based on a language-specific template, which comes with the universal header described later in this chapter.

To run the script, navigate to Tools | Run Command… and enter tclsh85 %F in the Run dialog, and then click on the Run button.

tkcon an alternate Tcl shell

If you find the standard Tcl shell too primitive, you must definitely have a look at tkcon—a standard console replacement with many useful features including a command history and syntax highlighting. What is even more interesting is that it is implemented as another Tcl package, and therefore it is perfectly possible to embed tkcon in to your Tcl application. The following is a screenshot of tkcon console, with the puts $auto_path command's output:

Tkcon is, by default, included in the ActiveTcl distribution. To install it on Ubuntu, enter:

% sudo apt-get install tkcon .

In order to run it, simply execute the command tkcon.