21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

172 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>print ... |& command...# start two-way pipeUsing ptys avoids the buffer deadlock issues described earlier, at some loss in performance.If your system does not have ptys, or if all the system’s ptys are in use, gawk automaticallyfalls back to using regular pipes.10.3 Using gawk for Network <strong>Programming</strong>EMISTERED: A host is a host from coast to coast,and no-one can talk to host that’s close,unless the host that isn’t closeis busy hung or dead.In addition to being able to open a two-way pipeline to a coprocess on the same system(see Section 10.2 [Two-Way Communications with Another Process], page 170), it is possibleto make a two-way connection to another process on another system across an IP networkingconnection.You can think of this as just a very long two-way pipeline to a coprocess. The way gawkdecides that you want to use TCP/IP networking is by recognizing special file names thatbegin with ‘/inet/’.The full syntax of the special file name is ‘/inet/protocol/local-port/remotehost/remote-port’.The components are:protocollocal-portThe protocol to use over IP. This must be either ‘tcp’, ‘udp’, or ‘raw’, for aTCP, UDP, or raw IP connection, respectively. The use of TCP is recommendedfor most applications.Caution: The use of raw sockets is not currently supported in version 3.1 ofgawk.The local TCP or UDP port number to use. Use a port number of ‘0’ whenyou want the system to pick a port. This is what you should do when writinga TCP or UDP client. You may also use a well-known service name, such as‘smtp’ or ‘http’, in which case gawk attempts to determine the predefined portnumber using the C getservbyname function.remote-hostThe IP address or fully-qualified domain name of the Internet host to whichyou want to connect.remote-portThe TCP or UDP port number to use on the given remote-host. Again, use ‘0’if you don’t care, or else a well-known service name.NOTE: Failure in opening a two-way socket will result in a non-fatal error beingreturned to the calling function.Consider the following very simple example:BEGIN {Service = "/inet/tcp/0/localhost/daytime"Service |& getline

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!