📄 socket.n
字号:
'\"'\" Copyright (c) 1996 Sun Microsystems, Inc.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\"'\" SCCS: @(#) socket.n 1.14 97/10/31 12:51:12.so man.macros.TH socket n 7.5 Tcl "Tcl Built-In Commands".BS'\" Note: do not modify the .SH NAME line immediately below!.SH NAMEsocket \- Open a TCP network connection.SH SYNOPSIS.sp\fBsocket \fR?\fIoptions\fR? \fIhost port\fR.sp\fBsocket\fR \fB\-server \fIcommand\fR ?\fIoptions\fR? \fIport\fR.BE.SH DESCRIPTION.PPThis command opens a network socket and returns a channelidentifier that may be used in future invocations of commands like\fBread\fR, \fBputs\fR and \fBflush\fR.At present only the TCP network protocol is supported; futurereleases may include support for additional protocols.The \fBsocket\fR command may be used to open either the client orserver side of a connection, depending on whether the \fB\-server\fRswitch is specified..SH "CLIENT SOCKETS".PPIf the \fB\-server\fR option is not specified, then the client side of aconnection is opened and the command returns a channel identifierthat can be used for both reading and writing.\fIPort\fR and \fIhost\fR specify a portto connect to; there must be a server accepting connections onthis port. \fIPort\fR is an integer port number and \fIhost\fRis either a domain-style name such as \fBwww.sunlabs.com\fR ora numerical IP address such as \fB127.0.0.1\fR.Use \fIlocalhost\fR to refer to the host on which the command is invoked..PPThe following options may also be present before \fIhost\fRto specify additional information about the connection:.TP\fB\-myaddr\fI addr\fR\fIAddr\fR gives the domain-style name or numerical IP address ofthe client-side network interface to use for the connection.This option may be useful if the client machine has multiple networkinterfaces. If the option is omitted then the client-side interfacewill be chosen by the system software..TP\fB\-myport\fI port\fR\fIPort\fR specifies an integer port number to use for the client'sside of the connection. If this option is omitted, the client'sport number will be chosen at random by the system software..TP\fB\-async\fRThe \fB\-async\fR option will cause the client socket to be connectedasynchronously. This means that the socket will be created immediately butmay not yet be connected to the server, when the call to \fBsocket\fRreturns. When a \fBgets\fR or \fBflush\fR is done on the socket before theconnection attempt succeeds or fails, if the socket is in blocking mode, theoperation will wait until the connection is completed or fails. If thesocket is in nonblocking mode and a \fBgets\fR or \fBflush\fR is done onthe socket before the connection attempt succeeds or fails, the operationreturns immediately and \fBfblocked\fR on the socket returns 1..SH "SERVER SOCKETS".PPIf the \fB\-server\fR option is specified then the new socketwill be a server for the port given by \fIport\fR.Tcl will automatically accept connections to the given port.For each connection Tcl will create a new channel that may be used tocommunicate with the client. Tcl then invokes \fIcommand\fRwith three additional arguments: the name of the new channel, theaddress, in network address notation, of the client's host, andthe client's port number..PPThe following additional option may also be specified before \fIhost\fR:.TP\fB\-myaddr\fI addr\fR\fIAddr\fR gives the domain-style name or numerical IP address ofthe server-side network interface to use for the connection.This option may be useful if the server machine has multiple networkinterfaces. If the option is omitted then the server socket is boundto the special address INADDR_ANY so that it can accept connections fromany interface..PPServer channels cannot be used for input or output; their sole use is toaccept new client connections. The channels created for each incomingclient connection are opened for input and output. Closing the serverchannel shuts down the server so that no new connections will beaccepted; however, existing connections will be unaffected..PPServer sockets depend on the Tcl event mechanism to find out whennew connections are opened. If the application doesn't enter theevent loop, for example by invoking the \fBvwait\fR command orcalling the C procedure \fBTcl_DoOneEvent\fR, then no connectionswill be accepted..SH CONFIGURATION OPTIONSThe \fBfconfigure\fR command can be used to query several readonlyconfiguration options for socket channels:.TP\fB\-sockname\fRThis option returns a list of three elements, the address, the host nameand 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 thelist..TP\fB\-peername\fRThis option is not supported by server sockets. For client and acceptedsockets, this option returns a list of three elements; these are theaddress, the host name and the port to which the peer socket is connectedor bound. If the host name cannot be computed, the second element of thelist is identical to the address, its first element..PP.SH "SEE ALSO"flush(n), open(n), read(n).SH KEYWORDSbind, channel, connection, domain name, host, network address, socket, tcp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -