📄 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 becauseeveryone can understand the <I>protocols</I> that are spoken onthe net. A protocol is a set of commands and responses. Thereare two layers of protocols that I'll mention here. The low-levellayer 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 thischapter. They use TCP/IP as a facilitator to communicate betweencomputers. The protocols all have the same basic pattern:<UL><LI>Begin a Conversation-Your computer (the client) starts a conversationwith another computer (the server).<LI>Hold a Conversation-During the conversation, commands aresent 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 systemuses the mail protocol to hold a conversation with the receivingsystem.<P><A HREF="f18-1.gif"><B>Figure 18.1 : </B><I>All protocols follow this Communicationsmodel</I>.</A><P>Internet conversations are done with sockets, in a manner similarto 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 abouta small subset of the socket fuNCtionality in order to use thehigh-level protocols.<P>Table 18.1 provides a list of the high-level protocols that youcan use. This chapter will not be able to cover them all, butif you'd like to investigate further, the protocols are detailedin 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 serveror ftp server. Underlying all of the high-level protocols is thevery popular Transfer Control Protocol/Internet Protocol or TCP/IP.You don't need to know about TCP/IP in order to use the high-levelprotocols. All you need to know is that TCP/IP enables a serverto <I>listen</I> and respond to an iNComing conversation. INComingconversations arrive at something called a port. A <I>Port</I>is an imaginary place where iNComing packets of information canarrive (just like a ship arrives at a sea port). Each type ofservice (for example, mail or file transfer) has its own portnumber.<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 turnour attention to examples that use them. You see how to send andreceive mail. Sending mail is done using the Simple Mail TransferProtocol (SMTP), which is detailed in an RFC numbered 821. Receivingmail is done using the Post Office Protocol (POP) as detailedin 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 subsetis all you need to get started. This section will focus in onthose aspects of sockets that are useful in Perl. There will bewhole areas of sockets that I won't mention.<P>Table 18.2 lists all of the Perl fuNCtions that relate to socketsso you have a handy refereNCe. But remember, you probably won'tneed 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 connectsto it (the client). The next couple of sections will look at bothserver 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 canbe used to hold the conversation. And finally, the socket is closedwith the <TT>close()</TT> fuNCtion.<P>The <TT>socket()</TT> call will looksomething 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 thisfuNCtion, so a default value of 6 is provided. Then, the socketis created with <TT>socket()</TT>.The socket name is <TT>SOCKET</TT>.Notice that it looks just like a file handle. When creating yourown sockets, the first parameter is the only thing that you shouldchange. The rest of the fuNCtion call will <I>always</I> use thesame last three parameters shown above. The actual meaning ofthe three parameters is unimportant at this stage. If you arecurious, please refer to the UNIX documentation previously mentioned.<P>Socket names exist in their own namespace. Actually, there areseveral pre-defined namespaces that you can use. The namespacesare called <I>protocol families</I> because the namespace controlshow a socket connects to the world outside your process. For example,the <TT>PF_INET</TT> namespace usedin the <TT>socket()</TT> fuNCtioncall above is used for the Internet.<P>ONCe the socket is created, you need to bind it to an addresswith the <TT>bind()</TT> fuNCtion.The <TT>bind()</TT> call might looklike 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 asocket's address. For example, <TT>www.water.com:20001</TT>.Symbolic names also have a number equivalent known as the dotteddecimal address. For example, 145.56.23.1. Port numbers are away of determining which socket at <TT>www.water.com</TT>you'd like to connect to. All port numbers below 1024 (or thesymbolic constant, <TT>IPPORT_RESERVED</TT>)are reserved for special sockets. For example, port 37 is reservedfor a time service and 25 is reserved for the smtp service. Thevalue of 20,001 used in this example was picked at random. Theonly limitations are: use a value above 1024 and no two socketson 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></TD></TR></TABLE></CENTER><P><P>The second line of this short example creates a full Internetsocket address using the <TT>pack()</TT>fuNCtion. This is another complicated topic that I will sidestep.As long as you know the port number and the server's address,you can simply plug those values into the example code and notworry about the rest. The important part of the example is the"\0\0\0\0" string. This string holds the four numbersthat make up the dotted decimal Internet address. If you alreadyknow the dotted decimal address, convert each number to octaland replace the appropriate \0 in the string.<P>If you know the symbolic name of the server instead of the dotted
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -