In this chapter, you'll see clients that use sockets to communicate with a number of well-known Internet services such as HTTP, echo, and more. The sockets themselves are simple enough; however, the protocols to communicate with different servers make life complex.
To get a feel for how a protocol operates, you can use Telnet to connect to a server, type different commands to it, and watch its responses. By default, Telnet attempts to connect to port 23. To connect to servers on different ports, specify the port you want to connect to like this:
% telnet localhost 25
| || |
This example assumes that you're using a Unix system. However, Telnet clients are available for all common operating systems, and they are all pretty similar; for example, on Windows, you might have to type the hostname and the port into a dialog box rather than on the command-line, but otherwise , the clients work the same.
This requests a connection to port 25, the SMTP port, on the local machine; SMTP is the protocol used to transfer email between servers or between a mail client and a server. If you know the commands to interact with an SMTP server, you can send email without going through a mail program. This trick can be used to forge email. For example, a few years ago, the summer students at the National Solar Observatory in Sunspot, New Mexico, made it appear that the party one of the scientists was throwing after the annual volleyball match between the staff and the students was in fact a victory party for the students. (Of course, the author of this book had absolutely nothing to do with such despicable behavior. ;-) ) The interaction with the SMTP server went something like this; input the user types is shown in bold (the names have been changed to protect the gullible):
flare% telnet localhost 25 Trying 127.0.0.1 ... Connected to localhost.sunspot.noao.edu. Escape character is '^]'. 220 flare.sunspot.noao.edu Sendmail 4.1/SMI-4.1 ready at Fri, 5 Jul 93 13:13:01 MDT HELO sunspot.noao.edu 250 flare.sunspot.noao.edu Hello localhost [127.0.0.1], pleased to meet you MAIL FROM: bart 250 bart... Sender ok RCPT TO: email@example.com 250 firstname.lastname@example.org... Recipient ok DATA 354 Enter mail, end with "." on a line by itself In a pitiful attempt to reingratiate myself with the students after their inevitable defeat of the staff on the volleyball court at 4:00 P.M., July 24, I will be throwing a victory party for the students at my house that evening at 7:00. Everyone is invited. Beer and Ben-Gay will be provided so the staff may drown their sorrows and assuage their aching muscles after their public humiliation. Sincerely, Bart . 250 Mail accepted QUIT 221 flare.sunspot.noao.edu delivering mail Connection closed by foreign host.
Several members of the staff asked Bart why he, a staff member, was throwing a victory party for the students. The moral of this story is that you should never trust email, especially patently ridiculous email like this, without independent verification. The other moral of this story is that you can use Telnet to simulate a client, see how the client and the server interact, and thus learn what your Java program needs to do. Although this session doesn't demonstrate all the features of the SMTP protocol, it's sufficient to enable you to deduce how a simple email client talks to a server.