:TITLE: Channel I/O: socket, fileevent, vwait ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl40.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: Tcl I/O is based on a channel. A channel is conceptually similar to the FILE * in C, or a stream in shell programming. The difference is that a channel may be a either a stream device like a file, tape drive, or tty, or a connection oriented construct like a socket.
A stream based channel is created with the open command, as discussed in lesson 26. A socket based channel is created with a socket command. A socket can be opened either as a TCP client, or as a server.
When a channel exists, a handler can be defined that will be invoked when the channel is available for reading or writing. This handler is defined with the fileevent command.
Finally, there is a command to wait until an event happens. The vwait command will wait until a variable is set. This can be used to create a semaphore style functionality for the interaction between client and server.
To connect to the local host, use the address 127.0.0.1 (the loopback address).
A stream based channel is created with the open command, as discussed in lesson 26. A socket based channel is created with a socket command. A socket can be opened either as a TCP client, or as a server.
If a channel is opened as a server, then the tcl program will 'listen' on that channel for another task to attempt to connect with it. When this happens, a new channel is created for that link (server-> new client), and the tcl program continues to listen for connections on the original port number. In this way, a single Tcl server could be talking to several clients simultaneously.
When a channel exists, a handler can be defined that will be invoked when the channel is available for reading or writing. This handler is defined with the fileevent command. When a tcl procedure does a gets or puts to a blocking device, and the device isn't ready for I/O, the program will block until the device is ready. This may be a long while if the other end of the I/O channel has gone off line. Using the fileevent command, the program only accesses an I/O channel when it is ready to move data.
Finally, there is a command to wait until an event happens. The vwait command will wait until a variable is set. This can be used to create a semaphore style functionality for the interaction between client and server, and let a controlling procedure know that an event has occurred.
Look at the example, and you'll see the socket command being used as both client and server, and the fileevent and vwait commands being used to control the I/O between the client and server.
Note in particular the flush commands being used. Just as a channel that is opened as a pipe to a command doesn't send data until either a flush is invoked, or a buffer is filled, the socket based channels don't automaticly send data.
To connect to the local host, use the address 127.0.0.1 (the loopback address).
Tcl can open many kinds of channels. It can open the simple stream type I/O channels as was discussed in lesson 26, and it can open channels to other machines on a network using the socket command.
Sockets come in two flavors: there are server sockets that wait for someone to connect to them, and then process the input (like an NNTP news server, or an SMTP mail server), and there are client sockets that connect to the server socket and request to have it perform an action. Tcl can open socket I/O channels as either servers or clients .
If a channel is opened as a server, then the tcl program will 'listen' on that channel for another task to attempt to connect with it. When this happens, a new channel is created for that link (server-> new client), and the tcl program continues to listen for connections on the original port number. In this way, a single Tcl server could be talking to several clients simultaneously.
Once a channel exists, a handler can be defined that will be invoked when the channel is available for reading or writing. This handler is defined with the fileevent command.
When a tcl procedure does a gets or puts to a blocking device (for instance, doing a gets to the user's keyboard), and the device isn't ready for I/O (ie: the user hasn't typed anything yet), the program will block until the device is ready. This may be a long while if the other end of the I/O channel has gone off line. Using the fileevent command, the program only accesses an I/O channel when it is ready to move data.
Finally, there is a command to wait until an event happens. The vwait command will wait until a variable is set. This can be used to provide semaphore functionality, or to let a controlling procedure know that an event has occurred.
Look at the example, and you'll see the socket command being used as both client and server, and the fileevent and vwait commands being used to control the I/O between the client and server.
Note in particular the flush commands being used. Just as a channel that is opened as a pipe to a command doesn't send data until either a flush is invoked, or a buffer is filled, the socket based channels don't automaticly send data.
The example defines two procs to handle events. One of these is the serverOpen proc, which will be called when the server receives a connection. The other is a readLine proc, that is declared to be the input-available event handler by the fileevent command within serverOpen.
When the code executes the line puts $sock "A Test Line", it sends the line to the server socket. Receiving this input causes the execution to automaticly go to the readLine proc, which reads the line from the server socket.
The code after the puts calls a vwait to wait until it knows that the line of data has been processed. When readLine has finished reading and displaying the line of data, it sends a reply to the client socket, and sets didRead. This lets the main line code know it can continue processing, and gets the reply.
To connect to the local host, use the address 127.0.0.1 (the loopback address).