Subject: Re: socket in TCL - DN [1]


Volker Hetzer <volker.hetzer@ieee.org> - 28 Jun 2000 - comp.lang.tcl

 Liu Tak Sun wrote:
 >
 > Hi :-)
 >
 > How can I set up a server socket and client socket with async. events
 > handiling
 > with TCL in a same machine?
 >     i.e. similiar to "select" function in Unix soket programming?
 You can't. You do it much simpler.
 You open a socket (see the doc for tcl's "socket" command) and then
 attach the handler directly to it. Whenever something arrives at the
 socket, your handler will be called automatically.
 Here's a short introduction:

 socket -server -myaddr localhost -myport 1025 -command MyAcceptCommand

 MyAcceptCommand is a procedure you provide. It gets called whenever
 some client does a "connect" to port 1025. It gets called with three
 parameters. A file handle (i.e. the newly created client specific
 socket handle), the clients address and its port number.

 proc MyAcceptCommand {NewFileHandle Clientaddress Clientport} {
 fileevent readable {<enter the code that serves the connection>}
 }

 Fileevent installs the callback that executes your code whenever something
 arrives from the client. Or whren the client colses the connection.

 > Also beside the while {1} that can cause the program to loop forever,
 > is there any other better solution to save system resources with the program
 > act as a demand and response to the read / write events in socket?
 Of course. See above. You install handlers on all ports/sockets
 you want to listen to and then, at the end of your tcl script you write
 "vwait Dummyvariable". At this point tcl will enter the event loop,
 do its internal select stuff and wakes up your handlers accordingly.

 Greetings!
 Volker
 --
 The early bird gets the worm. If you want something else for
 breakfast, get up later.

Last modified
2000-07-20

(195.108.246.52)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated