Next: , Previous: Using Networking, Up: Using Networking


2.1 gawk's Networking Mechanisms

The ‘|&’ operator for use in communicating with a coprocess is described in Two-way Communications With Another Process. It shows how to do two-way I/O to a separate process, sending it data with print or printf and reading data with getline. If you haven't read it already, you should detour there to do so.

gawk transparently extends the two-way I/O mechanism to simple networking through the use of special file names. When a “coprocess” that matches the special files we are about to describe is started, gawk creates the appropriate network connection, and then two-way I/O proceeds as usual.

At the C, C++, and Perl level, networking is accomplished via sockets, an Application Programming Interface (API) originally developed at the University of California at Berkeley that is now used almost universally for TCP/IP networking. Socket level programming, while fairly straightforward, requires paying attention to a number of details, as well as using binary data. It is not well-suited for use from a high-level language like awk. The special files provided in gawk hide the details from the programmer, making things much simpler and easier to use.

The special file name for network access is made up of several fields, all of which are mandatory:

     /net-type/protocol/localport/hostname/remoteport

The net-type field lets you specify IPv4 versus IPv6, or lets you allow the system to choose.