Sockets. Most modern operating systems use the socket abstraction to manage interprocess communication connections. As we've seen, a socket is similar to a file descriptor. The following system calls all operate on sockets:
socket()
- Create and return the file
descriptor (a small integer) of a new socket. The socket's
address family, connection type and protocol type are specified
in the call.
sendto()
- Send a message through a socket to a
specific destination.
recv()
- Receive a message into a local buffer.
Your task:
The Time Protocol is documented in an RFC. Note that there are several "network time" protocols. Look for the one that uses port 37. Look up the RFC to find out the details such as the message format for the Time Protocol. [Do you see the error in this RFC?]
Note that the representation of numeric variables is not the same across all cpu architectures. As a result, the format of numbers sent over a network was standardized, and so when you write programs to read such numbers over the network, you need to convert them. See byteorder(3SOCKET) for useful conversion utilities.
recvfrom()
to return the address of the
remote host as well as the data value.
recvfrom()
in an infinite loop to watch
for multiple returns (you'll need to use
ctrl-C
to terminate your program).
Identify the responding host and print the time in each
response. Use gethostbyaddr()
,
getnameinfo()
, or
getipnodebyaddr()
to print the
responding host's mnemonic name (rather than IP address
number).