| 1 |
'\" |
| 2 |
'\" Copyright (c) 1996-7 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 |
.TH Tcl_OpenTcpClient 3 8.0 Tcl "Tcl Library Procedures" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
Tcl_OpenTcpClient, Tcl_MakeTcpClientChannel, Tcl_OpenTcpServer \- procedures to open channels using TCP sockets |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tcl.h> \fR |
| 16 |
.sp |
| 17 |
Tcl_Channel |
| 18 |
\fBTcl_OpenTcpClient\fR(\fIinterp, port, host, myaddr, myport, async\fR) |
| 19 |
.sp |
| 20 |
Tcl_Channel |
| 21 |
\fBTcl_MakeTcpClientChannel\fR(\fIsock\fR) |
| 22 |
.sp |
| 23 |
Tcl_Channel |
| 24 |
\fBTcl_OpenTcpServer\fR(\fIinterp, port, myaddr, proc, clientData\fR) |
| 25 |
.sp |
| 26 |
.SH ARGUMENTS |
| 27 |
.AS Tcl_TcpAcceptProc clientData |
| 28 |
.AP Tcl_Interp *interp in |
| 29 |
Tcl interpreter to use for error reporting. If non-NULL and an |
| 30 |
error occurs, an error message is left in the interpreter's result. |
| 31 |
.AP int port in |
| 32 |
A port number to connect to as a client or to listen on as a server. |
| 33 |
.AP "const char" *host in |
| 34 |
A string specifying a host name or address for the remote end of the connection. |
| 35 |
.AP int myport in |
| 36 |
A port number for the client's end of the socket. If 0, a port number |
| 37 |
is allocated at random. |
| 38 |
.AP "const char" *myaddr in |
| 39 |
A string specifying the host name or address for network interface to use |
| 40 |
for the local end of the connection. If NULL, a default interface is |
| 41 |
chosen. |
| 42 |
.AP int async in |
| 43 |
If nonzero, the client socket is connected asynchronously to the server. |
| 44 |
.AP ClientData sock in |
| 45 |
Platform-specific handle for client TCP socket. |
| 46 |
.AP Tcl_TcpAcceptProc *proc in |
| 47 |
Pointer to a procedure to invoke each time a new connection is |
| 48 |
accepted via the socket. |
| 49 |
.AP ClientData clientData in |
| 50 |
Arbitrary one-word value to pass to \fIproc\fR. |
| 51 |
.BE |
| 52 |
.SH DESCRIPTION |
| 53 |
.PP |
| 54 |
These functions are convenience procedures for creating |
| 55 |
channels that communicate over TCP sockets. |
| 56 |
The operations on a channel |
| 57 |
are described in the manual entry for \fBTcl_OpenFileChannel\fR. |
| 58 |
.SS TCL_OPENTCPCLIENT |
| 59 |
.PP |
| 60 |
\fBTcl_OpenTcpClient\fR opens a client TCP socket connected to a \fIport\fR |
| 61 |
on a specific \fIhost\fR, and returns a channel that can be used to |
| 62 |
communicate with the server. The host to connect to can be specified either |
| 63 |
as a domain name style name (e.g. \fBwww.sunlabs.com\fR), or as a string |
| 64 |
containing the alphanumeric representation of its four-byte address (e.g. |
| 65 |
\fB127.0.0.1\fR). Use the string \fBlocalhost\fR to connect to a TCP socket on |
| 66 |
the host on which the function is invoked. |
| 67 |
.PP |
| 68 |
The \fImyaddr\fR and \fImyport\fR arguments allow a client to specify an |
| 69 |
address for the local end of the connection. If \fImyaddr\fR is NULL, then |
| 70 |
an interface is chosen automatically by the operating system. |
| 71 |
If \fImyport\fR is 0, then a port number is chosen at random by |
| 72 |
the operating system. |
| 73 |
.PP |
| 74 |
If \fIasync\fR is zero, the call to \fBTcl_OpenTcpClient\fR returns only |
| 75 |
after the client socket has either successfully connected to the server, or |
| 76 |
the attempted connection has failed. |
| 77 |
If \fIasync\fR is nonzero the socket is connected asynchronously and the |
| 78 |
returned channel may not yet be connected to the server when the call to |
| 79 |
\fBTcl_OpenTcpClient\fR returns. If the channel is in blocking mode and an |
| 80 |
input or output operation is done on the channel before the connection is |
| 81 |
completed or fails, that operation will wait until the connection either |
| 82 |
completes successfully or fails. If the channel is in nonblocking mode, the |
| 83 |
input or output operation will return immediately and a subsequent call to |
| 84 |
\fBTcl_InputBlocked\fR on the channel will return nonzero. |
| 85 |
.PP |
| 86 |
The returned channel is opened for reading and writing. |
| 87 |
If an error occurs in opening the socket, \fBTcl_OpenTcpClient\fR returns |
| 88 |
NULL and records a POSIX error code that can be retrieved |
| 89 |
with \fBTcl_GetErrno\fR. |
| 90 |
In addition, if \fIinterp\fR is non-NULL, an error message |
| 91 |
is left in the interpreter's result. |
| 92 |
.PP |
| 93 |
The newly created channel is not registered in the supplied interpreter; to |
| 94 |
register it, use \fBTcl_RegisterChannel\fR. |
| 95 |
If one of the standard channels, \fBstdin\fR, \fBstdout\fR or \fBstderr\fR was |
| 96 |
previously closed, the act of creating the new channel also assigns it as a |
| 97 |
replacement for the standard channel. |
| 98 |
.SS TCL_MAKETCPCLIENTCHANNEL |
| 99 |
.PP |
| 100 |
\fBTcl_MakeTcpClientChannel\fR creates a \fBTcl_Channel\fR around an |
| 101 |
existing, platform specific, handle for a client TCP socket. |
| 102 |
.PP |
| 103 |
The newly created channel is not registered in the supplied interpreter; to |
| 104 |
register it, use \fBTcl_RegisterChannel\fR. |
| 105 |
If one of the standard channels, \fBstdin\fR, \fBstdout\fR or \fBstderr\fR was |
| 106 |
previously closed, the act of creating the new channel also assigns it as a |
| 107 |
replacement for the standard channel. |
| 108 |
.SS TCL_OPENTCPSERVER |
| 109 |
.PP |
| 110 |
\fBTcl_OpenTcpServer\fR opens a TCP socket on the local host on a specified |
| 111 |
\fIport\fR and uses the Tcl event mechanism to accept requests from clients |
| 112 |
to connect to it. The \fImyaddr\fR argument specifies the network interface. |
| 113 |
If \fImyaddr\fR is NULL the special address INADDR_ANY should be used to |
| 114 |
allow connections from any network interface. |
| 115 |
Each time a client connects to this socket, Tcl creates a channel |
| 116 |
for the new connection and invokes \fIproc\fR with information about |
| 117 |
the channel. \fIProc\fR must match the following prototype: |
| 118 |
.PP |
| 119 |
.CS |
| 120 |
typedef void \fBTcl_TcpAcceptProc\fR( |
| 121 |
ClientData \fIclientData\fR, |
| 122 |
Tcl_Channel \fIchannel\fR, |
| 123 |
char *\fIhostName\fR, |
| 124 |
int \fIport\fR); |
| 125 |
.CE |
| 126 |
.PP |
| 127 |
The \fIclientData\fR argument will be the same as the \fIclientData\fR |
| 128 |
argument to \fBTcl_OpenTcpServer\fR, \fIchannel\fR will be the handle |
| 129 |
for the new channel, \fIhostName\fR points to a string containing |
| 130 |
the name of the client host making the connection, and \fIport\fR |
| 131 |
will contain the client's port number. |
| 132 |
The new channel |
| 133 |
is opened for both input and output. |
| 134 |
If \fIproc\fR raises an error, the connection is closed automatically. |
| 135 |
\fIProc\fR has no return value, but if it wishes to reject the |
| 136 |
connection it can close \fIchannel\fR. |
| 137 |
.PP |
| 138 |
\fBTcl_OpenTcpServer\fR normally returns a pointer to a channel |
| 139 |
representing the server socket. |
| 140 |
If an error occurs, \fBTcl_OpenTcpServer\fR returns NULL and |
| 141 |
records a POSIX error code that can be retrieved with \fBTcl_GetErrno\fR. |
| 142 |
In addition, if the interpreter is non-NULL, an error message |
| 143 |
is left in the interpreter's result. |
| 144 |
.PP |
| 145 |
The channel returned by \fBTcl_OpenTcpServer\fR cannot be used for |
| 146 |
either input or output. |
| 147 |
It is simply a handle for the socket used to accept connections. |
| 148 |
The caller can close the channel to shut down the server and disallow |
| 149 |
further connections from new clients. |
| 150 |
.PP |
| 151 |
TCP server channels operate correctly only in applications that dispatch |
| 152 |
events through \fBTcl_DoOneEvent\fR or through Tcl commands such as |
| 153 |
\fBvwait\fR; otherwise Tcl will never notice that a connection request from |
| 154 |
a remote client is pending. |
| 155 |
.PP |
| 156 |
The newly created channel is not registered in the supplied interpreter; to |
| 157 |
register it, use \fBTcl_RegisterChannel\fR. |
| 158 |
If one of the standard channels, \fBstdin\fR, \fBstdout\fR or \fBstderr\fR was |
| 159 |
previously closed, the act of creating the new channel also assigns it as a |
| 160 |
replacement for the standard channel. |
| 161 |
.SH "PLATFORM ISSUES" |
| 162 |
.PP |
| 163 |
On Unix platforms, the socket handle is a Unix file descriptor as |
| 164 |
returned by the \fBsocket\fR system call. On the Windows platform, the |
| 165 |
socket handle is a \fBSOCKET\fR as defined in the WinSock API. |
| 166 |
.SH "SEE ALSO" |
| 167 |
Tcl_OpenFileChannel(3), Tcl_RegisterChannel(3), vwait(n) |
| 168 |
.SH KEYWORDS |
| 169 |
channel, client, server, socket, TCP |