2 fblocked - Test whether the last input operation exhausted all avail-
10 The fblocked command returns 1 if the most recent input operation on
11 channelId returned less information than requested because all avail-
12 able input was exhausted. For example, if gets is invoked when there
13 are only three characters available for input and no end-of-line
14 sequence, gets returns an empty string and a subsequent call to
15 fblocked will return 1.
17 ChannelId must be an identifier for an open channel such as a Tcl stan-
18 dard channel (stdin, stdout, or stderr), the return value from an invo-
19 cation of open or socket, or the result of a channel creation command
20 provided by a Tcl extension.
23 The fblocked command is particularly useful when writing network
24 servers, as it allows you to write your code in a line-by-line style
25 without preventing the servicing of other connections. This can be
26 seen in this simple echo-service:
28 # This is called whenever a new client connects to the server
29 proc connect {chan host port} {
30 set clientName [format <%s:%d> $host $port]
31 puts "connection from $clientName"
32 fconfigure $chan -blocking 0 -buffering line
33 fileevent $chan readable [list echoLine $chan $clientName]
36 # This is called whenever either at least one byte of input
37 # data is available, or the channel was closed by the client.
38 proc echoLine {chan clientName} {
41 puts "finishing connection from $clientName"
43 } elseif {![fblocked $chan]} {
44 # Didn't block waiting for end-of-line
45 puts "$clientName - $line"
50 # Create the server socket and enter the event-loop to wait
51 # for incoming connections...
52 socket -server connect 12345
57 gets(n), open(n), read(n), socket(n), Tcl_StandardChannels(3)