⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch18.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 5 页
字号:
        alarm($timeout);    }    $status = 1;    # assume the connection will work.    socket(ECHO, AF_INET(), SOCK_STREAM(), $proto)        or die(&quot;socket: $!&quot;);    $packFormat = 'S n a4 x8';   # Windows 95, SunOs 4.1+    #$packFormat = 'S n c4 x8';   # SunOs 5.4+ (Solaris 2)    connect(ECHO, pack($packFormat, AF_INET(), $port, $serverAddr))        or $status = 0;    close(ECHO);    if (0 == Win32::IsWin95) {        alarm(0);        $SIG{'ALRM'} = $oldAlarmHandler;    }    return($status);}</PRE></BLOCKQUOTE><HR><P>This program will display:<BLOCKQUOTE><PRE>echo: red.planet.net could not be found, sorry.saturn.planet.net is up.</PRE></BLOCKQUOTE><P>When dealing with the echo service, you only need to make theconnection in order to determine that the server is up and running.As soon as the connection is made, you can close the socket.<P>Most of the program should be pretty familiar to you by now. However,you might not immediately realize what return statement in themiddle of the <TT>echo()</TT> fuNCtiondoes. The return statement is repeated here:<BLOCKQUOTE><PRE>return(print(&quot;echo: $host could not be found, sorry.\n&quot;), 0)        if ! defined($serverAddr);</PRE></BLOCKQUOTE><P>The statement uses the comma operator to execute two statementswhere normally you would see one. The last statement to be evaluatedis the value for the series of statements. In this case, a zerovalue is returned. I'm not recommending this style of coding,but I thought you should see it a least oNCe. Now, if you seethis technique in another programmer's scripts you'll understandit better. The return statement could also be done written likethis:<BLOCKQUOTE><PRE>if (! defined($serverAddr) {    print(&quot;echo: $host could not be found, sorry.\n&quot;)    return(0);}</PRE></BLOCKQUOTE><H2><A NAME="TransferringFilesFTP"><FONT SIZE=5 COLOR=#FF0000>Transferring Files (FTP)</FONT></A></H2><P>One of the backbones of the Internet is the ability to transferfiles. There are thousands of fcservers from which you can downloadfiles. For the latest graphic board drivers to the best in sharewareto the entire set of UNIX sources, ftp is the answer.<P>The program in Listing 18.5 downloads the Perl FAQ in compressedformat from ftp.cis.ufl.edu and displays a directory in two formats.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></TD></TR><TR><TD><BLOCKQUOTE>The ftplib.pl file can be found on the CD-ROM that accompanies this book. Please put it into your Perl library directory. I have modified the standard ftplib.pl that is available from the Internet to allow the library to work under Windows 95 and Windows NT.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Turn on the warning compiler option.<BR>Load the </I><TT><I>ftplib</I></TT><I>library.<BR>Turn on the strict pragma.<BR>Declare a variable to hold directory listings.<BR>Turn debugging mode on. This will display all of the protocolcommands and responses on </I><TT><I>STDERR</I></TT><I>.<BR>Connect to the ftp server providing a </I><TT><I>userid</I></TT><I>of anonymous and your email address as the password.<BR>Use the </I><TT><I>list()</I></TT><I>fuNCtion to get a directory listing without first changing tothe directory.<BR>Change to the </I><TT><I>/pub/perl/faq</I></TT><I>directory.<BR>Start binary mode. This is very important when getting compressedfiles or executables.<BR>Get the Perl FAQ file.<BR>Use </I><TT><I>list()</I></TT><I>to find out which files are in the current directory and thenprint the list.<BR>Use </I><TT><I>dir()</I></TT><I> tofind out which files are in the current directory andthen printthe list.<BR>Turn debugging off.<BR>Change to the </I><TT><I>/pub/perl/faq</I></TT><I>directory.<BR>Use </I><TT><I>list()</I></TT><I>to find out which files are in the current directory and thenprint the list.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 18.5&nbsp;&nbsp;18LST05.PL-Using the ftplib Library<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>#!/usr/bin/perl -wrequire('ftplib.pl');use strict;my(@dirList);ftp::debug('ON');ftp::open('ftp.cis.ufl.edu', 'anonymous', 'medined@planet.net') or die($!);@dirList = ftp::list('pub/perl/faq');ftp::cwd('/pub/perl/faq');ftp::binary();ftp::gets('FAQ.gz');@dirList = ftp::list();print(&quot;list of /pub/perl/faq\n&quot;);foreach (@dirList) {    print(&quot;\t$_\n&quot;);}@dirList = ftp::dir();print(&quot;list of /pub/perl/faq\n&quot;);foreach (@dirList) {    print(&quot;\t$_\n&quot;);}ftp::debug();ftp::cwd('/pub/perl/faq');@dirList = ftp::list();print(&quot;list of /pub/perl/faq\n&quot;);foreach (@dirList) {    print(&quot;\t$_\n&quot;);}</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>&lt;&lt; 220 flood FTP server (Version wu-2.4(21) Tue Apr 9 17:01:12 EDT 1996) ready.&gt;&gt; user anonymous&lt;&lt; 331 Guest login ok, send your complete e-mail address as password.&gt;&gt; pass .....&lt;&lt; 230-                     Welcome to the&lt;&lt; 230-                  University of Florida...&lt;&lt; 230 Guest login ok, access restrictions apply.&gt;&gt; port 207,3,100,103,4,135&lt;&lt; 200 PORT command successful.&gt;&gt; nlst pub/perl/faq&lt;&lt; 150 Opening ASCII mode data connection for file list.&lt;&lt; 226 Transfer complete.&gt;&gt; cwd /pub/perl/faq&lt;&lt; 250 CWD command successful.&gt;&gt; type i&lt;&lt; 200 Type set to I.&gt;&gt; port 207,3,100,103,4,136&lt;&lt; 200 PORT command successful.&gt;&gt; retr FAQ.gz&lt;&lt; 150 Opening BINARY mode data connection for FAQ.gz (75167 bytes).&lt;&lt; 226 Transfer complete.&gt;&gt; port 207,3,100,103,4,138&lt;&lt; 200 PORT command successful.&gt;&gt; nlst&lt;&lt; 150 Opening BINARY mode data connection for file list.&lt;&lt; 226 Transfer complete.list of /pub/perl/faq     FAQ     FAQ.gz&gt;&gt; port 207,3,100,103,4,139&lt;&lt; 200 PORT command successful.&gt;&gt; list&lt;&lt; 150 Opening BINARY mode data connection for /bin/ls.&lt;&lt; 226 Transfer complete.list of /pub/perl/faq     total 568     drwxrwxr-x   2 1208     31           512 Nov  7  1995 .     drwxrwxr-x  10 1208     68           512 Jun 18 21:32 ..     -rw-rw-r--   1 1208     31        197446 Nov  4  1995 FAQ     -rw-r--r--   1 1208     31         75167 Nov  7  1995 FAQ.gzlist of /pub/perl/faq     FAQ     FAQ.gz</PRE></BLOCKQUOTE><P>I'm sure that you can pick out the different ftp commands andresponses in this output. Notice that the ftp commands and responsesare only displayed when the debugging feature is turned on.<H2><A NAME="ReadingtheNewsNNTP"><FONT SIZE=5 COLOR=#FF0000>Reading the News (NNTP)</FONT></A></H2><P>One of the most valuable services offered on the net is Usenetnewsgroups. Most newsgroups are question and answer forums. Youpost a message-perhaps asking a question. And, usually, you geta quick response. In addition, a small number of newsgroups areused to distribute information. <A HREF="ch22.htm" >Chapter 22</A>, &quot;Internet Resources,&quot;describes some specific newsgroups that you might want to read.<P>Like most services, NNTP uses a client/server model. You connectto a news server and request information using NNTP. The protocolconsists of a series of commands and replies. I think NNTP isa bit more complicated than the other because the variety of thingsyou might want to do with news articles is larger.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></FONT></B></TD></TR><TR><TD><BLOCKQUOTE>Some of the NNTP commands will result in very large responses. For example, the <TT>LIST</TT> command will retrieve the name of every newsgroup that your server knows about. Because there are over 10,000 newsgroups it might take a lot of time for the response to be received.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>I suggest using Perl to filter newsgroups or to retrieve all thearticles available and create reports or extracts. Don't use Perlfor a full-blown news client. Use Java, Visual Basic, or anotherlanguage that is designed with user interfaces in mind. In addition,there are plenty of great free or inexpensive news clients available,why reinvent the wheel?<P>Listing 18.6 contains an object-oriented program that eNCapsulatesa small number of NNTP commands so that you can experiment withthe protocol. Only the simplest of the commands have been implementedto keep the example small and uNCluttered.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Turn on the warning compiler option.<BR>Load the </I><TT><I>Socket</I></TT><I>module.<BR>Turn on the </I><TT><I>strict</I></TT><I>pragma.<BR>Begin the News package. This also started the definition of theNews class.<BR>Define the </I><TT><I>new()</I></TT><I>fuNCtion2-the constructor for the News class.<BR>Get the class name from the parameter array.<BR>Get the name of the news server from the parameter array.<BR>Declare a hash with two entries-the class properties.<BR>Bless the hash.<BR>Call the </I><TT><I>initialize()</I></TT><I>fuNCtion that connects to the server.<BR>Define a signal handler to gracefully handle Ctrl+C and Ctrl+Break.<BR>Return a refereNCe to the hash-the class object.<BR>Define the </I><TT><I>initialize()</I></TT><I>fuNCtion-connects to the news server.<BR>Get the class name from the parameter array.<BR>Get the protocol number, port number, and server address.<BR>Create a socket.<BR>Initialize the format for the </I><TT><I>pack()</I></TT><I>fuNCtion.<BR>Connect to the news server.<BR>Modify the socket to use non-buffered I/O.<BR>Call the </I><TT><I>getInitialResponse()</I></TT><I>fuNCtion.<BR>Define </I><TT><I>getInitialResponse()</I></TT><I>-receiveresponse from connection.<BR>Get the class name from the parameter array.<BR>Initialize a buffer to hold the reponse.<BR>Get the reponse from the server.<BR>Print the response if debugging is turned on.<BR>Define </I><TT><I>closeSocket()</I></TT><I>-signalhandler.<BR>Close the socket.<BR>End the script.<BR>Define </I><TT><I>DESTROY()</I></TT><I>-thedeconstructor for the class.<BR>Close the socket.<BR>Define </I><TT><I>debug()</I></TT><I>-turnsdebugging on or off.<BR>Get the class name from the parameter array.<BR>Get the state (on or off) from the parameter array.<BR>Turn debugging on if the state is on or 1.<BR>Turn debugging off if the state is off or 0.<BR>Define </I><TT><I>send()</I></TT><I>-senda NNTP command and get a response.<BR>Get the class name from the parameter array.<BR>Get the command from the parameter array.<BR>Print the command if debugging is turned on.<BR>Send the command to the news server.<BR>Get a reply from the news server.<BR>Print the reply if debugging is turned on.<BR>Return the reply to the calling routine.<BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -