📄 ch28.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 28 -- Client/Server Using Java Applets
and Perl</TITLE>
<META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 28</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Client/Server Using Java Applets
and Perl</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#UsingCGIorSockets" >Using CGI or Sockets</A>
<LI><A HREF="#UsingtheDisk" >Using the Disk</A>
<UL>
<LI><A HREF="#AQuickNoteAboutJava" >A Quick Note About Java</A>
</UL>
<LI><A HREF="#UsingCGIandSockets" >Using CGI and Sockets</A>
<LI><A HREF="#ForMoreInformationonHowtoProgrami" >For More Information on How to Program in Java</A>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
This chapter provides the basic information for getting a client/server
application using Perl 5 and Java applets. After reading this
chapter, you'll understand how to get data from a Perl server
to a Java client.
<P>
The information in this chapter is based on the assumption that
you have read <A HREF="ch12.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch12.htm" >Chapter 12</A>, "Using
Sockets." In this chapter you'll learn how to connect a Java
applet to your socket. This chapter is <I>not</I> meant to be
a tutorial on Java. For programming in Java, you'll have to go
to the sources listed in the section titled "For More Information
on How to Program in Java."
<H2><A NAME="UsingCGIorSockets"><B><FONT SIZE=5 COLOR=#FF0000>Using
CGI or Sockets</FONT></B></A></H2>
<P>
The first way to hook client applets up to your Perl CGI server
is via a URL page. The basic scenario is that a Perl CGI script
creates an HTML document for your client, based on responses to
a <TT><FONT FACE="Courier">FORM</FONT></TT> or simply via a URL.
The output of your CGI script can even exist on disk in the form
of a file.
<P>
Using the disk to store the data as an intermediate place for
data to reside before it's retrieved by the client may seem like
a crude approach. (Compare this to using a sledgehammer to put
a nail in a wall.) Indeed, using the disk for a file transfer
is a very crude approach and not quite as elegant as using a socket
as the medium from the server to the client. For one thing, you
are writing to disk, which in itself is an inherently slow process
when compared with actions that are performed in memory alone.
Second, you are opening the door to potential file-locking problems.
That is, while your server is writing to disk, a client might
be trying to read the same file.
<P>
Even so, the problem with sockets is the overhead of copies of
a server daemon to handle each client's request. If the data per
request is small enough, writing it to disk won't hurt your computer's
performance. Most Net users at 28.8Kbps won't notice the extra
milliseconds spent on spinning the disk.
<P>
Writing logs to disk has the advantage of keeping a record of
what was sent back. Nightly or even hourly backups of the data
file can help ensure the possibility of reconstructing any previously
sent messages. With data being piped via a socket, you have to
write extra code to save data off to disk.
<P>
Figure 28.1 presents two ways of implementing client/server applications
with Perl. In the first scenario, you are writing intermediate
results to disk while satisfying requests for clients. The client
does not necessarily have to be a Java applet in the first scenario
because the Perl server could be writing HTML pages to disk for
another browser to pick up. In the second case, the results from
a server calculation are sent back directly with the use of a
socket. In this second case, a Java applet would reduce the coding
effort of communicating via sockets because of the built-in socket
support in Java. You are at liberty to write your own client application
to do the client-side socket processing.
<P>
<A HREF="f28-1.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f28-1.gif" ><B>Figure 28.1 : </B><I>Two ways of communicating between client and server.</I></A>
<P>
Ultimately, it's up to you to decide which approach to take when
designing an interface. If your server and client are guaranteed
to be able to use sockets, by all means code your applet in this
manner. If you are in doubt and are willing to use the disk as
an intermediary, use URLs.
<H2><A NAME="UsingtheDisk"><B><FONT SIZE=5 COLOR=#FF0000>Using
the Disk</FONT></B></A></H2>
<P>
This section focuses on writing a Java applet to collect data
from a file given a URL on a remote machine. The Java applet runs
on a client using a browser. The server in this example is a Perl
script that is used to create a data file. Listing 28.1 presents
the server-side code.
<P>
For using the disk as the intermediary, as in the example presented
here, the Java applet will be configured to read the file. It's
your Perl script's responsibility to ensure that the file is updated
regularly. For example, your Perl script might wake up every five
minutes, collect the temperature from a sensor, and then write
the HTML file.
<P>
The client using your applet will be querying the file that your
script writes. The Perl script to write the test data to a known
file is a format that is well known to other Web browsers. The
Java applet that implements the client side is shown in Listing
28.2.
<HR>
<BLOCKQUOTE>
<B>Listing 28.1. The Perl script to handle server-side details.
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2<BR>
3 #<BR>
4 # Sample "server" script to create
data for Java Applet<BR>
5 #<BR>
6<BR>
7 #<BR>
8 # Flush immediately to disk.<BR>
9 #<BR>
10 $| = 1;<BR>
11 $i = 0;<BR>
12<BR>
13 while(1) {<BR>
14 open (TEMPH,">tst.txt")
|| die "\n Cannot open \n";<BR>
15 $|=1;<BR>
16 $now = `date`;<BR>
17 print TEMPH "Data updated
\n $now \n";<BR>
18 select TEMPH;<BR>
19 $| = 1;<BR>
20 select STDOUT;<BR>
21 print "Data updated\n $now
\n";<BR>
22<BR>
23 # ------------------------------------------
<BR>
24 # You may want to increase this
time a bit<BR>
25 # to allow the client(or clients)
to catch up.<BR>
26 # ------------------------------------------
<BR>
27 sleep(5);<BR>
28 close TEMPH;<BR>
29 #<BR>
30 # If you really want to put a limit to this looping<BR>
31 # then uncomment the following lines.<BR>
32 #<BR>
33 # $i++;<BR>
34 # if ($i > 25) { exit(0); }<BR>
35<BR>
36 } # loop forever.</FONT></TT>
</BLOCKQUOTE>
<HR>
<HR>
<BLOCKQUOTE>
<B>Listing 28.2. Using URLs to display the data.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 //<BR>
2 // --------------------------------------------------------
<BR>
3 // Sample Java program to collect text from a remote
site<BR>
4 // --------------------------------------------------------
<BR>
5<BR>
6 //<BR>
7 // The following lines are required classes for
this program<BR>
8 //<BR>
9 import java.awt.*;<BR>
10 import java.io.*;<BR>
11 import java.net.URL;<BR>
12 import java.net.URLConnection;<BR>
13 import java.net.MalformedURLException;<BR>
14<BR>
15 //<BR>
16 // The Applet is declared here<BR>
17 //<BR>
18 public class l28 extends java.applet.Applet implements
Runnable {<BR>
19<BR>
20 int delta; //
Interval between fetches<BR>
21 URL theURL; //
<BR>
22 Thread runner;<BR>
23 TextArea ta = new TextArea("Synchronizing",20,20);
<BR>
24<BR>
25 public void init() {<BR>
26 String lbl = getParameter("URL");
<BR>
27 if (lbl == null) { lbl =
"http://ikra.com/tst.txt"; }<BR>
28 //<BR>
29 // Collect after 60 seconds
(as default).<BR>
30 //<BR>
31 delta = 60000;<BR>
32 String dt = getParameter("DELTA");
<BR>
33 if (dt == null) { delta
= 60000; }<BR>
34 else
{<BR>
35 delta
= Integer.parseInt(dt);<BR>
36 }
<BR>
37 //<BR>
38 // Never let the user collect
data at less<BR>
39 // than seven second intervals.
<BR>
40 //<BR>
41 if (dt < 7000) { delta
= 7000; }<BR>
42<BR>
43 //<BR>
44 // Attempt to parse URL
and bail out if you<BR>
45 // do not have a valid URL
<BR>
46 //<BR>
47 try { this.theURL = new
URL(lbl); }<BR>
48 catch ( MalformedURLException
e) {<BR>
49 System.out.println("Bad
URL:" + lbl);<BR>
50 }<BR>
51<BR>
52 //<BR>
53 // This part needs work
to make the output in<BR>
54 // the Java Applet look
pretty.<BR>
55 //<BR>
56 add(ta);<BR>
57<BR>
58 } // init function<BR>
59<BR>
60 //<BR>
61 // Your thread's starting function<BR>
62 //<BR>
63 public void start() {<BR>
64 if (runner == null) {<BR>
65 runner
= new Thread(this);<BR>
66 runner.start();
<BR>
67 }<BR>
68<BR>
69 } // start<BR>
70<BR>
71 //<BR>
72 // Your thread's stopping function<BR>
73 //<BR>
74 public void stop() {<BR>
75 if (runner != null) {<BR>
76 runner.stop();
<BR>
77 runner
= null;<BR>
78 }<BR>
79 } // stop<BR>
80<BR>
81 //<BR>
82 // While your thread is running this function is called.
<BR>
83 //<BR>
84 public void run() {<BR>
85 InputStream conn;<BR>
86 DataInputStream din;<BR>
87<BR>
88 String line;<BR>
89<BR>
90 while (true) {<BR>
91 //<BR>
92 // Pick up the data from
the URL passed into applet <BR>
93 //<BR>
94 try {<BR>
95 conn
= theURL.openStream();<BR>
96 din
= new DataInputStream(new BufferedInputStream(conn));<BR>
97 StringBuffer
buf = new StringBuffer();<BR>
98<BR>
99 while
((line = din.readLine()) != null) {<BR>
100
buf.append(line + "\n");<BR>
101 }<BR>
102 ta.setText(buf.toString());
<BR>
103 }<BR>
104 catch (IOException e) {<BR>
105 System.out.println("Whoops
no data at URL:"); }<BR>
106<BR>
107 //<BR>
108 // The screen has to be updated.
(Not really needed<BR>
109 // but it's better to leave the
next line in here for<BR>
110 // platforms running this applet.)
<BR>
111 //<BR>
112 repaint();<BR>
113<BR>
114 //<BR>
115 // Wait a bit before
trying html document again.<BR>
116 //<BR>
117 try {<BR>
118 Thread.sleep(delta);
<BR>
119 }<BR>
120 catch (InterruptedException e)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -