gawkinet: TCP Connecting
1
1 2.2 Establishing a TCP Connection
1 =================================
1
1 Let's observe a network connection at work. Type in the following
1 program and watch the output. Within a second, it connects via TCP
1 ('/inet/tcp') to the machine it is running on ('localhost') and asks the
1 service 'daytime' on the machine what time it is:
1
1 BEGIN {
1 "/inet/tcp/0/localhost/daytime" |& getline
1 print $0
1 close("/inet/tcp/0/localhost/daytime")
1 }
1
1 Even experienced 'awk' users will find the second line strange in two
1 respects:
1
1 * A special file is used as a shell command that pipes its output
1 into 'getline'. One would rather expect to see the special file
1 being read like any other file ('getline <
1 "/inet/tcp/0/localhost/daytime")'.
1
1 * The operator '|&' has not been part of any 'awk' implementation
1 (until now). It is actually the only extension of the 'awk'
1 language needed (apart from the special files) to introduce network
1 access.
1
1 The '|&' operator was introduced in 'gawk' 3.1 in order to overcome
1 the crucial restriction that access to files and pipes in 'awk' is
1 always unidirectional. It was formerly impossible to use both access
1 modes on the same file or pipe. Instead of changing the whole concept
1 of file access, the '|&' operator behaves exactly like the usual pipe
1 operator except for two additions:
1
1 * Normal shell commands connected to their 'gawk' program with a '|&'
1 pipe can be accessed bidirectionally. The '|&' turns out to be a
1 quite general, useful, and natural extension of 'awk'.
1
1 * Pipes that consist of a special file name for network connections
1 are not executed as shell commands. Instead, they can be read and
1 written to, just like a full-duplex network connection.
1
1 In the earlier example, the '|&' operator tells 'getline' to read a
1 line from the special file '/inet/tcp/0/localhost/daytime'. We could
1 also have printed a line into the special file. But instead we just
1 read a line with the time, printed it, and closed the connection.
1 (While we could just let 'gawk' close the connection by finishing the
1 program, in this Info file we are pedantic and always explicitly close
1 the connections.)
1