1Tcl_OpenTcpClient(3) Tcl Library Procedures Tcl_OpenTcpClient(3)
2
3
4
5______________________________________________________________________________
6
8 Tcl_OpenTcpClient, Tcl_MakeTcpClientChannel, Tcl_OpenTcpServer - proce‐
9 dures to open channels using TCP sockets
10
12 #include <tcl.h>
13
14 Tcl_Channel
15 Tcl_OpenTcpClient(interp, port, host, myaddr, myport, async)
16
17 Tcl_Channel
18 Tcl_MakeTcpClientChannel(sock)
19
20 Tcl_Channel
21 Tcl_OpenTcpServer(interp, port, myaddr, proc, clientData)
22
23
25 Tcl_Interp *interp (in) Tcl interpreter to use
26 for error reporting. If
27 non-NULL and an error
28 occurs, an error message
29 is left in the inter‐
30 preter's result.
31
32 int port (in) A port number to connect
33 to as a client or to
34 listen on as a server.
35
36 CONST char *host (in) A string specifying a
37 host name or address for
38 the remote end of the
39 connection.
40
41 int myport (in) A port number for the
42 client's end of the
43 socket. If 0, a port
44 number is allocated at
45 random.
46
47 CONST char *myaddr (in) A string specifying the
48 host name or address for
49 network interface to use
50 for the local end of the
51 connection. If NULL, a
52 default interface is
53 chosen.
54
55 int async (in) If nonzero, the client
56 socket is connected
57 asynchronously to the
58 server.
59
60 ClientData sock (in) Platform-specific handle
61 for client TCP socket.
62
63 Tcl_TcpAcceptProc *proc (in) Pointer to a procedure
64 to invoke each time a
65 new connection is
66 accepted via the socket.
67
68 ClientData clientData (in) Arbitrary one-word value
69 to pass to proc.
70_________________________________________________________________
71
72
74 These functions are convenience procedures for creating channels that
75 communicate over TCP sockets. The operations on a channel are
76 described in the manual entry for Tcl_OpenFileChannel.
77
78
80 Tcl_OpenTcpClient opens a client TCP socket connected to a port on a
81 specific host, and returns a channel that can be used to communicate
82 with the server. The host to connect to can be specified either as a
83 domain name style name (e.g. www.sunlabs.com), or as a string contain‐
84 ing the alphanumeric representation of its four-byte address (e.g.
85 127.0.0.1). Use the string localhost to connect to a TCP socket on the
86 host on which the function is invoked.
87
88 The myaddr and myport arguments allow a client to specify an address
89 for the local end of the connection. If myaddr is NULL, then an inter‐
90 face is chosen automatically by the operating system. If myport is 0,
91 then a port number is chosen at random by the operating system.
92
93 If async is zero, the call to Tcl_OpenTcpClient returns only after the
94 client socket has either successfully connected to the server, or the
95 attempted connection has failed. If async is nonzero the socket is
96 connected asynchronously and the returned channel may not yet be con‐
97 nected to the server when the call to Tcl_OpenTcpClient returns. If the
98 channel is in blocking mode and an input or output operation is done on
99 the channel before the connection is completed or fails, that operation
100 will wait until the connection either completes successfully or fails.
101 If the channel is in nonblocking mode, the input or output operation
102 will return immediately and a subsequent call to Tcl_InputBlocked on
103 the channel will return nonzero.
104
105 The returned channel is opened for reading and writing. If an error
106 occurs in opening the socket, Tcl_OpenTcpClient returns NULL and
107 records a POSIX error code that can be retrieved with Tcl_GetErrno. In
108 addition, if interp is non-NULL, an error message is left in the inter‐
109 preter's result.
110
111 The newly created channel is not registered in the supplied inter‐
112 preter; to register it, use Tcl_RegisterChannel. If one of the stan‐
113 dard channels, stdin, stdout or stderr was previously closed, the act
114 of creating the new channel also assigns it as a replacement for the
115 standard channel.
116
117
119 Tcl_MakeTcpClientChannel creates a Tcl_Channel around an existing,
120 platform specific, handle for a client TCP socket.
121
122 The newly created channel is not registered in the supplied inter‐
123 preter; to register it, use Tcl_RegisterChannel. If one of the stan‐
124 dard channels, stdin, stdout or stderr was previously closed, the act
125 of creating the new channel also assigns it as a replacement for the
126 standard channel.
127
128
130 Tcl_OpenTcpServer opens a TCP socket on the local host on a specified
131 port and uses the Tcl event mechanism to accept requests from clients
132 to connect to it. The myaddr argument specifies the network interface.
133 If myaddr is NULL the special address INADDR_ANY should be used to
134 allow connections from any network interface. Each time a client con‐
135 nects to this socket, Tcl creates a channel for the new connection and
136 invokes proc with information about the channel. Proc must match the
137 following prototype:
138 typedef void Tcl_TcpAcceptProc(
139 ClientData clientData,
140 Tcl_Channel channel,
141 char *hostName,
142 int port);
143
144 The clientData argument will be the same as the clientData argument to
145 Tcl_OpenTcpServer, channel will be the handle for the new channel,
146 hostName points to a string containing the name of the client host mak‐
147 ing the connection, and port will contain the client's port number.
148 The new channel is opened for both input and output. If proc raises an
149 error, the connection is closed automatically. Proc has no return
150 value, but if it wishes to reject the connection it can close channel.
151
152 Tcl_OpenTcpServer normally returns a pointer to a channel representing
153 the server socket. If an error occurs, Tcl_OpenTcpServer returns NULL
154 and records a POSIX error code that can be retrieved with Tcl_GetErrno.
155 In addition, if the interpreter is non-NULL, an error message is left
156 in the interpreter's result.
157
158 The channel returned by Tcl_OpenTcpServer cannot be used for either
159 input or output. It is simply a handle for the socket used to accept
160 connections. The caller can close the channel to shut down the server
161 and disallow further connections from new clients.
162
163 TCP server channels operate correctly only in applications that dis‐
164 patch events through Tcl_DoOneEvent or through Tcl commands such as
165 vwait; otherwise Tcl will never notice that a connection request from a
166 remote client is pending.
167
168 The newly created channel is not registered in the supplied inter‐
169 preter; to register it, use Tcl_RegisterChannel. If one of the stan‐
170 dard channels, stdin, stdout or stderr was previously closed, the act
171 of creating the new channel also assigns it as a replacement for the
172 standard channel.
173
174
176 On Unix platforms, the socket handle is a Unix file descriptor as │
177 returned by the socket system call. On the Windows platform, the │
178 socket handle is a SOCKET as defined in the WinSock API. On the Macin‐ │
179 tosh platform, the socket handle is a StreamPtr.
180
181
183 Tcl_OpenFileChannel(3), Tcl_RegisterChannel(3), vwait(n)
184
185
187 client, server, TCP
188
189
190
191Tcl 8.0 Tcl_OpenTcpClient(3)