Server Sockets

A TCP server socket allows multiple clients. The way this works is that the socket command creates a listening socket, and then new sockets are created when clients make connections to the server. Tcl takes care of all the details and makes this easy to use. You simply specify a port number and give the socket command a callback to execute when a client connects to your server socket. The callback is just a Tcl command. A simple example is shown below:

Example 17-2 Opening a server socket.
set listenSocket [socket -server Accept 2540]
proc Accept {newSock addr port} {
   puts "Accepted $newSock from $addr port $port"
}
vwait forever

The Accept command is the callback. With server sockets, Tcl adds additional arguments to the callback ...

Get Practical Programming in Tcl & Tk, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.