📄 opentcp.3
字号:
'\"'\" Copyright (c) 1996-7 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: @(#) OpenTcp.3 1.19 97/06/25 14:44:00.so man.macros.TH Tcl_OpenTcpClient 3 8.0 Tcl "Tcl Library Procedures".BS'\" Note: do not modify the .SH NAME line immediately below!.SH NAMETcl_OpenTcpClient, Tcl_MakeTcpClientChannel, Tcl_OpenTcpServer \- procedures to open channels using TCP sockets.SH SYNOPSIS.nf\fB#include <tcl.h> \fR.spTcl_Channel\fBTcl_OpenTcpClient\fR(\fIinterp, port, host, myaddr, myport, async\fR).spTcl_Channel\fBTcl_MakeTcpClientChannel\fR(\fIsock\fR).spTcl_Channel\fBTcl_OpenTcpServer\fR(\fIinterp, port, myaddr, proc, clientData\fR).sp.SH ARGUMENTS.AS Tcl_ChannelType newClientProcPtr in.AP Tcl_Interp *interp inTcl interpreter to use for error reporting. If non-NULL and anerror occurs, an error message is left in \fIinterp->result\fR..AP int port inA port number to connect to as a client or to listen on as a server..AP char *host inA string specifying a host name or address for the remote end of the connection..AP int myport inA port number for the client's end of the socket. If 0, a port numberis allocated at random..AP char *myaddr inA string specifying the host name or address for network interface to usefor the local end of the connection. If NULL, a default interface ischosen..AP int async inIf nonzero, the client socket is connected asynchronously to the server..AP ClientData sock inPlatform-specific handle for client TCP socket..AP Tcl_TcpAcceptProc *proc inPointer to a procedure to invoke each time a new connection isaccepted via the socket..AP ClientData clientData inArbitrary one-word value to pass to \fIproc\fR..BE.SH DESCRIPTION.PPThese functions are convenience procedures for creatingchannels that communicate over TCP sockets.The operations on a channelare described in the manual entry for \fBTcl_OpenFileChannel\fR..SH TCL_OPENTCPCLIENT.PP\fBTcl_OpenTcpClient\fR opens a client TCP socket connected to a \fIport\fRon a specific \fIhost\fR, and returns a channel that can be used tocommunicate with the server. The host to connect to can be specified eitheras a domain name style name (e.g. \fBwww.sunlabs.com\fR), or as a stringcontaining the alphanumeric representation of its four-byte address (e.g.\fB127.0.0.1\fR). Use the string \fBlocalhost\fR to connect to a TCP socket onthe host on which the function is invoked..PPThe \fImyaddr\fR and \fImyport\fR arguments allow a client to specify anaddress for the local end of the connection. If \fImyaddr\fR is NULL, thenan interface is chosen automatically by the operating system.If \fImyport\fR is 0, then a port number is chosen at random bythe operating system..PPIf \fIasync\fR is zero, the call to \fBTcl_OpenTcpClient\fR returns onlyafter the client socket has either successfully connected to the server, orthe attempted connection has failed.If \fIasync\fR is nonzero the socket is connected asynchronously and thereturned channel may not yet be connected to the server when the call to\fBTcl_OpenTcpClient\fR returns. If the channel is in blocking mode and aninput or output operation is done on the channel before the connection iscompleted or fails, that operation will wait until the connection eithercompletes successfully or fails. If the channel is in nonblocking mode, theinput or output operation will return immediately and a subsequent call to\fBTcl_InputBlocked\fR on the channel will return nonzero..PPThe returned channel is opened for reading and writing.If an error occurs in opening the socket, \fBTcl_OpenTcpClient\fR returnsNULL and records a POSIX error code that can be retrievedwith \fBTcl_GetErrno\fR.In addition, if \fIinterp\fR is non-NULL, an error messageis left in \fIinterp->result\fR..PPThe newly created channel is not registered in the supplied interpreter; toregister it, use \fBTcl_RegisterChannel\fR.If one of the standard channels, \fBstdin, stdout\fR or \fBstderr\fR waspreviously closed, the act of creating the new channel also assigns it as areplacement for the standard channel..SH TCL_MAKETCPCLIENTCHANNEL.PP\fBTcl_MakeTcpClientChannel\fR creates a \fBTcl_Channel\fR around anexisting, platform specific, handle for a client TCP socket..PPThe newly created channel is not registered in the supplied interpreter; toregister it, use \fBTcl_RegisterChannel\fR.If one of the standard channels, \fBstdin, stdout\fR or \fBstderr\fR waspreviously closed, the act of creating the new channel also assigns it as areplacement for the standard channel..SH TCL_OPENTCPSERVER.PP\fBTcl_OpenTcpServer\fR opens a TCP socket on the local host on a specified\fIport\fR and uses the Tcl event mechanism to accept requests from clientsto connect to it. The \fImyaddr\fP argument specifies the network interface.If \fImyaddr\fP is NULL the special address INADDR_ANY should be used toallow connections from any network interface.Each time a client connects to this socket, Tcl creates a channelfor the new connection and invokes \fIproc\fR with information aboutthe channel. \fIProc\fR must match the following prototype:.CStypedef void Tcl_TcpAcceptProc( ClientData \fIclientData\fR, Tcl_Channel \fIchannel\fR, char *\fIhostName\fR, int \fIport\fP);.CE.PPThe \fIclientData\fR argument will be the same as the \fIclientData\fRargument to \fBTcl_OpenTcpServer\fR, \fIchannel\fR will be the handlefor the new channel, \fIhostName\fR points to a string containingthe name of the client host making the connection, and \fIport\fPwill contain the client's port number.The new channelis opened for both input and output. If \fIproc\fR raises an error, the connection is closed automatically.\fIProc\fR has no return value, but if it wishes to reject theconnection it can close \fIchannel\fR..PP\fBTcl_OpenTcpServer\fR normally returns a pointer to a channelrepresenting the server socket.If an error occurs, \fBTcl_OpenTcpServer\fR returns NULL andrecords a POSIX error code that can be retrieved with \fBTcl_GetErrno\fR.In addition, if \fIinterp->result\fR is non-NULL, an error messageis left in \fIinterp->result\fR..PPThe channel returned by \fBTcl_OpenTcpServer\fR cannot be used foreither input or output.It is simply a handle for the socket used to accept connections.The caller can close the channel to shut down the server and disallowfurther connections from new clients..PPTCP server channels operate correctly only in applications that dispatchevents through \fBTcl_DoOneEvent\fR or through Tcl commands such as\fBvwait\fR; otherwise Tcl will never notice that a connection request froma remote client is pending..PPThe newly created channel is not registered in the supplied interpreter; toregister it, use \fBTcl_RegisterChannel\fR.If one of the standard channels, \fBstdin, stdout\fR or \fBstderr\fR waspreviously closed, the act of creating the new channel also assigns it as areplacement for the standard channel..VS.SH "PLATFORM ISSUES".PPOn Unix platforms, the socket handle is a Unix file descriptor asreturned by the \fBsocket\fR system call. On the Windows platform, thesocket handle is a \fBSOCKET\fR as defined in the WinSock API. On theMacintosh platform, the socket handle is a \fBStreamPtr\fR..VE.SH "SEE ALSO"Tcl_OpenFileChannel(3), Tcl_RegisterChannel(3), vwait(n).SH KEYWORDSclient, server, TCP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -