Wednesday, August 17, 2011

How to write a TCP/IP client program

To keep the client program short and allow you to focus upon the basic principles, the demonstration program is going to connect to an existing service you have running on your system. The client program will connect to your daytime service to retrieve the current date and time string. Before the program is presented, however, you should make sure that this service is enabled and operational on your system. As a first step, perform the following:

Output
[sgupta@rhel55x86 chap6]$ grep daytime /etc/services
daytime         13/tcp
daytime         13/udp
[sgupta@rhel55x86 chap6]$


You should see that your system recognizes the daytime Internet service and that it is available on port 13 using tcp. The first line of grep output confirms this for you.

TIP
The telnet program can often be used to perform simple tests with TCP/IP servers. It is very important, however, to remember to specify the port number after the IP number (or hostname) on the command line. Otherwise, the port number will default to 23, which is the telnet service! To test the daytime service, for example, you must specify the port number 13 after the IP number on the command line. The next step is to make sure it is operational. The telnet program is a program that is often usable for simple tests when TCP/IP is used. To test that the daytime service is running, you should be able to perform the following:

Output
$ telnet 127.0.0.1 13
Trying 127.0.0.1 . . .
Connected to 127.0.0.1.
Escape character is '^]'.
Tue Aug 17 17:59:30 1999
Connection closed by foreign host.
$

Make sure you specify the protocol number 13 after the IP number 127.0.0.1 (you can use a remote IP number, but for this testing procedure stick to 127.0.0.1). If your daytime service is
running, you should get a date and time string displayed, which is followed by the message "Connection closed by foreign host."
If the service is not available, you will see output similar to this:

Output
$ telnet 127.0.0.1 13
Trying 127.0.0.1 . . .
telnet: Unable to connect to remote host: Connection refused
$

If you do, then this indicates that your daytime service is not running. To troubleshoot this problem, examine your /etc/inetd.conf file:

Output
$ grep daytime /etc/inetd.conf
# Echo, discard, daytime, and chargen are used
#daytime stream tcp nowait root internal
#daytime dgram udp wait root internal
$

As shown in this case, the daytime service entry in the file has a # character in the first column. This effectively "comments out" the service, which makes it unavailable. This may have been done as a precaution against attacks from the Internet or other hostile users in your network (it's a general principle to disable any Internet service that you do not deem as necessary). To try out the client example program, you will need to enable the tcp daytime service entry (the udp service entry can be left commented out if it is already). To fix the service, edit the file /etc/inetd.conf by removing the leading # character for the daytime entry that includes the protocol tcp in it. Check it with grep again, and you should see
something like the following:

Output
$ grep daytime /etc/inetd.conf
# Echo, discard, daytime, and chargen are used
daytime stream tcp nowait root internal
#daytime dgram udp wait root internal
$

After making changes to the /etc/inetd.conf file, you must tell the inetd daemon to re-read and reprocess the changed file. This is done as follows:

Output
$ su -
Password:
# ps ax | grep inetd
313 ? S 0:00 inetd
828 pts/1 S 0:00 grep inetd
# kill -1 313
#

CAUTION
Symbolic signal names in commands such as the kill command are being promoted these days. One reason to use these symbolic symbol names is for safety against typing errors. For example, the command "kill -1 313" can be typed as:

kill -HUP 313

Some users (author included) prefer to live dangerously and have resisted making this change.

The above session accomplishes the following:
  1. The su command is used to change to the root account.
  2. Then you find out what the process ID of the inetd daemon is. The ps command indicates in the example that the process ID is 313 for the inetd daemon process (your process ID may be different).
  3. The kill -1 313 command is used to send the signal SIGHUP to process ID 313 (your process ID may be different). Be sure to not forget the -1 (or -HUP) argument on the command line. Otherwise, you'll kill off your inetd daemon!

Having done all of this, you should now be able to repeat the telnet test and verify that it works.

Example


No comments:

Post a Comment