some totals after processing is over. The BEGIN and END sections are optional and take the form BEGIN {action} END {action} These two sections, when present, are delimited by the body of the awk program. You can use them to print a suitable heading at the beginning and the average salary at the end. BUILT-IN VARIABLES Awk has several built-in variables. They are all assigned automatically, though it is also possible for a user to reassign some of them. You have already used NR, which signifies the record number of the current line. We’ll now have a brief look at some of the other variable. The FS Variable: as stated elsewhere, awk uses a contiguous string of spaces as the default field delimiter. FS redefines this field separator, which in the sample database happens to be the |. When used at all, it must occur in the BEGIN section so that the body of the program knows its value before it starts processing: {FS=”|”} This is an alternative to the –F option which does the same thing. The OFS Variable: when you used the print statement with comma-separated arguments, each argument was separated from the other by a space. This is awk’s default output field separator, and can reassigned using the variable OFS in the BEGIN section: { OFS=”~” } When you reassign this variable with a ~ awk will use this character for delimiting the print arguments. This is a useful variable for creating lines with delimited fields. The NF variable: NF comes in quite handy for cleaning up a database of lines that don’t contain the right number of fields. By using it on a file, say emp.lst, you can locate those lines not having 6 fields, and which have crept in due to faulty data entry: $awk ‘BEGIN {FS = “|”} NF! =6 { Print “Record No “, NR, “has”, “fields”}’ empx.lst Dept of ISE, CIT, Gubbi Page no 10 Networks Lab Part-A 2015-16 Experiment No: 1 THREE NODE POINT TO POINT NETWORK Date:
Socket characteristics Sockets are characterized by their domain, type and transport protocol. Common domains are: AF_UNIX: address format is UNIX pathname AF_INET: address format is host and port number Common types are: virtual circuit: received in order transmitted and reliably datagram: arbitrary order, unreliable Each socket type has one or more protocols. Ex: TCP/IP (virtual circuits) UDP (datagram) Use of sockets: sockets communicate client-server: the server waits for a connection from the client Connectionless sockets are peer-to-peer: each process is symmetric. Socket APIs socket: creates a socket of a given domain, type, protocol (buy a phone) bind: assigns a name to the socket (get a telephone number) listen: specifies the number of pending connections that can be queued for a server socket. (call waiting allowance) accept: server accepts a connection request from a client (answer phone) connect: client requests a connection request to a server (call) send, sendto: write to connection (speak) Dept of ISE, CIT, Gubbi Page no44 Networks Lab 2015-16 recv, recvfrom: read from connection (listen) shutdown: end the call Connection-based communication Server performs the following actions socket: create the socket
Where sid: is the socket id addrPtr: is a pointer to the address family dependent address structure len: is the size of *addrPtr Associates a socket id with an address to which other processes can connect. In internet protocol the address is [ipNumber, portNumber] sockaddr For the internet family: struct sockaddr_in { sa_family_t sin_family; // = AF INET in_port_t sin_port; // is a port number struct in_addr sin_addr; // an IP address } Dept of ISE, CIT, Page no46 Networks Lab 2015-16 listen #include <sys / #include <sys / socket.h> int listen (int sid, int size) ; Where size it the number of pending connection requests allowed (typically limited by Unix kernels to 5). Returns the 0 on success, or -1 if failure. accept #include <sys / types.h> #include <sys / socket.h> int accept(int sid ,struct sockaddr *addrPtr , int *lenPtr ) Returns the socketId and address of client connecting to socket. if lenPtr or addrPtr equal zero, no address structure is returned. lenPtr is the maximum size of address structure that can be called, returns the actual value.