Firstly, socket -server
is its own special thing. It necessarily does a callback when a client connects (the underlying infrastructure of the accept()
system call is hidden from you). There's also not really very much else you can do with a server socket; in particular, you can't read or write on a server socket, as there's no other end connected.
The connected sockets created (which are really just ordinary client sockets except for how they were made) are using whatever file descriptor the OS assigned; Tcl doesn't reassign them. You're strongly recommended to not try to land an accept on any of the standard channels, but passing a connected socket to a subprocess via redirection is supported on POSIX (I don't know if it works on Windows). You'd do that like this:
socket -server spawnSubprocess 12345
proc spawnSubprocess {sock host port} {
puts "connection from ${host}:${port}"
exec [info nameofexecutable] myscript.tcl <@$sock >@$sock 2>@$sock &
# You might want stderr to not go back to the client...
close $sock
}
vwait forever
Note that the subprocess can discover the socket configuration using fconfigure
.
In general, while it is possible to redirect the standard channels, it's very confusing to actually do it. Far better to leave them connected to wherever the calling process told them to go. But redirecting for subprocesses you create is absolutely fine.