socket.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:7k
- '"
- '" Copyright (c) 1996 Sun Microsystems, Inc.
- '" Copyright (c) 1998-1999 by Scriptics Corporation.
- '"
- '" See the file "license.terms" for information on usage and redistribution
- '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- '"
- '" RCS: @(#) $Id: socket.n,v 1.7.2.3 2004/10/27 14:23:58 dkf Exp $
- .so man.macros
- .TH socket n 8.0 Tcl "Tcl Built-In Commands"
- .BS
- '" Note: do not modify the .SH NAME line immediately below!
- .SH NAME
- socket - Open a TCP network connection
- .SH SYNOPSIS
- .sp
- fBsocket fR?fIoptionsfR? fIhost portfR
- .sp
- fBsocketfR fB-server fIcommandfR ?fIoptionsfR? fIportfR
- .BE
- .SH DESCRIPTION
- .PP
- This command opens a network socket and returns a channel
- identifier that may be used in future invocations of commands like
- fBreadfR, fBputsfR and fBflushfR.
- At present only the TCP network protocol is supported; future
- releases may include support for additional protocols.
- The fBsocketfR command may be used to open either the client or
- server side of a connection, depending on whether the fB-serverfR
- switch is specified.
- .PP
- Note that the default encoding for fIallfR sockets is the system
- encoding, as returned by fBencoding systemfR. Most of the time, you
- will need to use fBfconfigurefR to alter this to something else,
- such as fIutf-8fR (ideal for communicating with other Tcl
- processes) or fIiso8859-1fR (useful for many network protocols,
- especially the older ones).
- .SH "CLIENT SOCKETS"
- .PP
- If the fB-serverfR option is not specified, then the client side of a
- connection is opened and the command returns a channel identifier
- that can be used for both reading and writing.
- fIPortfR and fIhostfR specify a port
- to connect to; there must be a server accepting connections on
- this port. fIPortfR is an integer port number
- (or service name, where supported and understood by the host operating
- system) and fIhostfR
- is either a domain-style name such as fBwww.tcl.tkfR or
- a numerical IP address such as fB127.0.0.1fR.
- Use fIlocalhostfR to refer to the host on which the command is invoked.
- .PP
- The following options may also be present before fIhostfR
- to specify additional information about the connection:
- .TP
- fB-myaddrfI addrfR
- fIAddrfR gives the domain-style name or numerical IP address of
- the client-side network interface to use for the connection.
- This option may be useful if the client machine has multiple network
- interfaces. If the option is omitted then the client-side interface
- will be chosen by the system software.
- .TP
- fB-myportfI portfR
- fIPortfR specifies an integer port number (or service name, where
- supported and understood by the host operating system) to use for the
- client's
- side of the connection. If this option is omitted, the client's
- port number will be chosen at random by the system software.
- .TP
- fB-asyncfR
- The fB-asyncfR option will cause the client socket to be connected
- asynchronously. This means that the socket will be created immediately but
- may not yet be connected to the server, when the call to fBsocketfR
- returns. When a fBgetsfR or fBflushfR is done on the socket before the
- connection attempt succeeds or fails, if the socket is in blocking mode, the
- operation will wait until the connection is completed or fails. If the
- socket is in nonblocking mode and a fBgetsfR or fBflushfR is done on
- the socket before the connection attempt succeeds or fails, the operation
- returns immediately and fBfblockedfR on the socket returns 1.
- .SH "SERVER SOCKETS"
- .PP
- If the fB-serverfR option is specified then the new socket
- will be a server for the port given by fIportfR (either an integer
- or a service name, where supported and understood by the host
- operating system; if fIportfR is zero, the operating system will
- allocate a free port to the server socket which may be discovered by
- using fBfconfigurefR to read the fB-socknamefR option).
- Tcl will automatically accept connections to the given port.
- For each connection Tcl will create a new channel that may be used to
- communicate with the client. Tcl then invokes fIcommandfR
- with three additional arguments: the name of the new channel, the
- address, in network address notation, of the client's host, and
- the client's port number.
- .PP
- The following additional option may also be specified before fIhostfR:
- .TP
- fB-myaddrfI addrfR
- fIAddrfR gives the domain-style name or numerical IP address of
- the server-side network interface to use for the connection.
- This option may be useful if the server machine has multiple network
- interfaces. If the option is omitted then the server socket is bound
- to the special address INADDR_ANY so that it can accept connections from
- any interface.
- .PP
- Server channels cannot be used for input or output; their sole use is to
- accept new client connections. The channels created for each incoming
- client connection are opened for input and output. Closing the server
- channel shuts down the server so that no new connections will be
- accepted; however, existing connections will be unaffected.
- .PP
- Server sockets depend on the Tcl event mechanism to find out when
- new connections are opened. If the application doesn't enter the
- event loop, for example by invoking the fBvwaitfR command or
- calling the C procedure fBTcl_DoOneEventfR, then no connections
- will be accepted.
- .PP
- If fIportfR is specified as zero, the operating system will allocate
- an unused port for use as a server socket. The port number actually
- allocated may be retrieved from the created server socket using the
- fBfconfigurefR command to retrieve the fB-socknamefR option as
- described below.
- .SH "CONFIGURATION OPTIONS"
- The fBfconfigurefR command can be used to query several readonly
- configuration options for socket channels:
- .TP
- fB-errorfR
- This option gets the current error status of the given socket. This
- is useful when you need to determine if an asynchronous connect
- operation succeeded. If there was an error, the error message is
- returned. If there was no error, an empty string is returned.
- .TP
- fB-socknamefR
- This option returns a list of three elements, the address, the host name
- and the port number for the socket. If the host name cannot be computed,
- the second element is identical to the address, the first element of the
- list.
- .TP
- fB-peernamefR
- This option is not supported by server sockets. For client and accepted
- sockets, this option returns a list of three elements; these are the
- address, the host name and the port to which the peer socket is connected
- or bound. If the host name cannot be computed, the second element of the
- list is identical to the address, its first element.
- .PP
- .SH "EXAMPLES"
- Here is a very simple time server:
- .CS
- proc Server {channel clientaddr clientport} {
- puts "Connection from $clientaddr registered"
- puts $channel [clock format [clock seconds]]
- close $channel
- }
- fBsocketfR -server Server 9900
- vwait forever
- .CE
- .PP
- And here is the corresponding client to talk to the server:
- .CS
- set server localhost
- set sockChan [fBsocketfR $server 9900]
- gets $sockChan line
- close $sockChan
- puts "The time on $server is $line"
- .CE
- .SH "SEE ALSO"
- fconfigure(n), flush(n), open(n), read(n)
- .SH KEYWORDS
- bind, channel, connection, domain name, host, network address, socket, tcp