1socket(n)                    Tcl Built-In Commands                   socket(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       socket - Open a TCP network connection
9

SYNOPSIS

11       socket ?options? host port
12
13       socket -server command ?options? port
14_________________________________________________________________
15
16

DESCRIPTION

18       This  command  opens  a network socket and returns a channel identifier
19       that may be used in future invocations of commands like read, puts  and
20       flush.   At present only the TCP network protocol is supported;  future
21       releases may include support for additional protocols.  The socket com‐
22       mand  may be used to open either the client or server side of a connec‐
23       tion, depending on whether the -server switch is specified.
24
25       Note that the default encoding for all sockets is the system  encoding,
26       as returned by encoding system.  Most of the time, you will need to use
27       fconfigure to alter this to something else, such as  utf-8  (ideal  for
28       communicating  with  other Tcl processes) or iso8859-1 (useful for many
29       network protocols, especially the older ones).
30

CLIENT SOCKETS

32       If the -server option is not specified, then the client side of a  con‐
33       nection is opened and the command returns a channel identifier that can
34       be used for both reading and writing.  Port and host specify a port  to
35       connect to;  there must be a server accepting connections on this port.
36       Port is an integer port number (or service name,  where  supported  and
37       understood  by  the host operating system) and host is either a domain-
38       style name such as  www.tcl.tk  or  a  numerical  IP  address  such  as
39       127.0.0.1.   Use localhost to refer to the host on which the command is
40       invoked.
41
42       The following options may also be present before host to specify  addi‐
43       tional information about the connection:
44
45       -myaddr addr
46              Addr  gives the domain-style name or numerical IP address of the
47              client-side network interface to use for the  connection.   This
48              option  may be useful if the client machine has multiple network
49              interfaces.  If the  option  is  omitted  then  the  client-side
50              interface will be chosen by the system software.
51
52       -myport port
53              Port  specifies  an  integer port number (or service name, where
54              supported and understood by the host operating  system)  to  use
55              for  the  client's  side  of  the connection.  If this option is
56              omitted, the client's port number will be chosen  at  random  by
57              the system software.
58
59       -async The  -async  option will cause the client socket to be connected
60              asynchronously. This means that the socket will be created imme‐
61              diately  but  may  not  yet be connected to the server, when the
62              call to socket returns. When a gets or  flush  is  done  on  the
63              socket  before  the connection attempt succeeds or fails, if the
64              socket is in blocking mode, the operation will  wait  until  the
65              connection  is completed or fails. If the socket is in nonblock‐
66              ing mode and a gets or flush is done on the  socket  before  the
67              connection  attempt  succeeds  or  fails,  the operation returns
68              immediately and fblocked on the socket returns 1.
69

SERVER SOCKETS

71       If the -server option is specified then the new socket will be a server
72       for  the port given by port (either an integer or a service name, where
73       supported and understood by the host operating system; if port is zero,
74       the  operating  system  will  allocate a free port to the server socket
75       which may be discovered by  using  fconfigure  to  read  the  -sockname
76       option).   Tcl will automatically accept connections to the given port.
77       For each connection Tcl will create a new channel that may be  used  to
78       communicate with the client.  Tcl then invokes command with three addi‐
79       tional arguments: the name of the new channel, the address, in  network
80       address notation, of the client's host, and the client's port number.
81
82       The following additional option may also be specified before host:
83
84       -myaddr addr
85              Addr  gives the domain-style name or numerical IP address of the
86              server-side network interface to use for the  connection.   This
87              option  may be useful if the server machine has multiple network
88              interfaces.  If the option is omitted then the server socket  is
89              bound  to  the  special address INADDR_ANY so that it can accept
90              connections from any interface.
91
92       Server channels cannot be used for input or output; their sole  use  is
93       to  accept new client connections. The channels created for each incom‐
94       ing client connection are opened for  input  and  output.  Closing  the
95       server channel shuts down the server so that no new connections will be
96       accepted;  however, existing connections will be unaffected.
97
98       Server sockets depend on the Tcl event mechanism to find out  when  new
99       connections  are  opened.   If  the application doesn't enter the event
100       loop, for example by invoking the vwait command or calling the C proce‐
101       dure Tcl_DoOneEvent, then no connections will be accepted.
102
103       If  port  is  specified  as zero, the operating system will allocate an
104       unused port for use as a server socket.  The port number actually allo‐
105       cated  may  be retrieved from the created server socket using the fcon‐
106       figure command to retrieve the -sockname option as described below.
107

CONFIGURATION OPTIONS

109       The fconfigure command can be used to query several readonly configura‐
110       tion options for socket channels:
111
112       -error This  option  gets the current error status of the given socket.
113              This is useful when you need to  determine  if  an  asynchronous
114              connect  operation  succeeded.  If there was an error, the error
115              message is returned.  If there was no error, an empty string  is
116              returned.
117
118       -sockname
119              This  option  returns a list of three elements, the address, the
120              host name and the port number for the socket. If the  host  name
121              cannot  be  computed,  the  second  element  is identical to the
122              address, the first element of the list.
123
124       -peername
125              This option is not supported by server sockets. For  client  and
126              accepted  sockets, this option returns a list of three elements;
127              these are the address, the host name and the port to  which  the
128              peer  socket  is  connected or bound. If the host name cannot be
129              computed, the second element of the list  is  identical  to  the
130              address, its first element.
131

EXAMPLES

133       Here is a very simple time server:
134              proc Server {channel clientaddr clientport} {
135                 puts "Connection from $clientaddr registered"
136                 puts $channel [clock format [clock seconds]]
137                 close $channel
138              }
139
140              socket -server Server 9900
141              vwait forever
142
143       And here is the corresponding client to talk to the server:
144              set server localhost
145              set sockChan [socket $server 9900]
146              gets $sockChan line
147              close $sockChan
148              puts "The time on $server is $line"
149
150

SEE ALSO

152       fconfigure(n), flush(n), open(n), read(n)
153
154

KEYWORDS

156       bind,  channel, connection, domain name, host, network address, socket,
157       tcp
158
159
160
161Tcl                                   8.0                            socket(n)
Impressum