| 1 | '\" |
| 2 | '\" Copyright (c) 1996 Sun Microsystems, Inc. |
| 3 | '\" Copyright (c) 1998-1999 by Scriptics Corporation. |
| 4 | '\" |
| 5 | '\" See the file "license.terms" for information on usage and redistribution |
| 6 | '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 | '\" |
| 8 | '\" RCS: @(#) $Id: socket.n,v 1.7.2.3 2004/10/27 14:23:58 dkf Exp $ |
| 9 | '\" The definitions below are for supplemental macros used in Tcl/Tk |
| 10 | '\" manual entries. |
| 11 | '\" |
| 12 | '\" .AP type name in/out ?indent? |
| 13 | '\" Start paragraph describing an argument to a library procedure. |
| 14 | '\" type is type of argument (int, etc.), in/out is either "in", "out", |
| 15 | '\" or "in/out" to describe whether procedure reads or modifies arg, |
| 16 | '\" and indent is equivalent to second arg of .IP (shouldn't ever be |
| 17 | '\" needed; use .AS below instead) |
| 18 | '\" |
| 19 | '\" .AS ?type? ?name? |
| 20 | '\" Give maximum sizes of arguments for setting tab stops. Type and |
| 21 | '\" name are examples of largest possible arguments that will be passed |
| 22 | '\" to .AP later. If args are omitted, default tab stops are used. |
| 23 | '\" |
| 24 | '\" .BS |
| 25 | '\" Start box enclosure. From here until next .BE, everything will be |
| 26 | '\" enclosed in one large box. |
| 27 | '\" |
| 28 | '\" .BE |
| 29 | '\" End of box enclosure. |
| 30 | '\" |
| 31 | '\" .CS |
| 32 | '\" Begin code excerpt. |
| 33 | '\" |
| 34 | '\" .CE |
| 35 | '\" End code excerpt. |
| 36 | '\" |
| 37 | '\" .VS ?version? ?br? |
| 38 | '\" Begin vertical sidebar, for use in marking newly-changed parts |
| 39 | '\" of man pages. The first argument is ignored and used for recording |
| 40 | '\" the version when the .VS was added, so that the sidebars can be |
| 41 | '\" found and removed when they reach a certain age. If another argument |
| 42 | '\" is present, then a line break is forced before starting the sidebar. |
| 43 | '\" |
| 44 | '\" .VE |
| 45 | '\" End of vertical sidebar. |
| 46 | '\" |
| 47 | '\" .DS |
| 48 | '\" Begin an indented unfilled display. |
| 49 | '\" |
| 50 | '\" .DE |
| 51 | '\" End of indented unfilled display. |
| 52 | '\" |
| 53 | '\" .SO |
| 54 | '\" Start of list of standard options for a Tk widget. The |
| 55 | '\" options follow on successive lines, in four columns separated |
| 56 | '\" by tabs. |
| 57 | '\" |
| 58 | '\" .SE |
| 59 | '\" End of list of standard options for a Tk widget. |
| 60 | '\" |
| 61 | '\" .OP cmdName dbName dbClass |
| 62 | '\" Start of description of a specific option. cmdName gives the |
| 63 | '\" option's name as specified in the class command, dbName gives |
| 64 | '\" the option's name in the option database, and dbClass gives |
| 65 | '\" the option's class in the option database. |
| 66 | '\" |
| 67 | '\" .UL arg1 arg2 |
| 68 | '\" Print arg1 underlined, then print arg2 normally. |
| 69 | '\" |
| 70 | '\" RCS: @(#) $Id: man.macros,v 1.4 2000/08/25 06:18:32 ericm Exp $ |
| 71 | '\" |
| 72 | '\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages. |
| 73 | .if t .wh -1.3i ^B |
| 74 | .nr ^l \n(.l |
| 75 | .ad b |
| 76 | '\" # Start an argument description |
| 77 | .de AP |
| 78 | .ie !"\\$4"" .TP \\$4 |
| 79 | .el \{\ |
| 80 | . ie !"\\$2"" .TP \\n()Cu |
| 81 | . el .TP 15 |
| 82 | .\} |
| 83 | .ta \\n()Au \\n()Bu |
| 84 | .ie !"\\$3"" \{\ |
| 85 | \&\\$1 \\fI\\$2\\fP (\\$3) |
| 86 | .\".b |
| 87 | .\} |
| 88 | .el \{\ |
| 89 | .br |
| 90 | .ie !"\\$2"" \{\ |
| 91 | \&\\$1 \\fI\\$2\\fP |
| 92 | .\} |
| 93 | .el \{\ |
| 94 | \&\\fI\\$1\\fP |
| 95 | .\} |
| 96 | .\} |
| 97 | .. |
| 98 | '\" # define tabbing values for .AP |
| 99 | .de AS |
| 100 | .nr )A 10n |
| 101 | .if !"\\$1"" .nr )A \\w'\\$1'u+3n |
| 102 | .nr )B \\n()Au+15n |
| 103 | .\" |
| 104 | .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n |
| 105 | .nr )C \\n()Bu+\\w'(in/out)'u+2n |
| 106 | .. |
| 107 | .AS Tcl_Interp Tcl_CreateInterp in/out |
| 108 | '\" # BS - start boxed text |
| 109 | '\" # ^y = starting y location |
| 110 | '\" # ^b = 1 |
| 111 | .de BS |
| 112 | .br |
| 113 | .mk ^y |
| 114 | .nr ^b 1u |
| 115 | .if n .nf |
| 116 | .if n .ti 0 |
| 117 | .if n \l'\\n(.lu\(ul' |
| 118 | .if n .fi |
| 119 | .. |
| 120 | '\" # BE - end boxed text (draw box now) |
| 121 | .de BE |
| 122 | .nf |
| 123 | .ti 0 |
| 124 | .mk ^t |
| 125 | .ie n \l'\\n(^lu\(ul' |
| 126 | .el \{\ |
| 127 | .\" Draw four-sided box normally, but don't draw top of |
| 128 | .\" box if the box started on an earlier page. |
| 129 | .ie !\\n(^b-1 \{\ |
| 130 | \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' |
| 131 | .\} |
| 132 | .el \}\ |
| 133 | \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' |
| 134 | .\} |
| 135 | .\} |
| 136 | .fi |
| 137 | .br |
| 138 | .nr ^b 0 |
| 139 | .. |
| 140 | '\" # VS - start vertical sidebar |
| 141 | '\" # ^Y = starting y location |
| 142 | '\" # ^v = 1 (for troff; for nroff this doesn't matter) |
| 143 | .de VS |
| 144 | .if !"\\$2"" .br |
| 145 | .mk ^Y |
| 146 | .ie n 'mc \s12\(br\s0 |
| 147 | .el .nr ^v 1u |
| 148 | .. |
| 149 | '\" # VE - end of vertical sidebar |
| 150 | .de VE |
| 151 | .ie n 'mc |
| 152 | .el \{\ |
| 153 | .ev 2 |
| 154 | .nf |
| 155 | .ti 0 |
| 156 | .mk ^t |
| 157 | \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n' |
| 158 | .sp -1 |
| 159 | .fi |
| 160 | .ev |
| 161 | .\} |
| 162 | .nr ^v 0 |
| 163 | .. |
| 164 | '\" # Special macro to handle page bottom: finish off current |
| 165 | '\" # box/sidebar if in box/sidebar mode, then invoked standard |
| 166 | '\" # page bottom macro. |
| 167 | .de ^B |
| 168 | .ev 2 |
| 169 | 'ti 0 |
| 170 | 'nf |
| 171 | .mk ^t |
| 172 | .if \\n(^b \{\ |
| 173 | .\" Draw three-sided box if this is the box's first page, |
| 174 | .\" draw two sides but no top otherwise. |
| 175 | .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c |
| 176 | .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c |
| 177 | .\} |
| 178 | .if \\n(^v \{\ |
| 179 | .nr ^x \\n(^tu+1v-\\n(^Yu |
| 180 | \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c |
| 181 | .\} |
| 182 | .bp |
| 183 | 'fi |
| 184 | .ev |
| 185 | .if \\n(^b \{\ |
| 186 | .mk ^y |
| 187 | .nr ^b 2 |
| 188 | .\} |
| 189 | .if \\n(^v \{\ |
| 190 | .mk ^Y |
| 191 | .\} |
| 192 | .. |
| 193 | '\" # DS - begin display |
| 194 | .de DS |
| 195 | .RS |
| 196 | .nf |
| 197 | .sp |
| 198 | .. |
| 199 | '\" # DE - end display |
| 200 | .de DE |
| 201 | .fi |
| 202 | .RE |
| 203 | .sp |
| 204 | .. |
| 205 | '\" # SO - start of list of standard options |
| 206 | .de SO |
| 207 | .SH "STANDARD OPTIONS" |
| 208 | .LP |
| 209 | .nf |
| 210 | .ta 5.5c 11c |
| 211 | .ft B |
| 212 | .. |
| 213 | '\" # SE - end of list of standard options |
| 214 | .de SE |
| 215 | .fi |
| 216 | .ft R |
| 217 | .LP |
| 218 | See the \\fBoptions\\fR manual entry for details on the standard options. |
| 219 | .. |
| 220 | '\" # OP - start of full description for a single option |
| 221 | .de OP |
| 222 | .LP |
| 223 | .nf |
| 224 | .ta 4c |
| 225 | Command-Line Name: \\fB\\$1\\fR |
| 226 | Database Name: \\fB\\$2\\fR |
| 227 | Database Class: \\fB\\$3\\fR |
| 228 | .fi |
| 229 | .IP |
| 230 | .. |
| 231 | '\" # CS - begin code excerpt |
| 232 | .de CS |
| 233 | .RS |
| 234 | .nf |
| 235 | .ta .25i .5i .75i 1i |
| 236 | .. |
| 237 | '\" # CE - end code excerpt |
| 238 | .de CE |
| 239 | .fi |
| 240 | .RE |
| 241 | .. |
| 242 | .de UL |
| 243 | \\$1\l'|0\(ul'\\$2 |
| 244 | .. |
| 245 | .TH socket n 8.0 Tcl "Tcl Built-In Commands" |
| 246 | .BS |
| 247 | '\" Note: do not modify the .SH NAME line immediately below! |
| 248 | .SH NAME |
| 249 | socket \- Open a TCP network connection |
| 250 | .SH SYNOPSIS |
| 251 | .sp |
| 252 | \fBsocket \fR?\fIoptions\fR? \fIhost port\fR |
| 253 | .sp |
| 254 | \fBsocket\fR \fB\-server \fIcommand\fR ?\fIoptions\fR? \fIport\fR |
| 255 | .BE |
| 256 | |
| 257 | .SH DESCRIPTION |
| 258 | .PP |
| 259 | This command opens a network socket and returns a channel |
| 260 | identifier that may be used in future invocations of commands like |
| 261 | \fBread\fR, \fBputs\fR and \fBflush\fR. |
| 262 | At present only the TCP network protocol is supported; future |
| 263 | releases may include support for additional protocols. |
| 264 | The \fBsocket\fR command may be used to open either the client or |
| 265 | server side of a connection, depending on whether the \fB\-server\fR |
| 266 | switch is specified. |
| 267 | .PP |
| 268 | Note that the default encoding for \fIall\fR sockets is the system |
| 269 | encoding, as returned by \fBencoding system\fR. Most of the time, you |
| 270 | will need to use \fBfconfigure\fR to alter this to something else, |
| 271 | such as \fIutf\-8\fR (ideal for communicating with other Tcl |
| 272 | processes) or \fIiso8859\-1\fR (useful for many network protocols, |
| 273 | especially the older ones). |
| 274 | .SH "CLIENT SOCKETS" |
| 275 | .PP |
| 276 | If the \fB\-server\fR option is not specified, then the client side of a |
| 277 | connection is opened and the command returns a channel identifier |
| 278 | that can be used for both reading and writing. |
| 279 | \fIPort\fR and \fIhost\fR specify a port |
| 280 | to connect to; there must be a server accepting connections on |
| 281 | this port. \fIPort\fR is an integer port number |
| 282 | (or service name, where supported and understood by the host operating |
| 283 | system) and \fIhost\fR |
| 284 | is either a domain-style name such as \fBwww.tcl.tk\fR or |
| 285 | a numerical IP address such as \fB127.0.0.1\fR. |
| 286 | Use \fIlocalhost\fR to refer to the host on which the command is invoked. |
| 287 | .PP |
| 288 | The following options may also be present before \fIhost\fR |
| 289 | to specify additional information about the connection: |
| 290 | .TP |
| 291 | \fB\-myaddr\fI addr\fR |
| 292 | \fIAddr\fR gives the domain-style name or numerical IP address of |
| 293 | the client-side network interface to use for the connection. |
| 294 | This option may be useful if the client machine has multiple network |
| 295 | interfaces. If the option is omitted then the client-side interface |
| 296 | will be chosen by the system software. |
| 297 | .TP |
| 298 | \fB\-myport\fI port\fR |
| 299 | \fIPort\fR specifies an integer port number (or service name, where |
| 300 | supported and understood by the host operating system) to use for the |
| 301 | client's |
| 302 | side of the connection. If this option is omitted, the client's |
| 303 | port number will be chosen at random by the system software. |
| 304 | .TP |
| 305 | \fB\-async\fR |
| 306 | The \fB\-async\fR option will cause the client socket to be connected |
| 307 | asynchronously. This means that the socket will be created immediately but |
| 308 | may not yet be connected to the server, when the call to \fBsocket\fR |
| 309 | returns. When a \fBgets\fR or \fBflush\fR is done on the socket before the |
| 310 | connection attempt succeeds or fails, if the socket is in blocking mode, the |
| 311 | operation will wait until the connection is completed or fails. If the |
| 312 | socket is in nonblocking mode and a \fBgets\fR or \fBflush\fR is done on |
| 313 | the socket before the connection attempt succeeds or fails, the operation |
| 314 | returns immediately and \fBfblocked\fR on the socket returns 1. |
| 315 | .SH "SERVER SOCKETS" |
| 316 | .PP |
| 317 | If the \fB\-server\fR option is specified then the new socket |
| 318 | will be a server for the port given by \fIport\fR (either an integer |
| 319 | or a service name, where supported and understood by the host |
| 320 | operating system; if \fIport\fR is zero, the operating system will |
| 321 | allocate a free port to the server socket which may be discovered by |
| 322 | using \fBfconfigure\fR to read the \fB\-sockname\fR option). |
| 323 | Tcl will automatically accept connections to the given port. |
| 324 | For each connection Tcl will create a new channel that may be used to |
| 325 | communicate with the client. Tcl then invokes \fIcommand\fR |
| 326 | with three additional arguments: the name of the new channel, the |
| 327 | address, in network address notation, of the client's host, and |
| 328 | the client's port number. |
| 329 | .PP |
| 330 | The following additional option may also be specified before \fIhost\fR: |
| 331 | .TP |
| 332 | \fB\-myaddr\fI addr\fR |
| 333 | \fIAddr\fR gives the domain-style name or numerical IP address of |
| 334 | the server-side network interface to use for the connection. |
| 335 | This option may be useful if the server machine has multiple network |
| 336 | interfaces. If the option is omitted then the server socket is bound |
| 337 | to the special address INADDR_ANY so that it can accept connections from |
| 338 | any interface. |
| 339 | .PP |
| 340 | Server channels cannot be used for input or output; their sole use is to |
| 341 | accept new client connections. The channels created for each incoming |
| 342 | client connection are opened for input and output. Closing the server |
| 343 | channel shuts down the server so that no new connections will be |
| 344 | accepted; however, existing connections will be unaffected. |
| 345 | .PP |
| 346 | Server sockets depend on the Tcl event mechanism to find out when |
| 347 | new connections are opened. If the application doesn't enter the |
| 348 | event loop, for example by invoking the \fBvwait\fR command or |
| 349 | calling the C procedure \fBTcl_DoOneEvent\fR, then no connections |
| 350 | will be accepted. |
| 351 | .PP |
| 352 | If \fIport\fR is specified as zero, the operating system will allocate |
| 353 | an unused port for use as a server socket. The port number actually |
| 354 | allocated may be retrieved from the created server socket using the |
| 355 | \fBfconfigure\fR command to retrieve the \fB\-sockname\fR option as |
| 356 | described below. |
| 357 | .SH "CONFIGURATION OPTIONS" |
| 358 | The \fBfconfigure\fR command can be used to query several readonly |
| 359 | configuration options for socket channels: |
| 360 | .TP |
| 361 | \fB\-error\fR |
| 362 | This option gets the current error status of the given socket. This |
| 363 | is useful when you need to determine if an asynchronous connect |
| 364 | operation succeeded. If there was an error, the error message is |
| 365 | returned. If there was no error, an empty string is returned. |
| 366 | .TP |
| 367 | \fB\-sockname\fR |
| 368 | This option returns a list of three elements, the address, the host name |
| 369 | and the port number for the socket. If the host name cannot be computed, |
| 370 | the second element is identical to the address, the first element of the |
| 371 | list. |
| 372 | .TP |
| 373 | \fB\-peername\fR |
| 374 | This option is not supported by server sockets. For client and accepted |
| 375 | sockets, this option returns a list of three elements; these are the |
| 376 | address, the host name and the port to which the peer socket is connected |
| 377 | or bound. If the host name cannot be computed, the second element of the |
| 378 | list is identical to the address, its first element. |
| 379 | .PP |
| 380 | .SH "EXAMPLES" |
| 381 | Here is a very simple time server: |
| 382 | .CS |
| 383 | proc Server {channel clientaddr clientport} { |
| 384 | puts "Connection from $clientaddr registered" |
| 385 | puts $channel [clock format [clock seconds]] |
| 386 | close $channel |
| 387 | } |
| 388 | |
| 389 | \fBsocket\fR -server Server 9900 |
| 390 | vwait forever |
| 391 | .CE |
| 392 | .PP |
| 393 | And here is the corresponding client to talk to the server: |
| 394 | .CS |
| 395 | set server localhost |
| 396 | set sockChan [\fBsocket\fR $server 9900] |
| 397 | gets $sockChan line |
| 398 | close $sockChan |
| 399 | puts "The time on $server is $line" |
| 400 | .CE |
| 401 | |
| 402 | .SH "SEE ALSO" |
| 403 | fconfigure(n), flush(n), open(n), read(n) |
| 404 | |
| 405 | .SH KEYWORDS |
| 406 | bind, channel, connection, domain name, host, network address, socket, tcp |