18.10 Exercise: Ping Server

Team-FLY

The ping command can be used to elicit a response from a remote host. The default for some systems is to just display a message signifying that the host responded. On other systems the default is to indicate how long it took for a reply to be received.

Example 18.32

The following command queries the usp.cs.utsa.edu host.

 ping usp.cs.utsa.edu 

The command might output the following message to mean that the host usp.cs.utsa.edu is responding to network communication.

 usp.cs.utsa.edu is alive 

This section describes an exercise that uses UICI to implement myping , a slightly fancier version of the ping service. The myping function responds with a message such as the following.

 usp.cs.utsa.edu: 5:45am up 12:11, 2 users, load average: 0.14, 0.08, 0.07 

The myping program is a client-server application. A myping server running on the host listens at a well-known port for client requests . The server forks a child to respond to the request. The original server process continues listening. Assume that the myping well-known port number is defined by the constant MYPINGPORT .

Write the code for the myping client. The client takes the host name as a command-line argument, makes a connection to the port specified by MYPINGPORT , reads what comes in on the connection and echoes it to standard output until end-of-file, closes the connection, and exits. Assume that if the connection attempt to the host fails, the client sleeps for SLEEPTIME seconds and then retries. After the number of failed connection attempts exceeds RETRIES , the client outputs the message that the host is not available and exits. Test the program by using the bidirectional server discussed in Example 18.18.

Implement the myping server. The server listens for connections on MYPINGPORT . If a client makes a connection, the server forks a child to handle the request and the original process resumes listening at MYPINGPORT . The child closes the listening file descriptor, calls the process_ping function, closes the communication file descriptor, and exits.

Write a process_ping function with the following prototype.

 int process_ping(int communfd); 

For initial testing, process_ping can just output an error message to the communication file descriptor. For the final implementation, process_ping should construct a message consisting of the host name and the output of the uptime command. An example message is as follows .

 usp.cs.utsa.edu: 5:45am up 13:11, 2 users, load average: 0.14, 0.08, 0.07 

Use uname to get the host name.

  SYNOPSIS  #include <sys/utsname.h>   int uname(struct utsname *name);  POSIX  

If successful, uname returns a nonnegative value. If unsuccessful , uname returns “1 and sets errno . No mandatory errors are defined for uname .

The struct utsname structure, which is defined in sys/utsname.h , has at least the following members .

 char sysname[];    /* name of this OS implementation */ char nodenamep[];  /* name of this node within communication network */ char release[];    /* current release level of this implementation */ char version[];    /* current version level of this release */ char machine[];    /* name of hardware type on which system is running */ 
Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net