| 1 |
'\" |
| 2 |
'\" Copyright (c) 1996 Sun Microsystems, Inc. |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH fblocked n 7.5 Tcl "Tcl Built-In Commands" |
| 8 |
.BS |
| 9 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 10 |
.SH NAME |
| 11 |
fblocked \- Test whether the last input operation exhausted all available input |
| 12 |
.SH SYNOPSIS |
| 13 |
\fBfblocked \fIchannelId\fR |
| 14 |
.BE |
| 15 |
|
| 16 |
.SH DESCRIPTION |
| 17 |
.PP |
| 18 |
The \fBfblocked\fR command returns 1 if the most recent input operation |
| 19 |
on \fIchannelId\fR returned less information than requested because all |
| 20 |
available input was exhausted. |
| 21 |
For example, if \fBgets\fR is invoked when there are only three |
| 22 |
characters available for input and no end-of-line sequence, \fBgets\fR |
| 23 |
returns an empty string and a subsequent call to \fBfblocked\fR will |
| 24 |
return 1. |
| 25 |
.PP |
| 26 |
\fIChannelId\fR must be an identifier for an open channel such as a |
| 27 |
Tcl standard channel (\fBstdin\fR, \fBstdout\fR, or \fBstderr\fR), |
| 28 |
the return value from an invocation of \fBopen\fR or \fBsocket\fR, or |
| 29 |
the result of a channel creation command provided by a Tcl extension. |
| 30 |
.SH EXAMPLE |
| 31 |
The \fBfblocked\fR command is particularly useful when writing network |
| 32 |
servers, as it allows you to write your code in a line-by-line style |
| 33 |
without preventing the servicing of other connections. This can be |
| 34 |
seen in this simple echo-service: |
| 35 |
.PP |
| 36 |
.CS |
| 37 |
# This is called whenever a new client connects to the server |
| 38 |
proc connect {chan host port} { |
| 39 |
set clientName [format <%s:%d> $host $port] |
| 40 |
puts "connection from $clientName" |
| 41 |
fconfigure $chan -blocking 0 -buffering line |
| 42 |
fileevent $chan readable [list echoLine $chan $clientName] |
| 43 |
} |
| 44 |
|
| 45 |
# This is called whenever either at least one byte of input |
| 46 |
# data is available, or the channel was closed by the client. |
| 47 |
proc echoLine {chan clientName} { |
| 48 |
gets $chan line |
| 49 |
if {[eof $chan]} { |
| 50 |
puts "finishing connection from $clientName" |
| 51 |
close $chan |
| 52 |
} elseif {![\fBfblocked\fR $chan]} { |
| 53 |
# Didn't block waiting for end-of-line |
| 54 |
puts "$clientName - $line" |
| 55 |
puts $chan $line |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
# Create the server socket and enter the event-loop to wait |
| 60 |
# for incoming connections... |
| 61 |
socket -server connect 12345 |
| 62 |
vwait forever |
| 63 |
.CE |
| 64 |
.SH "SEE ALSO" |
| 65 |
gets(n), open(n), read(n), socket(n), Tcl_StandardChannels(3) |
| 66 |
.SH KEYWORDS |
| 67 |
blocking, nonblocking |