Preface
Tcl or Tool Command Language is a very powerful, but easy-to-learn dynamic programming language, suitable for a very wide range of uses. Tcl is a mature yet evolving language used by developers, from one man shops to large corporations. .
It is commonly used for rapid prototyping, scripted applications, GUIs, and testing. Tcl is used on embedded-system platforms, both in its full form and in several other small-footprinted versions. Tcl is also used for CGI scripting and as the scripting language for the Eggdrop bot.
Tcl is popularly used today in many automated test harnesses, both for software and hardware, and has a loyal following in the Network Testing and SQA communities. According to the Tcl website, many have called it the "best-kept secret in the software industry". It is used for rapid development, truly cross-platform GUIs, flexible integration, elegant networking model, great I18N and thread support—all that and it's easy to learn and easy to deploy!
This book will delve into the network programming capabilities of Tcl: one of the best-kept secrets of the software industry.
What this book covers
Chapter 1, Introducing Tcl serves as a quick introduction to the Tcl world. It explains what the Tcl is, how to install it and start using it. It presents some tools for making development easy, such as Eclipse and Komodo. It describes Tcl language basics, such as language syntax, data types, and the important commands. This and the following chapters build a solid base for Tcl programming.
Chapter 2, Advanced Tcl Features focuses on advanced topics. More common commands are introduced. The reader learns how to handle time, namespaces, and use Tcl as an object-oriented programming language. A large part of the chapter is dedicated to file I/O operations. The reader also learns how to create Tcl packages or modules. Event loop and various types of events are also discussed. Threads are introduced in the end.
Chapter 3, Tcl Standalone Binaries teaches you how to break free from the constraint of having the Tcl interpreter installed, and how to build portable, standalone executable applications written in Tcl. You will also learn about the Metakit database concepts and its usage.
Chapter 4, Troubleshooting Tcl Applications focuses on topics related to "fixing" the applications. First, the reader learns how to introduce, configure, and apply logging information to the code. Later, the available tools and ways of debugging both local and remote applications are presented.
Chapter 5, Data Storage discusses topics such as text data storage and processing. Some of these include working with SQL databases (both remote systems such as MySQL and Postgresql, and local ones like SQLite), processing XML data (parsing and generation), handling plaintext data while focusing on common encoding issues.
Chapter 6, Networking in Tcl presents basic information about the TCP/UDP networking abilities of Tcl, from both the client and server sides. Core concepts and commands are discussed extensively along with examples.
Chapter 7, Using Common Internet Services teaches you how to send e-mails from Tcl, build e-mails containing HTML and various kinds of messages. It discusses how to retrieve, parse, and process e-mail messages, and also covers how to get data over HTTP, read RSS feeds, and upload and download files over FTP.
Chapter 8, Using Additional Internet Services introduces the reader to additional Internet protocols: querying information using DNS and querying the current date and time over an NTP service. It also discusses LDAP and how to use it to query information about user registries and how to authorize users. It describes the comm protocol, which can be used to communicate between local and remote Tcl applications. It presents how the protocol can be used and how the comm commands can be made secure.
Chapter 9, Learning SNMP describes SNMP (Simple Network Management Protocol) and its use for management of multiple types of devices such as routers, switches, modems, and servers. It introduces the reader to how data is structured in SNMP by describing MIB (Management Information Base). It describes Scotty package, which provides SNMP functionality to Tcl. Step-by-step instructions are given for retrieving information and retrieving notifications from devices. It also describes how Tcl can be used to create custom SNMP agents using the Scotty package. This chapter also describes ICMP-related features; performing ping and trace routes.
Chapter 10, Web Programming in Tcl introduces Tclhttpd: a Tcl-based web server that can be used standalone and embedded in other applications. Using it for providing static files and web applications is described in detail. Built-in templating system is shown as well. This chapter also gives information on other subjects, using CGI scripts, handling specific types of files from Tcl, authorizing requests, and creating custom template systems.
Chapter 11, TclHttpd in Client-Server Applications provides details on creating HTTP-based client-server applications. It gives the reader a good understanding of how such applications are created, by providing a step-by-step example of building such an application. It also discusses building the client and the server as standalone binaries and providing automatic updates of the client. This example is then extended to show how modules can be added to allow additional code to be deployed to clients on demand.
Chapter 12, SOAP and XML-RPC covers creating and using web services from Tcl. It describes how to invoke methods over XML-RPC, along with an example of how this can be done to automatically add blog posts. SOAP is also described. This chapter shows how to create an SOAP-based web service and how such services can be used from within Tcl.
Chapter 13, SSL and Security focuses on the security aspects of network programming. It shows the reader how to secure communications and ensure that applications or users can authenticate remote systems. Step-by-step instructions are given on how to create a public key infrastructure and securely transfer keys to systems in the network. The chapter also introduces the reader to Tcl's safe interpreter feature and how it can be used to create a role-based security model for running unsecure Tcl code.
What you need for this book
The book is written so that both experienced and novice Tclers can find useful information inside. It starts with a quick introduction to Tcl and its networking support for those who are less familiar with them.
Authors focus on showing practical, yet simple examples for each module and command described so that the reader understands how to use them when solving the real life problems. The examples given are useful programs that try to solve real-world needs. All sample programs are clear and concise, yet nothing essential is left out and the programming style focuses on readability rather than on stylistic rigor or efficiency.
Who this book is for
This book is for Tcl developers who have basic knowledge of network programming concepts, who want to add networking capabilities to their applications. Working knowledge of Tcl and basic experience of network protocols will be useful. The reader should be familiar with basic concepts used in modern networking—keywords such as TCP, HTTP, or XML should not be a mystery. The book does not require advanced knowledge of Tcl the first few chapters will swiftly introduce the readers into it, allowing them to refresh the information or gain a quick overview of the Tcl language abilities.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "Calculations in Tcl are done by using a separate command called expr
"
A block of code is set as follows:
proc createDict {} { set value [dict create] dict set value firstValue 1 dict set value otherValue 2 return $value }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
% puts [clock format [clock scan "12:45 +3 hours"]] Tue Sep 22 15:45:00 CEST 2009
Any command-line input or output is written as follows:
set rows [mk::select myTag.people] printRows myTag.people $rows
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "After a successful installation, we can go to the Administrative Tools folder and run the Services applet.".
Note
Warnings or important notes appear in a box like this.
Note
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to< feedback@packtpub.com>
, and mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail< suggest@packtpub.com>
.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Note
Downloading the example code for the book
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at< copyright@packtpub.com>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
Questions
You can contact us at< questions@packtpub.com>
if you are having a problem with any aspect of the book, and we will do our best to address it.