📄 ch18.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 18 -- Using Internet Protocols</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT SIZE=6 COLOR=#FF0000>Chapter 18</FONT></H1>
<H1><FONT SIZE=6 COLOR=#FF0000>Using Internet Protocols</FONT>
</H1>
<HR>
<P>
<CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER>
<UL>
<LI><A HREF="#Sockets">
Sockets</A>
<UL>
<LI><A HREF="#TheServerSideofaConversation">
The Server Side of a Conversation</A>
<LI><A HREF="#TheClientSideofaConversation">
The Client Side of a Conversation</A>
</UL>
<LI><A HREF="#UsingtheTimeService">
Using the Time Service</A>
<LI><A HREF="#SendingMailSMTP">
Sending Mail (SMTP)</A>
<UL>
<LI><A HREF="#TheTTFONTSIZEFACECourierMAILFONTTTFONTSIZECommandFONT">
The <TT>MAIL</TT>
Command</FONT></A>
<LI><A HREF="#TheTTFONTSIZEFACECourierRCPTFONTTTFONTSIZECommandFONT">
The <TT>RCPT </TT>Command</FONT>
</A>
<LI><A HREF="#TheTTFONTSIZEFACECourierDATAFONTTTFONTSIZECommandFONT">
The <TT>DATA</TT>
Command</FONT></A>
<LI><A HREF="#ReportingUndeliverableMail">
Reporting Undeliverable Mail</A>
<LI><A HREF="#UsingPerltoSendMail">
Using Perl to Send Mail</A>
</UL>
<LI><A HREF="#ReceivingMailPOP">
Receiving Mail (POP)</A>
<LI><A HREF="#CheckingforUpnessEcho">
Checking for Upness (Echo)</A>
<LI><A HREF="#TransferringFilesFTP">
Transferring Files (FTP)</A>
<LI><A HREF="#ReadingtheNewsNNTP">
Reading the News (NNTP)</A>
<LI><A HREF="#TheWorldWideWebHTTP">
The World Wide Web (HTTP)</A>
<LI><A HREF="#Summary">
Summary</A>
<LI><A HREF="#ReviewQuestions">
Review Questions</A>
<LI><A HREF="#ReviewExercises">
Review Exercises</A>
</UL>
<HR>
<P>
One of the reasons the Internet has blossomed so quickly is because
everyone can understand the <I>protocols</I> that are spoken on
the net. A protocol is a set of commands and responses. There
are two layers of protocols that I'll mention here. The low-level
layer is called TCP/IP and while it is crucial to the Internet,
we can effectively ignore it. The high-level protocols like ftp,
smtp, pop, http, and telnet are what you'll read about in this
chapter. They use TCP/IP as a facilitator to communicate between
computers. The protocols all have the same basic pattern:
<UL>
<LI>Begin a Conversation-Your computer (the client) starts a conversation
with another computer (the server).
<LI>Hold a Conversation-During the conversation, commands are
sent and acknowledged.
<LI>End a Conversation-The conversation is terminated.
</UL>
<P>
Figure 18.1 is what the protocol for sending mail looks like.
The end-user creates a mail message and then the sending system
uses the mail protocol to hold a conversation with the receiving
system.
<P>
<A HREF="f18-1.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/f18-1.gif"><B>Figure 18.1 : </B><I>All protocols follow this Communications
model</I>.</A>
<P>
Internet conversations are done with sockets, in a manner similar
to using the telephone or shouting out a window. I won't kid you,
sockets are a complicated subject. They are discussed in the "Sockets"
section that follows. Fortunately, you only have to learn about
a small subset of the socket fuNCtionality in order to use the
high-level protocols.
<P>
Table 18.1 provides a list of the high-level protocols that you
can use. This chapter will not be able to cover them all, but
if you'd like to investigate further, the protocols are detailed
in documents at the <B>http://ds.internic.net/ds/dspg0 intdoc.html</B>
Web site.<BR>
<CENTER><B>Table 18.1 A Small Sampling of Protocols</B></CENTER>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD WIDTH=75><CENTER><I>Protocol</I></CENTER></TD><TD WIDTH=73><CENTER><I>Number</I></CENTER>
</TD><TD WIDTH=442><I>Description</I></TD></TR>
<TR><TD WIDTH=75><CENTER>auth</CENTER></TD><TD WIDTH=73><CENTER>113</CENTER>
</TD><TD WIDTH=442>Authentication</TD></TR>
<TR><TD WIDTH=75><CENTER>echo</CENTER></TD><TD WIDTH=73><CENTER> 7</CENTER>
</TD><TD WIDTH=442>Checks server to see if they are running</TD>
</TR>
<TR><TD WIDTH=75><CENTER>finger</CENTER></TD><TD WIDTH=73><CENTER> 79</CENTER>
</TD><TD WIDTH=442>Lets you retrieve information about a user
</TD></TR>
<TR><TD WIDTH=75><CENTER>ftp</CENTER></TD><TD WIDTH=73><CENTER> 21</CENTER>
</TD><TD WIDTH=442>File Transfer Protocol</TD></TR>
<TR><TD WIDTH=75><CENTER>nntp</CENTER></TD><TD WIDTH=73><CENTER> 119</CENTER>
</TD><TD WIDTH=442>Network News Transfer Protocol - Usenet News Groups
</TD></TR>
<TR><TD WIDTH=75><CENTER>pop</CENTER></TD><TD WIDTH=73><CENTER> 109</CENTER>
</TD><TD WIDTH=442>Post Office Protocol - iNComing mail</TD></TR>
<TR><TD WIDTH=75><CENTER>smtp</CENTER></TD><TD WIDTH=73><CENTER> 25</CENTER>
</TD><TD WIDTH=442>Simple Mail Transfer Protocol - outgoing mail
</TD></TR>
<TR><TD WIDTH=75><CENTER>time</CENTER></TD><TD WIDTH=73><CENTER> 37</CENTER>
</TD><TD WIDTH=442>Time Server</TD></TR>
<TR><TD WIDTH=75><CENTER>telnet</CENTER></TD><TD WIDTH=73><CENTER> 23</CENTER>
</TD><TD WIDTH=442>Lets you connect to a host and use it as if you were a directly connected terminal
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Each protocol is also called a service. HeNCe the term, mail server
or ftp server. Underlying all of the high-level protocols is the
very popular Transfer Control Protocol/Internet Protocol or TCP/IP.
You don't need to know about TCP/IP in order to use the high-level
protocols. All you need to know is that TCP/IP enables a server
to <I>listen</I> and respond to an iNComing conversation. INComing
conversations arrive at something called a port. A <I>Port</I>
is an imaginary place where iNComing packets of information can
arrive (just like a ship arrives at a sea port). Each type of
service (for example, mail or file transfer) has its own port
number.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If you have access to a UNIX machine, look at the <TT>/etc/services</TT> file for a list of the services and their assigned port numbers. Users of Windows 95-and, I suspect Windows NT-can look in \windows\services.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
In this chapter, we take a quick look at sockets, and then turn
our attention to examples that use them. You see how to send and
receive mail. Sending mail is done using the Simple Mail Transfer
Protocol (SMTP), which is detailed in an RFC numbered 821. Receiving
mail is done using the Post Office Protocol (POP) as detailed
in RFC 1725.
<H2><A NAME="Sockets"><FONT SIZE=5 COLOR=#FF0000>
Sockets</FONT></A></H2>
<P>
<I>Sockets</I> are the low-level links that enable Internet conversations.
There are a whole slew of fuNCtions that deal with sockets. Fortunately,
you don't normally need to deal with them all. A small subset
is all you need to get started. This section will focus in on
those aspects of sockets that are useful in Perl. There will be
whole areas of sockets that I won't mention.
<P>
Table 18.2 lists all of the Perl fuNCtions that relate to sockets
so you have a handy refereNCe. But remember, you probably won't
need them all.<BR>
<CENTER><B>Table 18.2 Perl's Socket FuNCtions</B></CENTER>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD WIDTH=319><I>FuNCtion</I></TD><TD WIDTH=271><I>Description</I>
</TD></TR>
<TR><TD WIDTH=319>accept(NEWSOCKET, SOCKET)</TD><TD WIDTH=271>Accepts a socket connection from clients waiting for a connection. The original socket, <TT>SOCKET</TT>, is left along, and a new socket is created for the remote process to talk with.
<TT>SOCKET</TT> must have already been opened using the <TT>socket()</TT> fuNCtion. Returns true if it succeeded, false otherwise.
</TD></TR>
<TR><TD WIDTH=319>bind(SOCKET, PACKED_ADDRESS)</TD><TD WIDTH=271>Binds a network address to the socket handle. Returns true if it succeeded, false otherwise.
</TD></TR>
<TR><TD WIDTH=319>connect(SOCKET, PACKED_ADDRESS)</TD><TD WIDTH=271>Attempts to connect to a socket. Returns true if it succeeded, false otherwise.
</TD></TR>
<TR><TD WIDTH=319>getpeername(SOCKET)</TD><TD WIDTH=271>Returns the packed address of the remote side of the connection. This fuNCtion can be used to reject connections for security reasons, if needed.
</TD></TR>
<TR><TD WIDTH=319>getsockname(SOCKET)</TD><TD WIDTH=271>Returns the packed address of the local side of the connection.
</TD></TR>
<TR><TD WIDTH=319>getsockopt(SOCKET, LEVEL, OPTNAME)</TD><TD WIDTH=271>Returns the socket option requested, or undefined if there is an error.
</TD></TR>
<TR><TD WIDTH=319>listen(SOCKET, QUEUESIZE)</TD><TD WIDTH=271>Creates a queue for <TT>SOCKET</TT> with <TT>QUEUESIZE</TT> slots. Returns true if it succeeded, false otherwise.
</TD></TR>
<TR><TD WIDTH=319>recv(SOCKET, BUFFER, LEN, FLAGS)</TD><TD WIDTH=271>Attempts to receive <TT>LENGTH</TT> bytes of data into a buffer from <TT>SOCKET</TT>. Returns the address of the sender, or the undefined value if there's an error. <TT>BUFFER</TT> will
be grown or shrunk to the length actually read. However, you must initalize BUFFER before use. For example <TT>my($buffer) = '';</TT>.
</TD></TR>
<TR><TD WIDTH=319>select(RBITS, WBITS, EBITS, TIMEOUT)</TD><TD WIDTH=271>Examines file descriptors to see if they are ready or if they have exception conditions pending.
</TD></TR>
<TR><TD WIDTH=319>send(SOCKET, BUFFER, FLAGS, [TO])</TD><TD WIDTH=271>Sends a message to a socket. On uNConnected sockets you must specify a destination (the <TT>TO</TT> parameter). Returns the number of characters sent, or the undefined value if there is
an error.
</TD></TR>
<TR><TD WIDTH=319>setsockopt(SOCKET, LEVEL, OPTNAME, OPTVAL)
</TD><TD WIDTH=271>Sets the socket option requested. Returns undefined if there is an error. <TT>OPTVAL</TT> may be specified as undefined if you don't want to pass an argument.
</TD></TR>
<TR><TD WIDTH=319>shutdown(SOCKET, HOW)</TD><TD WIDTH=271>Shuts down a socket connection in the manner indicated by HOW. If <TT>HOW</TT> = 0, all iNComing information will be ignored. If <TT>HOW </TT>= 1, all outgoing information will be stopped. If
<TT>HOW</TT> = 2, then both sending and receiving is disallowed.
</TD></TR>
<TR><TD WIDTH=319>socket(SOCKET, DOMAIN,TYPE, PROTOCOL)</TD><TD WIDTH=271>Opens a specific <TT>TYPE</TT> of socket and attaches it to the name <TT>SOCKET</TT>. See "The Server Side of a Conversation" for more details. Returns true if successful,
false if not.
</TD></TR>
<TR><TD WIDTH=319>socketpair(SOCK1, SOCK2, DOMAIN, TYPE, PROTO)
</TD><TD WIDTH=271>Creates an unnamed pair of sockets in the specified domain, of the specified type. Returns true if successful, false if not.
</TD></TR>
</TABLE>
</CENTER>
<P>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If you are interested in knowing everything about sockets, you need to get your hands on some UNIX documentation. The Perl set of socket fuNCtions are pretty much a duplication of those available using the C language under UNIX. Only the parameters are
different because Perl data structures are handled differently. You can find UNIX documentation at <B>http://www.delorie.com/gnu/docs/</B> on the World Wide Web.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Programs that use sockets inherently use the client-server paradigm.
One program creates a socket (the server) and another connects
to it (the client). The next couple of sections will look at both
server programs and client programs.
<H3><A NAME="TheServerSideofaConversation">
The Server Side of a Conversation</A></H3>
<P>
Server programs will use the <TT>socket()</TT>
fuNCtion to create a socket; <TT>bind()</TT>
to give the socket an address so that it can be found; <TT>listen()</TT>
to see if anyone wants to talk; and <TT>accept()</TT>
to start the conversation. Then <TT>send()</TT>
and <TT>recv()</TT> fuNCtions can
be used to hold the conversation. And finally, the socket is closed
with the <TT>close()</TT> fuNCtion.
<P>
The <TT>socket()</TT> call will look
something like this:
<BLOCKQUOTE>
<PRE>
$tcpProtocolNumber = getprotobyname('tcp') || 6;
socket(SOCKET, PF_INET(), SOCK_STREAM(), $tcpProtocolNumber)
or die("socket: $!");
</PRE>
</BLOCKQUOTE>
<P>
The first line gets the TCP protocol number using the <TT>getprotobyname()</TT>
fuNCtion. Some systems-such as Windows 95-do not implement this
fuNCtion, so a default value of 6 is provided. Then, the socket
is created with <TT>socket()</TT>.
The socket name is <TT>SOCKET</TT>.
Notice that it looks just like a file handle. When creating your
own sockets, the first parameter is the only thing that you should
change. The rest of the fuNCtion call will <I>always</I> use the
same last three parameters shown above. The actual meaning of
the three parameters is unimportant at this stage. If you are
curious, please refer to the UNIX documentation previously mentioned.
<P>
Socket names exist in their own namespace. Actually, there are
several pre-defined namespaces that you can use. The namespaces
are called <I>protocol families</I> because the namespace controls
how a socket connects to the world outside your process. For example,
the <TT>PF_INET</TT> namespace used
in the <TT>socket()</TT> fuNCtion
call above is used for the Internet.
<P>
ONCe the socket is created, you need to bind it to an address
with the <TT>bind()</TT> fuNCtion.
The <TT>bind()</TT> call might look
like this:
<BLOCKQUOTE>
<PRE>
$port = 20001;
$internetPackedAddress = pack('Sna4x8', AF_INET(), $port, "\0\0\0\0");
bind(SOCKET, $internetPackedAddress)
or die("bind: $!");
</PRE>
</BLOCKQUOTE>
<P>
All Internet sockets reside on a computer with symbolic names.
The server's name in conjuNCtion with a port number makes up a
socket's address. For example, <TT>www.water.com:20001</TT>.
Symbolic names also have a number equivalent known as the dotted
decimal address. For example, 145.56.23.1. Port numbers are a
way of determining which socket at <TT>www.water.com</TT>
you'd like to connect to. All port numbers below 1024 (or the
symbolic constant, <TT>IPPORT_RESERVED</TT>)
are reserved for special sockets. For example, port 37 is reserved
for a time service and 25 is reserved for the smtp service. The
value of 20,001 used in this example was picked at random. The
only limitations are: use a value above 1024 and no two sockets
on the same computer should have the same port number.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
You can always refer to your own computer using the dotted decimal address of <TT>127.0.0.1</TT> or the symbolic name <TT>localhost</TT>.
</BLOCKQUOTE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -