OSDN Git Service

7a7486722e5d8437f40a4076116e1e0af769b33e
[pf3gnuchains/sourceware.git] / tcl / doc / socket.n
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$
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
33 .SH "CLIENT SOCKETS"
34 .PP
35 If the \fB\-server\fR option is not specified, then the client side of a
36 connection is opened and the command returns a channel identifier
37 that can be used for both reading and writing.
38 \fIPort\fR and \fIhost\fR specify a port
39 to connect to;  there must be a server accepting connections on
40 this port.  \fIPort\fR is an integer port number and \fIhost\fR
41 is either a domain-style name such as \fBwww.sunlabs.com\fR or
42 a numerical IP address such as \fB127.0.0.1\fR.
43 Use \fIlocalhost\fR to refer to the host on which the command is invoked.
44 .PP
45 The following options may also be present before \fIhost\fR
46 to specify additional information about the connection:
47 .TP
48 \fB\-myaddr\fI addr\fR
49 \fIAddr\fR gives the domain-style name or numerical IP address of
50 the client-side network interface to use for the connection.
51 This option may be useful if the client machine has multiple network
52 interfaces.  If the option is omitted then the client-side interface
53 will be chosen by the system software.
54 .TP
55 \fB\-myport\fI port\fR
56 \fIPort\fR specifies an integer port number to use for the client's
57 side of the connection.  If this option is omitted, the client's
58 port number will be chosen at random by the system software.
59 .TP
60 \fB\-async\fR
61 The \fB\-async\fR option will cause the client socket to be connected
62 asynchronously. This means that the socket will be created immediately but
63 may not yet be connected to the server, when the call to \fBsocket\fR
64 returns. When a \fBgets\fR or \fBflush\fR is done on the socket before the
65 connection attempt succeeds or fails, if the socket is in blocking mode, the
66 operation will wait until the connection is completed or fails. If the
67 socket is in nonblocking mode and a \fBgets\fR or \fBflush\fR is done on
68 the socket before the connection attempt succeeds or fails, the operation
69 returns immediately and \fBfblocked\fR on the socket returns 1.
70
71 .SH "SERVER SOCKETS"
72 .PP
73 If the \fB\-server\fR option is specified then the new socket
74 will be a server for the port given by \fIport\fR.
75 Tcl will automatically accept connections to the given port.
76 For each connection Tcl will create a new channel that may be used to
77 communicate with the client.  Tcl then invokes \fIcommand\fR
78 with three additional arguments: the name of the new channel, the
79 address, in network address notation, of the client's host, and
80 the client's port number.
81 .PP
82 The following additional option may also be specified before \fIhost\fR:
83 .TP
84 \fB\-myaddr\fI addr\fR
85 \fIAddr\fR gives the domain-style name or numerical IP address of
86 the server-side network interface to use for the connection.
87 This option may be useful if the server machine has multiple network
88 interfaces.  If the option is omitted then the server socket is bound
89 to the special address INADDR_ANY so that it can accept connections from
90 any interface.
91 .PP
92 Server channels cannot be used for input or output; their sole use is to
93 accept new client connections. The channels created for each incoming
94 client connection are opened for input and output. Closing the server
95 channel shuts down the server so that no new connections will be
96 accepted;  however, existing connections will be unaffected.
97 .PP
98 Server sockets depend on the Tcl event mechanism to find out when
99 new connections are opened.  If the application doesn't enter the
100 event loop, for example by invoking the \fBvwait\fR command or
101 calling the C procedure \fBTcl_DoOneEvent\fR, then no connections
102 will be accepted.
103
104 .SH "CONFIGURATION OPTIONS"
105 The \fBfconfigure\fR command can be used to query several readonly
106 configuration options for socket channels:
107 .VS 8.0.5
108 .TP
109 \fB\-error\fR
110 This option gets the current error status of the given socket.  This
111 is useful when you need to determine if an asynchronous connect
112 operation succeeded.  If there was an error, the error message is
113 returned.  If there was no error, an empty string is returned.
114 .VE 8.0.5
115 .TP
116 \fB\-sockname\fR
117 This option returns a list of three elements, the address, the host name
118 and the port number for the socket. If the host name cannot be computed,
119 the second element is identical to the address, the first element of the
120 list.
121 .TP
122 \fB\-peername\fR
123 This option is not supported by server sockets. For client and accepted
124 sockets, this option returns a list of three elements; these are the
125 address, the host name and the port to which the peer socket is connected
126 or bound. If the host name cannot be computed, the second element of the
127 list is identical to the address, its first element.
128 .PP
129
130 .SH "SEE ALSO"
131 flush(n), open(n), read(n)
132
133 .SH KEYWORDS
134 bind, channel, connection, domain name, host, network address, socket, tcp