gawk: TCP/IP Networking
1
1 12.4 Using 'gawk' for Network Programming
1 =========================================
1
1 'EMRED':
1 A host is a host from coast to coast,
1 and nobody talks to a host that's close,
1 unless the host that isn't close
1 is busy, hung, or dead.
1 -- _Mike O'Brien (aka Mr. Protocol)_
1
1 In addition to being able to open a two-way pipeline to a coprocess
1 on the same system (⇒Two-way I/O), it is possible to make a
1 two-way connection to another process on another system across an IP
1 network connection.
1
1 You can think of this as just a _very long_ two-way pipeline to a
1 coprocess. The way 'gawk' decides that you want to use TCP/IP
1 networking is by recognizing special file names that begin with one of
1 '/inet/', '/inet4/', or '/inet6/'.
1
1 The full syntax of the special file name is
1 '/NET-TYPE/PROTOCOL/LOCAL-PORT/REMOTE-HOST/REMOTE-PORT'. The components
1 are:
1
1 NET-TYPE
1 Specifies the kind of Internet connection to make. Use '/inet4/'
1 to force IPv4, and '/inet6/' to force IPv6. Plain '/inet/' (which
1 used to be the only option) uses the system default, most likely
1 IPv4.
1
1 PROTOCOL
1 The protocol to use over IP. This must be either 'tcp', or 'udp',
1 for a TCP or UDP IP connection, respectively. TCP should be used
1 for most applications.
1
1 LOCAL-PORT
1 The local TCP or UDP port number to use. Use a port number of '0'
1 when you want the system to pick a port. This is what you should
1 do when writing a TCP or UDP client. You may also use a well-known
1 service name, such as 'smtp' or 'http', in which case 'gawk'
1 attempts to determine the predefined port number using the C
1 'getaddrinfo()' function.
1
1 REMOTE-HOST
1 The IP address or fully qualified domain name of the Internet host
1 to which you want to connect.
1
1 REMOTE-PORT
1 The TCP or UDP port number to use on the given REMOTE-HOST. Again,
1 use '0' if you don't care, or else a well-known service name.
1
1 NOTE: Failure in opening a two-way socket will result in a nonfatal
1 error being returned to the calling code. The value of 'ERRNO'
1 indicates the error (⇒Auto-set).
1
1 Consider the following very simple example:
1
1 BEGIN {
1 Service = "/inet/tcp/0/localhost/daytime"
1 Service |& getline
1 print $0
1 close(Service)
1 }
1
1 This program reads the current date and time from the local system's
1 TCP 'daytime' server. It then prints the results and closes the
1 connection.
1
1 Because this topic is extensive, the use of 'gawk' for TCP/IP
1 programming is documented separately. See *note(General Introduction,
1 gawkinet, TCP/IP Internetworking with 'gawk')Top::, for a much more
1 complete introduction and discussion, as well as extensive examples.
1
1 NOTE: 'gawk' can only open direct sockets. There is currently no
1 way to access services available over Secure Socket Layer (SSL);
1 this includes any web service whose URL starts with 'https://'.
1