| 1 |
'\" |
| 2 |
'\" Copyright (c) 1996 Sun Microsystems, Inc. |
| 3 |
'\" Copyright (c) 1998-1999 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 |
.TH socket n 8.6 Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
socket \- Open a TCP network connection |
| 14 |
.SH SYNOPSIS |
| 15 |
.sp |
| 16 |
\fBsocket \fR?\fIoptions\fR? \fIhost port\fR |
| 17 |
.sp |
| 18 |
\fBsocket\fR \fB\-server \fIcommand\fR ?\fIoptions\fR? \fIport\fR |
| 19 |
.BE |
| 20 |
.SH DESCRIPTION |
| 21 |
.PP |
| 22 |
This command opens a network socket and returns a channel identifier |
| 23 |
that may be used in future invocations of commands like \fBread\fR, |
| 24 |
\fBputs\fR and \fBflush\fR. At present only the TCP network protocol |
| 25 |
is supported over IPv4 and IPv6; future releases may include support |
| 26 |
for additional protocols. The \fBsocket\fR command may be used to |
| 27 |
open either the client or server side of a connection, depending on |
| 28 |
whether the \fB\-server\fR switch is specified. |
| 29 |
.PP |
| 30 |
Note that the default encoding for \fIall\fR sockets is the system |
| 31 |
encoding, as returned by \fBencoding system\fR. Most of the time, you |
| 32 |
will need to use \fBchan configure\fR to alter this to something else, |
| 33 |
such as \fIutf\-8\fR (ideal for communicating with other Tcl |
| 34 |
processes) or \fIiso8859\-1\fR (useful for many network protocols, |
| 35 |
especially the older ones). |
| 36 |
.SH "CLIENT SOCKETS" |
| 37 |
.PP |
| 38 |
If the \fB\-server\fR option is not specified, then the client side of a |
| 39 |
connection is opened and the command returns a channel identifier |
| 40 |
that can be used for both reading and writing. |
| 41 |
\fIPort\fR and \fIhost\fR specify a port |
| 42 |
to connect to; there must be a server accepting connections on |
| 43 |
this port. \fIPort\fR is an integer port number |
| 44 |
(or service name, where supported and understood by the host operating |
| 45 |
system) and \fIhost\fR |
| 46 |
is either a domain-style name such as \fBwww.tcl.tk\fR or |
| 47 |
a numerical IPv4 or IPv6 address such as \fB127.0.0.1\fR or \fB2001:DB8::1\fR. |
| 48 |
Use \fIlocalhost\fR to refer to the host on which the command is invoked. |
| 49 |
.PP |
| 50 |
The following options may also be present before \fIhost\fR |
| 51 |
to specify additional information about the connection: |
| 52 |
.TP |
| 53 |
\fB\-myaddr\fI addr\fR |
| 54 |
. |
| 55 |
\fIAddr\fR gives the domain-style name or numerical IP address of |
| 56 |
the client-side network interface to use for the connection. |
| 57 |
This option may be useful if the client machine has multiple network |
| 58 |
interfaces. If the option is omitted then the client-side interface |
| 59 |
will be chosen by the system software. |
| 60 |
.TP |
| 61 |
\fB\-myport\fI port\fR |
| 62 |
. |
| 63 |
\fIPort\fR specifies an integer port number (or service name, where |
| 64 |
supported and understood by the host operating system) to use for the |
| 65 |
client's |
| 66 |
side of the connection. If this option is omitted, the client's |
| 67 |
port number will be chosen at random by the system software. |
| 68 |
.TP |
| 69 |
\fB\-async\fR |
| 70 |
. |
| 71 |
This option will cause the client socket to be connected |
| 72 |
asynchronously. This means that the socket will be created immediately |
| 73 |
but may not yet be connected to the server, when the call to |
| 74 |
\fBsocket\fR returns. |
| 75 |
.RS |
| 76 |
.PP |
| 77 |
When a \fBgets\fR or \fBflush\fR is done on the socket before the |
| 78 |
connection attempt succeeds or fails, if the socket is in blocking |
| 79 |
mode, the operation will wait until the connection is completed or |
| 80 |
fails. If the socket is in nonblocking mode and a \fBgets\fR or |
| 81 |
\fBflush\fR is done on the socket before the connection attempt |
| 82 |
succeeds or fails, the operation returns immediately and |
| 83 |
\fBfblocked\fR on the socket returns 1. Synchronous client sockets may |
| 84 |
be switched (after they have connected) to operating in asynchronous |
| 85 |
mode using: |
| 86 |
.PP |
| 87 |
.CS |
| 88 |
\fBchan configure \fIchan \fB\-blocking 0\fR |
| 89 |
.CE |
| 90 |
.PP |
| 91 |
See the \fBchan configure\fR command for more details. |
| 92 |
.PP |
| 93 |
The Tcl event loop should be running while an asynchronous connection |
| 94 |
is in progress, because it may have to do several connection attempts |
| 95 |
in the background. Running the event loop also allows you to set up a |
| 96 |
writable channel event on the socket to get notified when the |
| 97 |
asynchronous connection has succeeded or failed. See the \fBvwait\fR |
| 98 |
and the \fBchan\fR commands for more details on the event loop and |
| 99 |
channel events. |
| 100 |
.PP |
| 101 |
The \fBchan configure\fR option \fB-connecting\fR may be used to check if the connect is still running. To verify a successful connect, the option \fB-error\fR may be checked when \fB-connecting\fR returned 0. |
| 102 |
.PP |
| 103 |
Operation without the event queue requires at the moment calls to \fBchan configure\fR to advance the internal state machine. |
| 104 |
.RE |
| 105 |
.SH "SERVER SOCKETS" |
| 106 |
.PP |
| 107 |
If the \fB\-server\fR option is specified then the new socket will be |
| 108 |
a server that listens on the given \fIport\fR (either an integer or a |
| 109 |
service name, where supported and understood by the host operating |
| 110 |
system; if \fIport\fR is zero, the operating system will allocate a |
| 111 |
free port to the server socket which may be discovered by using |
| 112 |
\fBchan configure\fR to read the \fB\-sockname\fR option). If the host |
| 113 |
supports both, IPv4 and IPv6, the socket will listen on both address |
| 114 |
families. Tcl will automatically accept connections to the given port. |
| 115 |
For each connection Tcl will create a new channel that may be used to |
| 116 |
communicate with the client. Tcl then invokes \fIcommand\fR (properly |
| 117 |
a command prefix list, see the \fBEXAMPLES\fR below) with three |
| 118 |
additional arguments: the name of the new channel, the address, in |
| 119 |
network address notation, of the client's host, and the client's port |
| 120 |
number. |
| 121 |
.PP |
| 122 |
The following additional option may also be specified before \fIport\fR: |
| 123 |
.TP |
| 124 |
\fB\-myaddr\fI addr\fR |
| 125 |
. |
| 126 |
\fIAddr\fR gives the domain-style name or numerical IP address of the |
| 127 |
server-side network interface to use for the connection. This option |
| 128 |
may be useful if the server machine has multiple network interfaces. |
| 129 |
If the option is omitted then the server socket is bound to the |
| 130 |
wildcard address so that it can accept connections from any |
| 131 |
interface. If \fIaddr\fR is a domain name that resolves to multiple IP |
| 132 |
addresses that are available on the local machine, the socket will |
| 133 |
listen on all of them. |
| 134 |
.PP |
| 135 |
Server channels cannot be used for input or output; their sole use is to |
| 136 |
accept new client connections. The channels created for each incoming |
| 137 |
client connection are opened for input and output. Closing the server |
| 138 |
channel shuts down the server so that no new connections will be |
| 139 |
accepted; however, existing connections will be unaffected. |
| 140 |
.PP |
| 141 |
Server sockets depend on the Tcl event mechanism to find out when |
| 142 |
new connections are opened. If the application does not enter the |
| 143 |
event loop, for example by invoking the \fBvwait\fR command or |
| 144 |
calling the C procedure \fBTcl_DoOneEvent\fR, then no connections |
| 145 |
will be accepted. |
| 146 |
.PP |
| 147 |
If \fIport\fR is specified as zero, the operating system will allocate |
| 148 |
an unused port for use as a server socket. The port number actually |
| 149 |
allocated may be retrieved from the created server socket using the |
| 150 |
\fBchan configure\fR command to retrieve the \fB\-sockname\fR option as |
| 151 |
described below. |
| 152 |
.SH "CONFIGURATION OPTIONS" |
| 153 |
.PP |
| 154 |
The \fBchan configure\fR command can be used to query several readonly |
| 155 |
configuration options for socket channels: |
| 156 |
.TP |
| 157 |
\fB\-error\fR |
| 158 |
. |
| 159 |
This option gets the current error status of the given socket. This |
| 160 |
is useful when you need to determine if an asynchronous connect |
| 161 |
operation succeeded. If there was an error, the error message is |
| 162 |
returned. If there was no error, an empty string is returned. |
| 163 |
.RS |
| 164 |
.PP |
| 165 |
Note that the error status is reset by the read operation; this mimics |
| 166 |
the underlying getsockopt(SO_ERROR) call. |
| 167 |
.RE |
| 168 |
.TP |
| 169 |
\fB\-sockname\fR |
| 170 |
. |
| 171 |
For client sockets (including the channels that get created when a |
| 172 |
client connects to a server socket) this option returns a list of |
| 173 |
three elements, the address, the host name and the port number for the |
| 174 |
socket. If the host name cannot be computed, the second element is |
| 175 |
identical to the address, the first element of the list. |
| 176 |
.RS |
| 177 |
.PP |
| 178 |
For server sockets this option returns a list of a multiple of three |
| 179 |
elements each group of which have the same meaning as described |
| 180 |
above. The list contains more than one group when the server socket |
| 181 |
was created without \fB\-myaddr\fR or with the argument to |
| 182 |
\fB\-myaddr\fR being a domain name that resolves multiple IP addresses |
| 183 |
that are local to the invoking host. |
| 184 |
.RE |
| 185 |
.TP |
| 186 |
\fB\-peername\fR |
| 187 |
. |
| 188 |
This option is not supported by server sockets. For client and accepted |
| 189 |
sockets, this option returns a list of three elements; these are the |
| 190 |
address, the host name and the port to which the peer socket is connected |
| 191 |
or bound. If the host name cannot be computed, the second element of the |
| 192 |
list is identical to the address, its first element. |
| 193 |
.TP |
| 194 |
\fB\-connecting\fR |
| 195 |
. |
| 196 |
This option is not supported by server sockets. For client sockets, this option returns 1 if an asyncroneous connect is still in progress, 0 otherwise. |
| 197 |
.PP |
| 198 |
.SH "EXAMPLES" |
| 199 |
.PP |
| 200 |
Here is a very simple time server: |
| 201 |
.PP |
| 202 |
.CS |
| 203 |
proc Server {startTime channel clientaddr clientport} { |
| 204 |
puts "Connection from $clientaddr registered" |
| 205 |
set now [clock seconds] |
| 206 |
puts $channel [clock format $now] |
| 207 |
puts $channel "[expr {$now - $startTime}] since start" |
| 208 |
close $channel |
| 209 |
} |
| 210 |
|
| 211 |
\fBsocket -server\fR [list Server [clock seconds]] 9900 |
| 212 |
vwait forever |
| 213 |
.CE |
| 214 |
.PP |
| 215 |
And here is the corresponding client to talk to the server and extract |
| 216 |
some information: |
| 217 |
.PP |
| 218 |
.CS |
| 219 |
set server localhost |
| 220 |
set sockChan [\fBsocket\fR $server 9900] |
| 221 |
gets $sockChan line1 |
| 222 |
gets $sockChan line2 |
| 223 |
close $sockChan |
| 224 |
puts "The time on $server is $line1" |
| 225 |
puts "That is [lindex $line2 0]s since the server started" |
| 226 |
.CE |
| 227 |
.SH "HISTORY" |
| 228 |
Support for IPv6 was added in Tcl 8.6. |
| 229 |
.SH "SEE ALSO" |
| 230 |
chan(n), flush(n), open(n), read(n) |
| 231 |
.SH KEYWORDS |
| 232 |
asynchronous I/O, bind, channel, connection, domain name, host, network address, socket, tcp |
| 233 |
'\" Local Variables: |
| 234 |
'\" mode: nroff |
| 235 |
'\" End: |