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.15 2007/12/13 15:22:33 dgp Exp $ |
---|
9 | .so man.macros |
---|
10 | .TH socket n 8.0 Tcl "Tcl Built-In Commands" |
---|
11 | .BS |
---|
12 | '\" Note: do not modify the .SH NAME line immediately below! |
---|
13 | .SH NAME |
---|
14 | socket \- Open a TCP network connection |
---|
15 | .SH SYNOPSIS |
---|
16 | .sp |
---|
17 | \fBsocket \fR?\fIoptions\fR? \fIhost port\fR |
---|
18 | .sp |
---|
19 | \fBsocket\fR \fB\-server \fIcommand\fR ?\fIoptions\fR? \fIport\fR |
---|
20 | .BE |
---|
21 | |
---|
22 | .SH DESCRIPTION |
---|
23 | .PP |
---|
24 | This command opens a network socket and returns a channel |
---|
25 | identifier that may be used in future invocations of commands like |
---|
26 | \fBread\fR, \fBputs\fR and \fBflush\fR. |
---|
27 | At present only the TCP network protocol is supported; future |
---|
28 | releases may include support for additional protocols. |
---|
29 | The \fBsocket\fR command may be used to open either the client or |
---|
30 | server side of a connection, depending on whether the \fB\-server\fR |
---|
31 | switch is specified. |
---|
32 | .PP |
---|
33 | Note that the default encoding for \fIall\fR sockets is the system |
---|
34 | encoding, as returned by \fBencoding system\fR. Most of the time, you |
---|
35 | will need to use \fBfconfigure\fR to alter this to something else, |
---|
36 | such as \fIutf\-8\fR (ideal for communicating with other Tcl |
---|
37 | processes) or \fIiso8859\-1\fR (useful for many network protocols, |
---|
38 | especially the older ones). |
---|
39 | .SH "CLIENT SOCKETS" |
---|
40 | .PP |
---|
41 | If the \fB\-server\fR option is not specified, then the client side of a |
---|
42 | connection is opened and the command returns a channel identifier |
---|
43 | that can be used for both reading and writing. |
---|
44 | \fIPort\fR and \fIhost\fR specify a port |
---|
45 | to connect to; there must be a server accepting connections on |
---|
46 | this port. \fIPort\fR is an integer port number |
---|
47 | (or service name, where supported and understood by the host operating |
---|
48 | system) and \fIhost\fR |
---|
49 | is either a domain-style name such as \fBwww.tcl.tk\fR or |
---|
50 | a numerical IP address such as \fB127.0.0.1\fR. |
---|
51 | Use \fIlocalhost\fR to refer to the host on which the command is invoked. |
---|
52 | .PP |
---|
53 | The following options may also be present before \fIhost\fR |
---|
54 | to specify additional information about the connection: |
---|
55 | .TP |
---|
56 | \fB\-myaddr\fI addr\fR |
---|
57 | \fIAddr\fR gives the domain-style name or numerical IP address of |
---|
58 | the client-side network interface to use for the connection. |
---|
59 | This option may be useful if the client machine has multiple network |
---|
60 | interfaces. If the option is omitted then the client-side interface |
---|
61 | will be chosen by the system software. |
---|
62 | .TP |
---|
63 | \fB\-myport\fI port\fR |
---|
64 | \fIPort\fR specifies an integer port number (or service name, where |
---|
65 | supported and understood by the host operating system) to use for the |
---|
66 | client's |
---|
67 | side of the connection. If this option is omitted, the client's |
---|
68 | port number will be chosen at random by the system software. |
---|
69 | .TP |
---|
70 | \fB\-async\fR |
---|
71 | The \fB\-async\fR option will cause the client socket to be connected |
---|
72 | asynchronously. This means that the socket will be created immediately but |
---|
73 | may not yet be connected to the server, when the call to \fBsocket\fR |
---|
74 | returns. When a \fBgets\fR or \fBflush\fR is done on the socket before the |
---|
75 | connection attempt succeeds or fails, if the socket is in blocking mode, the |
---|
76 | operation will wait until the connection is completed or fails. If the |
---|
77 | socket is in nonblocking mode and a \fBgets\fR or \fBflush\fR is done on |
---|
78 | the socket before the connection attempt succeeds or fails, the operation |
---|
79 | returns immediately and \fBfblocked\fR on the socket returns 1. Synchronous |
---|
80 | client sockets may be switched (after they have connected) to operating in |
---|
81 | asynchronous mode using: |
---|
82 | .RS |
---|
83 | .CS |
---|
84 | \fBfconfigure \fIchan \fB\-blocking 0\fR |
---|
85 | .CE |
---|
86 | .PP |
---|
87 | See the \fBfconfigure\fR command for more details. |
---|
88 | .RE |
---|
89 | .SH "SERVER SOCKETS" |
---|
90 | .PP |
---|
91 | If the \fB\-server\fR option is specified then the new socket |
---|
92 | will be a server for the port given by \fIport\fR (either an integer |
---|
93 | or a service name, where supported and understood by the host |
---|
94 | operating system; if \fIport\fR is zero, the operating system will |
---|
95 | allocate a free port to the server socket which may be discovered by |
---|
96 | using \fBfconfigure\fR to read the \fB\-sockname\fR option). |
---|
97 | Tcl will automatically accept connections to the given port. |
---|
98 | For each connection Tcl will create a new channel that may be used to |
---|
99 | communicate with the client. Tcl then invokes \fIcommand\fR |
---|
100 | with three additional arguments: the name of the new channel, the |
---|
101 | address, in network address notation, of the client's host, and |
---|
102 | the client's port number. |
---|
103 | .PP |
---|
104 | The following additional option may also be specified before \fIhost\fR: |
---|
105 | .TP |
---|
106 | \fB\-myaddr\fI addr\fR |
---|
107 | \fIAddr\fR gives the domain-style name or numerical IP address of |
---|
108 | the server-side network interface to use for the connection. |
---|
109 | This option may be useful if the server machine has multiple network |
---|
110 | interfaces. If the option is omitted then the server socket is bound |
---|
111 | to the special address INADDR_ANY so that it can accept connections from |
---|
112 | any interface. |
---|
113 | .PP |
---|
114 | Server channels cannot be used for input or output; their sole use is to |
---|
115 | accept new client connections. The channels created for each incoming |
---|
116 | client connection are opened for input and output. Closing the server |
---|
117 | channel shuts down the server so that no new connections will be |
---|
118 | accepted; however, existing connections will be unaffected. |
---|
119 | .PP |
---|
120 | Server sockets depend on the Tcl event mechanism to find out when |
---|
121 | new connections are opened. If the application does not enter the |
---|
122 | event loop, for example by invoking the \fBvwait\fR command or |
---|
123 | calling the C procedure \fBTcl_DoOneEvent\fR, then no connections |
---|
124 | will be accepted. |
---|
125 | .PP |
---|
126 | If \fIport\fR is specified as zero, the operating system will allocate |
---|
127 | an unused port for use as a server socket. The port number actually |
---|
128 | allocated may be retrieved from the created server socket using the |
---|
129 | \fBfconfigure\fR command to retrieve the \fB\-sockname\fR option as |
---|
130 | described below. |
---|
131 | .SH "CONFIGURATION OPTIONS" |
---|
132 | The \fBfconfigure\fR command can be used to query several readonly |
---|
133 | configuration options for socket channels: |
---|
134 | .TP |
---|
135 | \fB\-error\fR |
---|
136 | This option gets the current error status of the given socket. This |
---|
137 | is useful when you need to determine if an asynchronous connect |
---|
138 | operation succeeded. If there was an error, the error message is |
---|
139 | returned. If there was no error, an empty string is returned. |
---|
140 | .TP |
---|
141 | \fB\-sockname\fR |
---|
142 | This option returns a list of three elements, the address, the host name |
---|
143 | and the port number for the socket. If the host name cannot be computed, |
---|
144 | the second element is identical to the address, the first element of the |
---|
145 | list. |
---|
146 | .TP |
---|
147 | \fB\-peername\fR |
---|
148 | This option is not supported by server sockets. For client and accepted |
---|
149 | sockets, this option returns a list of three elements; these are the |
---|
150 | address, the host name and the port to which the peer socket is connected |
---|
151 | or bound. If the host name cannot be computed, the second element of the |
---|
152 | list is identical to the address, its first element. |
---|
153 | .PP |
---|
154 | .SH "EXAMPLES" |
---|
155 | Here is a very simple time server: |
---|
156 | .CS |
---|
157 | proc Server {channel clientaddr clientport} { |
---|
158 | puts "Connection from $clientaddr registered" |
---|
159 | puts $channel [clock format [clock seconds]] |
---|
160 | close $channel |
---|
161 | } |
---|
162 | |
---|
163 | \fBsocket\fR -server Server 9900 |
---|
164 | vwait forever |
---|
165 | .CE |
---|
166 | .PP |
---|
167 | And here is the corresponding client to talk to the server: |
---|
168 | .CS |
---|
169 | set server localhost |
---|
170 | set sockChan [\fBsocket\fR $server 9900] |
---|
171 | gets $sockChan line |
---|
172 | close $sockChan |
---|
173 | puts "The time on $server is $line" |
---|
174 | .CE |
---|
175 | |
---|
176 | .SH "SEE ALSO" |
---|
177 | fconfigure(n), flush(n), open(n), read(n) |
---|
178 | |
---|
179 | .SH KEYWORDS |
---|
180 | bind, channel, connection, domain name, host, network address, socket, tcp |
---|