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

📄 ch28.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<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>, &quot;Using

Sockets.&quot; 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 &quot;For More Information

on How to Program in Java.&quot;

<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">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2<BR>

&nbsp;3 #<BR>

&nbsp;4 #&nbsp;&nbsp;Sample &quot;server&quot; script to create

data for Java Applet<BR>

&nbsp;5 #<BR>

&nbsp;6<BR>

&nbsp;7 #<BR>

&nbsp;8 # Flush immediately to disk.<BR>

&nbsp;9 #<BR>

10 $| = 1;<BR>

11 $i = 0;<BR>

12<BR>

13 while(1) {<BR>

14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;open (TEMPH,&quot;&gt;tst.txt&quot;)

|| die &quot;\n Cannot open \n&quot;;<BR>

15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$|=1;<BR>

16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$now = `date`;<BR>

17&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print TEMPH &quot;Data updated

\n $now \n&quot;;<BR>

18&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select TEMPH;<BR>

19&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$| = 1;<BR>

20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select STDOUT;<BR>

21&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;Data updated\n $now

\n&quot;;<BR>

22<BR>

23&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ------------------------------------------

<BR>

24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# You may want to increase this

time a bit<BR>

25&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# to allow the client(or clients)

to catch up.<BR>

26&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ------------------------------------------

<BR>

27&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(5);<BR>

28&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 #&nbsp;&nbsp;&nbsp;&nbsp;$i++;<BR>

34 #&nbsp;&nbsp;&nbsp;&nbsp;if ($i &gt; 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">&nbsp;&nbsp;1 //<BR>

&nbsp;&nbsp;2 // --------------------------------------------------------

<BR>

&nbsp;&nbsp;3 // Sample Java program to collect text from a remote

site<BR>

&nbsp;&nbsp;4 // --------------------------------------------------------

<BR>

&nbsp;&nbsp;5<BR>

&nbsp;&nbsp;6 //<BR>

&nbsp;&nbsp;7 // The following lines are required classes for

this program<BR>

&nbsp;&nbsp;8 //<BR>

&nbsp;&nbsp;9 import java.awt.*;<BR>

&nbsp;10 import java.io.*;<BR>

&nbsp;11 import java.net.URL;<BR>

&nbsp;12 import java.net.URLConnection;<BR>

&nbsp;13 import java.net.MalformedURLException;<BR>

&nbsp;14<BR>

&nbsp;15 //<BR>

&nbsp;16 // The Applet is declared here<BR>

&nbsp;17 //<BR>

&nbsp;18 public class l28 extends java.applet.Applet implements

Runnable {<BR>

&nbsp;19<BR>

&nbsp;20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int delta;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//

Interval between fetches<BR>

&nbsp;21&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URL theURL;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//

<BR>

&nbsp;22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread runner;<BR>

&nbsp;23&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TextArea ta = new TextArea(&quot;Synchronizing&quot;,20,20);

<BR>

&nbsp;24<BR>

&nbsp;25 public void init() {<BR>

&nbsp;26&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String lbl = getParameter(&quot;URL&quot;);

<BR>

&nbsp;27&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (lbl == null) { lbl =

&quot;http://ikra.com/tst.txt&quot;; }<BR>

&nbsp;28&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;29&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Collect after 60 seconds

(as default).<BR>

&nbsp;30&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;31&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delta = 60000;<BR>

&nbsp;32&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String dt = getParameter(&quot;DELTA&quot;);

<BR>

&nbsp;33&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (dt == null) { delta

= 60000; }<BR>

&nbsp;34&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else

{<BR>

&nbsp;35&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delta

= Integer.parseInt(dt);<BR>

&nbsp;36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

&nbsp;37&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;38&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Never let the user collect

data at less<BR>

&nbsp;39&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// than seven second intervals.

<BR>

&nbsp;40&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;41&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (dt &lt; 7000) { delta

= 7000; }<BR>

&nbsp;42<BR>

&nbsp;43&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;44&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Attempt to parse URL

and bail out if you<BR>

&nbsp;45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// do not have a valid URL

<BR>

&nbsp;46&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;47&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try { this.theURL = new

URL(lbl); }<BR>

&nbsp;48&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch ( MalformedURLException

e) {<BR>

&nbsp;49&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;Bad

URL:&quot; + lbl);<BR>

&nbsp;50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;51<BR>

&nbsp;52&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;53&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// This part needs work

to make the output in<BR>

&nbsp;54&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// the Java Applet look

pretty.<BR>

&nbsp;55&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;56&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(ta);<BR>

&nbsp;57<BR>

&nbsp;58 }&nbsp;// init function<BR>

&nbsp;59<BR>

&nbsp;60 //<BR>

&nbsp;61 // Your thread's starting function<BR>

&nbsp;62 //<BR>

&nbsp;63 public void start() {<BR>

&nbsp;64&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (runner == null) {<BR>

&nbsp;65&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runner

= new Thread(this);<BR>

&nbsp;66&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runner.start();

<BR>

&nbsp;67&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;68<BR>

&nbsp;69 } // start<BR>

&nbsp;70<BR>

&nbsp;71 //<BR>

&nbsp;72 // Your thread's stopping function<BR>

&nbsp;73 //<BR>

&nbsp;74 public void stop() {<BR>

&nbsp;75&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (runner != null) {<BR>

&nbsp;76&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runner.stop();

<BR>

&nbsp;77&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runner

= null;<BR>

&nbsp;78&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;79 } // stop<BR>

&nbsp;80<BR>

&nbsp;81 //<BR>

&nbsp;82 // While your thread is running this function is called.

<BR>

&nbsp;83 //<BR>

&nbsp;84 public void run() {<BR>

&nbsp;85&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream conn;<BR>

&nbsp;86&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataInputStream din;<BR>

&nbsp;87<BR>

&nbsp;88&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String line;<BR>

&nbsp;89<BR>

&nbsp;90&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (true) {<BR>

&nbsp;91&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;92&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Pick up the data from

the URL passed into applet&nbsp;<BR>

&nbsp;93&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;94&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>

&nbsp;95&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn

= theURL.openStream();<BR>

&nbsp;96&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;din

= new DataInputStream(new BufferedInputStream(conn));<BR>

&nbsp;97&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuffer

buf = new StringBuffer();<BR>

&nbsp;98<BR>

&nbsp;99&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while

((line = din.readLine()) != null) {<BR>

100&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

buf.append(line + &quot;\n&quot;);<BR>

101&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

102&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ta.setText(buf.toString());

<BR>

103&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

104&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (IOException e) {<BR>

105&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Whoops

no data at URL:&quot;); }<BR>

106<BR>

107&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

108&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// The screen has to be updated.

(Not really needed<BR>

109&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// but it's better to leave the

next line in here for<BR>

110&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// platforms running this applet.)

<BR>

111&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

112&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repaint();<BR>

113<BR>

114&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

115&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;Wait a bit before

trying html document again.<BR>

116&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

117&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>

118&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(delta);

<BR>

119&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

120&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (InterruptedException e)

⌨️ 快捷键说明

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