Tcl_OpenTcpClient(3tcl)
π Tcl_OpenTcpClient β a.k.a. "the TCP socket channel squad" π
okay bestie, so this is one of those pages from the Tcl Library Procedures collection (it lives in section 3tcl, iykyk) and it's about four little functions whose whole entire job is opening channels over TCP sockets. no cap, they are so convenient. π«Ά
β¨ NAME β¨
Tcl_OpenTcpClient, Tcl_MakeTcpClientChannel, Tcl_OpenTcpServer, Tcl_OpenTcpServerEx β procedures to open channels using TCP sockets. literally the whole friend group. π
π SYNOPSIS π
#include <tcl.h>
Tcl_Channel
Tcl_OpenTcpClient(interp, port, host, myaddr, myport, async)
Tcl_Channel
Tcl_MakeTcpClientChannel(sock)
Tcl_Channel
Tcl_OpenTcpServer(interp, port, myaddr, proc, clientData)
Tcl_Channel
Tcl_OpenTcpServerEx(interp, service, myaddr, flags, backlog, proc, clientData)
π ARGUMENTS π
Tcl_Interp *interp(in) β the Tcl interpreter you wanna use for error reporting. If it's non-NULL and something goes wrong, the error message gets left in the interpreter's result. she's the messenger fr. π£οΈint port(in) β a port number to connect to as a client, or to listen on as a server. pick your lane. β¨const char *service(in) β a string specifying the port number to connect to as a client or to listen on as a server. yes it's a string this time, she's flexible like that. π¦const char *host(in) β a string with a host name or address for the remote end of the connection. the other side of the call, if you will. πint myport(in) β port number for the client's end of the socket. if it's0, a port number gets allocated at random. chaos mode, but make it legal. π²const char *myaddr(in) β a string with the host name or address for the network interface to use for the local end of the connection. ifNULL, a default interface gets chosen. let the system pick, she has taste. πint async(in) β if nonzero, the client socket gets connected asynchronously to the server. patient? not her. β‘int backlog(in) β length of the OS listen backlog queue. use-1for the default value. that's the note, bestie,-1. πunsigned flags(in) β an ORed combination ofTCL_TCPSERVER_*flags that specify extra information about the socket being created. π©void *sock(in) β platform-specific handle for a client TCP socket. yes, platform-specific, we'll get to that. πTcl_TcpAcceptProc *proc(in) β pointer to a procedure to invoke each time a new connection is accepted via the socket. the welcome committee. πvoid *clientData(in) β arbitrary one-word value to pass toproc. little gift, hand-delivered. π
π DESCRIPTION π
So these functions are convenience procedures for creating channels that communicate over TCP sockets. and the operations you can do on a channel? those are described in the manual entry for Tcl_OpenFileChannel. go read her too, she's iconic. π
π§ TCL_OPENTCPCLIENT
Tcl_OpenTcpClient opens a client TCP socket connected to a port on a specific host, and returns a channel that you can use to communicate with the server. the host to connect to can be specified either as a domain name style name (e.g. www.sunlabs.com), or as a string containing the alphanumeric representation of its four-byte address (e.g. 127.0.0.1). and pssst β use the string localhost to connect to a TCP socket on the host on which the function is invoked. self-love, truly. πͺ
The myaddr and myport arguments let a client specify an address for the local end of the connection. if myaddr is NULL, then an interface is chosen automatically by the operating system. if myport is 0, then a port number is chosen at random by the operating system. she's got a whole system doing her scheduling, honestly. π
If async is zero, the call to Tcl_OpenTcpClient returns only after the client socket has either successfully connected to the server, or the attempted connection has failed. no in-between, no "he's typingβ¦" energy. β
If async is nonzero, the socket is connected asynchronously and the returned channel may not yet be connected to the server when the call to Tcl_OpenTcpClient returns. If the channel is in blocking mode and an input or output operation is done on the channel before the connection is completed or fails, that operation will wait until the connection either completes successfully or fails. If the channel is in nonblocking mode, the input or output operation will return immediately and a subsequent call to Tcl_InputBlocked on the channel will return nonzero. so like, she's technically there but she hasn't fully arrived yet. π
The returned channel is opened for reading and writing. If an error occurs in opening the socket, Tcl_OpenTcpClient returns NULL and records a POSIX error code that can be retrieved with Tcl_GetErrno. In addition, if interp is non-NULL, an error message is left in the interpreter's result. receipts on receipts. π§Ύ
The newly created channel is not registered in the supplied interpreter; to register it, use Tcl_RegisterChannel. and if one of the standard channels, stdin, stdout or stderr was previously closed, the act of creating the new channel also assigns it as a replacement for the standard channel. glow-up assignment, honestly. β¨
π¦ TCL_MAKETCPCLIENTCHANNEL
Tcl_MakeTcpClientChannel creates a Tcl_Channel around an existing, platform specific, handle for a client TCP socket. like thrifting β you already have the socket, she just gives it a cute wrapper. ποΈ
The newly created channel is not registered in the supplied interpreter; to register it, use Tcl_RegisterChannel. If one of the standard channels, stdin, stdout or stderr was previously closed, the act of creating the new channel also assigns it as a replacement for the standard channel. (yes bestie, same paragraph, same rules, she's consistent.) π«Ά
πΈ TCL_OPENTCPSERVER
Tcl_OpenTcpServer opens a TCP socket on the local host on a specified port and uses the Tcl event mechanism to accept requests from clients to connect to it. so she's hosting the party and the event loop is the bouncer. π
The myaddr argument specifies the network interface. If myaddr is NULL the special address INADDR_ANY should be used to allow connections from any network interface. open invite, no list. π
Each time a client connects to this socket, Tcl creates a channel for the new connection and invokes proc with information about the channel. proc must match the following prototype:
typedef void Tcl_TcpAcceptProc(
void *clientData,
Tcl_Channel channel,
char *hostName,
int port);
The clientData argument will be the same as the clientData argument to Tcl_OpenTcpServer, channel will be the handle for the new channel, hostName points to a string containing the name of the client host making the connection, and port will contain the client's port number. The new channel is opened for both input and output. If proc raises an error, the connection is closed automatically. proc has no return value, but if it wishes to reject the connection it can close channel. ok so she can ghost, that's canon. π
Tcl_OpenTcpServer normally returns a pointer to a channel representing the server socket. If an error occurs, Tcl_OpenTcpServer returns NULL and records a POSIX error code that can be retrieved with Tcl_GetErrno. In addition, if the interpreter is non-NULL, an error message is left in the interpreter's result.
The channel returned by Tcl_OpenTcpServer cannot be used for either 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 disallow further connections from new clients. she's a doorman, not a gossip. πͺ
TCP server channels operate correctly only in applications that dispatch events throughTcl_DoOneEventor through Tcl commands such asvwait; otherwise Tcl will never notice that a connection request from a remote client is pending. this one is IMPORTANT bestie, no event loop, no connections. periodt. π
The newly created channel is not registered in the supplied interpreter; to register it, use Tcl_RegisterChannel. If one of the standard channels, stdin, stdout or stderr was previously closed, the act of creating the new channel also assigns it as a replacement for the standard channel.
π TCL_OPENTCPSERVEREX
Tcl_OpenTcpServerEx behaviour is identical to Tcl_OpenTcpServer but gives more flexibility to the user by providing a mean to further customize some aspects of the socket via the flags parameter. Available flags (dependent on platform) are TCL_TCPSERVER_REUSEADDR TCL_TCPSERVER_REUSEPORT. same girl, extra accessories. π
π PLATFORM ISSUES π
On Unix platforms, the socket handle is a Unix file descriptor as returned by the socket system call. On the Windows platform, the socket handle is a SOCKET as defined in the WinSock API. two different worlds, same slay. πβ¨
π SEE ALSO π
β KEYWORDS β
channel, client, server, socket, TCP
okay that's the whole page bestie, go open a socket, register your channel, and don't forget to actually dispatch events or nothing will ever happen. π
this was brought to you by the Tcl crew, version 9.0, dated on the Tcl_OpenTcpClient(3tcl) page itself β mwah, ty for reading, stan a TCP channel today. πβ¨
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1996-7 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms