1 |
578 |
markom |
'\"
|
2 |
|
|
'\" Copyright (c) 1996 Sun Microsystems, Inc.
|
3 |
|
|
'\"
|
4 |
|
|
'\" See the file "license.terms" for information on usage and redistribution
|
5 |
|
|
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
6 |
|
|
'\"
|
7 |
|
|
'\" RCS: @(#) $Id: socket.n,v 1.1.1.1 2002-01-16 10:25:25 markom Exp $
|
8 |
|
|
.so man.macros
|
9 |
|
|
.TH socket n 7.5 Tcl "Tcl Built-In Commands"
|
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 |
|
|
|
21 |
|
|
.SH DESCRIPTION
|
22 |
|
|
.PP
|
23 |
|
|
This command opens a network socket and returns a channel
|
24 |
|
|
identifier that may be used in future invocations of commands like
|
25 |
|
|
\fBread\fR, \fBputs\fR and \fBflush\fR.
|
26 |
|
|
At present only the TCP network protocol is supported; future
|
27 |
|
|
releases may include support for additional protocols.
|
28 |
|
|
The \fBsocket\fR command may be used to open either the client or
|
29 |
|
|
server side of a connection, depending on whether the \fB\-server\fR
|
30 |
|
|
switch is specified.
|
31 |
|
|
|
32 |
|
|
.SH "CLIENT SOCKETS"
|
33 |
|
|
.PP
|
34 |
|
|
If the \fB\-server\fR option is not specified, then the client side of a
|
35 |
|
|
connection is opened and the command returns a channel identifier
|
36 |
|
|
that can be used for both reading and writing.
|
37 |
|
|
\fIPort\fR and \fIhost\fR specify a port
|
38 |
|
|
to connect to; there must be a server accepting connections on
|
39 |
|
|
this port. \fIPort\fR is an integer port number and \fIhost\fR
|
40 |
|
|
is either a domain-style name such as \fBwww.sunlabs.com\fR or
|
41 |
|
|
a numerical IP address such as \fB127.0.0.1\fR.
|
42 |
|
|
Use \fIlocalhost\fR to refer to the host on which the command is invoked.
|
43 |
|
|
.PP
|
44 |
|
|
The following options may also be present before \fIhost\fR
|
45 |
|
|
to specify additional information about the connection:
|
46 |
|
|
.TP
|
47 |
|
|
\fB\-myaddr\fI addr\fR
|
48 |
|
|
\fIAddr\fR gives the domain-style name or numerical IP address of
|
49 |
|
|
the client-side network interface to use for the connection.
|
50 |
|
|
This option may be useful if the client machine has multiple network
|
51 |
|
|
interfaces. If the option is omitted then the client-side interface
|
52 |
|
|
will be chosen by the system software.
|
53 |
|
|
.TP
|
54 |
|
|
\fB\-myport\fI port\fR
|
55 |
|
|
\fIPort\fR specifies an integer port number to use for the client's
|
56 |
|
|
side of the connection. If this option is omitted, the client's
|
57 |
|
|
port number will be chosen at random by the system software.
|
58 |
|
|
.TP
|
59 |
|
|
\fB\-async\fR
|
60 |
|
|
The \fB\-async\fR option will cause the client socket to be connected
|
61 |
|
|
asynchronously. This means that the socket will be created immediately but
|
62 |
|
|
may not yet be connected to the server, when the call to \fBsocket\fR
|
63 |
|
|
returns. When a \fBgets\fR or \fBflush\fR is done on the socket before the
|
64 |
|
|
connection attempt succeeds or fails, if the socket is in blocking mode, the
|
65 |
|
|
operation will wait until the connection is completed or fails. If the
|
66 |
|
|
socket is in nonblocking mode and a \fBgets\fR or \fBflush\fR is done on
|
67 |
|
|
the socket before the connection attempt succeeds or fails, the operation
|
68 |
|
|
returns immediately and \fBfblocked\fR on the socket returns 1.
|
69 |
|
|
|
70 |
|
|
.SH "SERVER SOCKETS"
|
71 |
|
|
.PP
|
72 |
|
|
If the \fB\-server\fR option is specified then the new socket
|
73 |
|
|
will be a server for the port given by \fIport\fR.
|
74 |
|
|
Tcl will automatically accept connections to the given port.
|
75 |
|
|
For each connection Tcl will create a new channel that may be used to
|
76 |
|
|
communicate with the client. Tcl then invokes \fIcommand\fR
|
77 |
|
|
with three additional arguments: the name of the new channel, the
|
78 |
|
|
address, in network address notation, of the client's host, and
|
79 |
|
|
the client's port number.
|
80 |
|
|
.PP
|
81 |
|
|
The following additional option may also be specified before \fIhost\fR:
|
82 |
|
|
.TP
|
83 |
|
|
\fB\-myaddr\fI addr\fR
|
84 |
|
|
\fIAddr\fR gives the domain-style name or numerical IP address of
|
85 |
|
|
the server-side network interface to use for the connection.
|
86 |
|
|
This option may be useful if the server machine has multiple network
|
87 |
|
|
interfaces. If the option is omitted then the server socket is bound
|
88 |
|
|
to the special address INADDR_ANY so that it can accept connections from
|
89 |
|
|
any interface.
|
90 |
|
|
.PP
|
91 |
|
|
Server channels cannot be used for input or output; their sole use is to
|
92 |
|
|
accept new client connections. The channels created for each incoming
|
93 |
|
|
client connection are opened for input and output. Closing the server
|
94 |
|
|
channel shuts down the server so that no new connections will be
|
95 |
|
|
accepted; however, existing connections will be unaffected.
|
96 |
|
|
.PP
|
97 |
|
|
Server sockets depend on the Tcl event mechanism to find out when
|
98 |
|
|
new connections are opened. If the application doesn't enter the
|
99 |
|
|
event loop, for example by invoking the \fBvwait\fR command or
|
100 |
|
|
calling the C procedure \fBTcl_DoOneEvent\fR, then no connections
|
101 |
|
|
will be accepted.
|
102 |
|
|
|
103 |
|
|
.SH CONFIGURATION OPTIONS
|
104 |
|
|
The \fBfconfigure\fR command can be used to query several readonly
|
105 |
|
|
configuration options for socket channels:
|
106 |
|
|
.TP
|
107 |
|
|
\fB\-sockname\fR
|
108 |
|
|
This option returns a list of three elements, the address, the host name
|
109 |
|
|
and the port number for the socket. If the host name cannot be computed,
|
110 |
|
|
the second element is identical to the address, the first element of the
|
111 |
|
|
list.
|
112 |
|
|
.TP
|
113 |
|
|
\fB\-peername\fR
|
114 |
|
|
This option is not supported by server sockets. For client and accepted
|
115 |
|
|
sockets, this option returns a list of three elements; these are the
|
116 |
|
|
address, the host name and the port to which the peer socket is connected
|
117 |
|
|
or bound. If the host name cannot be computed, the second element of the
|
118 |
|
|
list is identical to the address, its first element.
|
119 |
|
|
.PP
|
120 |
|
|
|
121 |
|
|
.SH "SEE ALSO"
|
122 |
|
|
flush(n), open(n), read(n)
|
123 |
|
|
|
124 |
|
|
.SH KEYWORDS
|
125 |
|
|
bind, channel, connection, domain name, host, network address, socket, tcp
|