Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/socket.n @ 47

Last change on this file since 47 was 25, checked in by landauf, 16 years ago

added tcl to libs

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