📄 ch18.htm
字号:
</TABLE>
</CENTER>
<P>
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 strict pragma.<BR>
Display a message if the </I><TT><I>red.planet.net</I></TT><I>
server is reachable.<BR>
Display a message if the </I><TT><I>saturn.planet.net</I></TT><I>
server is reachable.<BR>
Declare the </I><TT><I>echo()</I></TT><I>
fuNCtion.<BR>
Get the host and timeout parameters from the paramter array. If
no timeout parameter is specified, 5 seconds wil be used.<BR>
Declare some local variables.<BR>
Get the tcp protocol and echo port numbers.<BR>
Get the server's Internet address.<BR>
If </I><TT><I>$serverAddr</I></TT><I>
is undefined then the name of the server was probably iNCorrect
and an error message is displayed.<BR>
Check to see if the script is running under Windows 95.<BR>
If not under Windows 95, store the old alarm handler fuNCtion,
set the alarm handler to be an anonymous fuNCtion that simply
ends the script, and set an alarm to go off in </I><TT><I>$timeout</I></TT><I>
seconds.<BR>
Initialize the status variable to true.<BR>
Create a socket called </I><TT><I>ECHO</I></TT><I>.
<BR>
Initialize </I><TT><I>$packedFormat</I></TT><I>
with format specifiers.<BR>
Connect the socket to the remote server.<BR>
Close the socket.<BR>
Check to see if the script is running under Windows 95.<BR>
If not under Windows 95, reset the alarm and restore the old alarm
handler fuNCtion.<BR>
Return the status.</I>
</BLOCKQUOTE>
<HR>
<BLOCKQUOTE>
<B>Listing 18.4 18LST04.PL-Using the Echo Service<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
#!/usr/bin/perl -w
use Socket;
use strict;
print "red.planet.net is up.\n" if echo('red.planet.net');
print "saturn.planet.net is up.\n" if echo('saturn.planet.net');
sub echo {
my($host) = shift;
my($timeout) = shift || 5;
my($oldAlarmHandler, $status);
my($proto) = getprotobyname("tcp") || 6;
my($port) = getservbyname("echo", "tcp") || 7;
my($serverAddr) = (gethostbyname($host))[4];
return(print("echo: $host could not be found, sorry.\n"), 0)
if ! defined($serverAddr);
if (0 == Win32::IsWin95) {
$oldAlarmHandler = $SIG{'ALRM'};
$SIG{'ALRM'} = sub { die(); };
alarm($timeout);
}
$status = 1; # assume the connection will work.
socket(ECHO, AF_INET(), SOCK_STREAM(), $proto)
or die("socket: $!");
$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 the
connection 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 the
middle of the <TT>echo()</TT> fuNCtion
does. The return statement is repeated here:
<BLOCKQUOTE>
<PRE>
return(print("echo: $host could not be found, sorry.\n"), 0)
if ! defined($serverAddr);
</PRE>
</BLOCKQUOTE>
<P>
The statement uses the comma operator to execute two statements
where normally you would see one. The last statement to be evaluated
is the value for the series of statements. In this case, a zero
value is returned. I'm not recommending this style of coding,
but I thought you should see it a least oNCe. Now, if you see
this technique in another programmer's scripts you'll understand
it better. The return statement could also be done written like
this:
<BLOCKQUOTE>
<PRE>
if (! defined($serverAddr) {
print("echo: $host could not be found, sorry.\n")
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 transfer
files. There are thousands of fcservers from which you can download
files. For the latest graphic board drivers to the best in shareware
to the entire set of UNIX sources, ftp is the answer.
<P>
The program in Listing 18.5 downloads the Perl FAQ in compressed
format 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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 protocol
commands 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 to
the 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 compressed
files 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 then
print the list.<BR>
Use </I><TT><I>dir()</I></TT><I> to
find out which files are in the current directory andthen print
the 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 then
print the list.</I>
</BLOCKQUOTE>
<HR>
<BLOCKQUOTE>
<B>Listing 18.5 18LST05.PL-Using the ftplib Library
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
#!/usr/bin/perl -w
require('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("list of /pub/perl/faq\n");
foreach (@dirList) {
print("\t$_\n");
}
@dirList = ftp::dir();
print("list of /pub/perl/faq\n");
foreach (@dirList) {
print("\t$_\n");
}
ftp::debug();
ftp::cwd('/pub/perl/faq');
@dirList = ftp::list();
print("list of /pub/perl/faq\n");
foreach (@dirList) {
print("\t$_\n");
}
</PRE>
</BLOCKQUOTE>
<HR>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
<< 220 flood FTP server (Version wu-2.4(21) Tue Apr 9 17:01:12 EDT 1996)
ready.
>> user anonymous
<< 331 Guest login ok, send your complete e-mail address as password.
>> pass .....
<< 230- Welcome to the
<< 230- University of Florida
.
.
.
<< 230 Guest login ok, access restrictions apply.
>> port 207,3,100,103,4,135
<< 200 PORT command successful.
>> nlst pub/perl/faq
<< 150 Opening ASCII mode data connection for file list.
<< 226 Transfer complete.
>> cwd /pub/perl/faq
<< 250 CWD command successful.
>> type i
<< 200 Type set to I.
>> port 207,3,100,103,4,136
<< 200 PORT command successful.
>> retr FAQ.gz
<< 150 Opening BINARY mode data connection for FAQ.gz (75167 bytes).
<< 226 Transfer complete.
>> port 207,3,100,103,4,138
<< 200 PORT command successful.
>> nlst
<< 150 Opening BINARY mode data connection for file list.
<< 226 Transfer complete.
list of /pub/perl/faq
FAQ
FAQ.gz
>> port 207,3,100,103,4,
139
<< 200 PORT command successful.
>> list
<< 150 Opening BINARY mode data connection for /bin/ls.
<< 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.gz
list of /pub/perl/faq
FAQ
FAQ.gz
</PRE>
</BLOCKQUOTE>
<P>
I'm sure that you can pick out the different ftp commands and
responses in this output. Notice that the ftp commands and responses
are 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 Usenet
newsgroups. Most newsgroups are question and answer forums. You
post a message-perhaps asking a question. And, usually, you get
a quick response. In addition, a small number of newsgroups are
used to distribute information. <A HREF="ch22.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch22.htm" >Chapter 22</A>, "Internet Resources,"
describes some specific newsgroups that you might want to read.
<P>
Like most services, NNTP uses a client/server model. You connect
to a news server and request information using NNTP. The protocol
consists of a series of commands a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -