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