Simple TCP/IP time client
This program demonstrates a simple TCP/IP client. It will connect to a time server and display the current time.
Programming Issues
This program illustrates the classic process for a TCP/IP client program. Summarised, it is as follows:
- Create a socket with a call to
socket()
- Create and initialize a socket address structure
with the address family (
AF_INET
here), the server's remote IP address, and the port the server is listening on. - Actively connect to the server by calling
connect()
- Communicate with the server, and (in this case) allow the server to perform the active close.
Usage
Launch a server process in the background (or find one already running) and run the program to connect to it:
[paul@localhost paul]$ ./timeserv 3357 & [paul@localhost paul]$ ./timeclnt -a 127.0.0.1 -p 3357 The current time is: Sat Oct 16 19:26:17 1999 [paul@localhost paul]$
Use the -a
and -p
switches to
specify the remote IP address and port of the server.