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

📄 ch28.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 3 页
字号:
79&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

80&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;close NEWSOCKET;<BR>

81<BR>

82 exit(0);<BR>

83<BR>

84 sub cleanup {<BR>

85 print &quot;\n Being killed &quot;;<BR>

86 close MY_SOCKET;<BR>

87 die &quot;$0: Cleanup : $!\n&quot;;<BR>

88 }<BR>

89</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Note that in line 24 in the server script shown in Listing 28.4,

all output is flushed immediately to the socket with the <TT><FONT FACE="Courier">$|=1;</FONT></TT>

command. The client Java applet to talk to the server is also

shown. Two classes are defined in this source file:

<UL>

<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">cl28</FONT></TT>

class for the applet itself

<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">StreamComm</FONT></TT>

class for listening for data on the socket to the Perl server

</UL>

<P>

After you compile the Java program in Listing 28.4, you'll get

two files in your directory:

<UL>

<LI><TT><FONT FACE="Courier">cl28.class</FONT></TT>

<LI><TT><FONT FACE="Courier">StreamComm.class</FONT></TT>

</UL>

<P>

The Java program takes two parameters: the port name and the host

name. No timeout value is given because the applet always reads

and displays immediately what it sees on the socket. The parameters

into the applet are handled in lines 26 and 42 in Listing 28.4.

<P>

The applet itself is included in an HTML file, as illustrated

by Listing 28.5.

<HR>

<BLOCKQUOTE>

<B>Listing 28.5. Using the socket applet.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1<BR>

&nbsp;2 &lt;HTML&gt;<BR>

&nbsp;3 &lt;BODY&gt;<BR>

&nbsp;4 &lt;TITLE&gt;  Test<BR>

&nbsp;5 &lt;/TITLE&gt;<BR>

&nbsp;6 &lt;applet code=cl28.class width=300 height=100&gt;<BR>

&nbsp;7 &lt;param name=HOST value=&quot;ikra.com&quot;&gt;<BR>

&nbsp;8 &lt;param name=PORT value=&quot;6783&quot;&gt;<BR>

&nbsp;9 &lt;/applet&gt;<BR>

10 &lt;/BODY&gt;<BR>

11 &lt;/HTML&gt;</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Lines 6 through 9 define the parameters into the new applet, called

<TT><FONT FACE="Courier">cl28.class</FONT></TT>. The required

<TT><FONT FACE="Courier">width</FONT></TT> and <TT><FONT FACE="Courier">height</FONT></TT>

parameters are also provided where the applet is declared.

<P>

The code for the applet itself is shown in Listing 28.6.

<HR>

<BLOCKQUOTE>

<B>Listing 28.6. Java applet using sockets.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;&nbsp;1 // --------------------------------------------------------

<BR>

&nbsp;&nbsp;2 //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Using

Sockets<BR>

&nbsp;&nbsp;3 // --------------------------------------------------------

<BR>

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

site<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.applet.*;<BR>

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

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

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

&nbsp;13<BR>

&nbsp;14<BR>

&nbsp;15 //<BR>

&nbsp;16 // The applet begins here.<BR>

&nbsp;17 //<BR>

&nbsp;18 public class cl28 extends Applet {<BR>

&nbsp;19<BR>

&nbsp;20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;21&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Declare the socket,port

tuple here<BR>

&nbsp;22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;23&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public int PORT;<BR>

&nbsp;24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Socket s;<BR>

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

&nbsp;26&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TextArea txtOut;<BR>

&nbsp;27<BR>

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

&nbsp;29&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// This class is derived

from the<BR>

&nbsp;30&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&quot;Java

in a Nutshell&quot; book<BR>

&nbsp;31&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;32&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StreamComm listener;<BR>

&nbsp;33<BR>

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

&nbsp;35<BR>

&nbsp;36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String host = getParameter(&quot;HOST&quot;);

<BR>

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

&nbsp;38&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// DO NOT FORGET TO chANGE

THE HOST NAME FROM<BR>

&nbsp;39&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ikra.com TO THE NODE

NAME YOUR SERVER IS ON!!<BR>

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

&nbsp;41&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (host == null) { host

= &quot;ikra.com&quot;; }<BR>

&nbsp;42&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String port = getParameter(&quot;PORT&quot;);

<BR>

&nbsp;43<BR>

&nbsp;44&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Set the default at 6970

<BR>

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

&nbsp;47&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (port == null) { PORT

= 6970; }<BR>

&nbsp;48&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {<BR>

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

&nbsp;50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PORT

= Integer.parseInt(port);<BR>

&nbsp;51&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

catch (NumberFormatException e) {<BR>

&nbsp;52&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PORT

= 6970;<BR>

&nbsp;53&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

&nbsp;54<BR>

&nbsp;55&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;56<BR>

&nbsp;57<BR>

&nbsp;58&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;59&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Use the socket &quot;s&quot;

to connect to server.<BR>

&nbsp;60&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

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

&nbsp;62&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s

= new Socket(host,PORT);<BR>

&nbsp;63&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;din

= new DataInputStream(s.getInputStream());<BR>

&nbsp;64<BR>

&nbsp;65&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;txtOut

= new TextArea(&quot;txtOut&quot;, 20, 25);<BR>

&nbsp;66&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.add(txtOut);

<BR>

&nbsp;67<BR>

&nbsp;68<BR>

&nbsp;69&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listener

= new StreamComm(din, txtOut);<BR>

&nbsp;70<BR>

&nbsp;71&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;showStatus(&quot;Connect:&quot;

+<BR>

&nbsp;72&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.getInetAddress().getHostName()

+<BR>

&nbsp;73&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;:&quot;

+ s.getPort());<BR>

&nbsp;74<BR>

&nbsp;75&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} // try<BR>

&nbsp;76&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (IOException e) {

<BR>

&nbsp;77&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;showStatus(e.toString());

<BR>

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

&nbsp;79 } // init<BR>

&nbsp;80<BR>

&nbsp;81&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

&nbsp;82&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// When your thread stops

<BR>

&nbsp;83&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<BR>

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

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

&nbsp;86&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.close();

<BR>

&nbsp;87&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

// try<BR>

&nbsp;88&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch

(IOException e) {<BR>

&nbsp;89&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;showStatus(e.toString());

<BR>

&nbsp;90&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

// catch<BR>

&nbsp;91&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;92<BR>

&nbsp;93 } // class cl28<BR>

&nbsp;94<BR>

&nbsp;95<BR>

&nbsp;96 //<BR>

&nbsp;97 // Class to put some wrapper around the Socket reading

<BR>

&nbsp;98 // code.<BR>

&nbsp;99 class StreamComm extends Thread {<BR>

100<BR>

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

102&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TextArea outp;<BR>

103&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuffer buf;<BR>

104<BR>

105&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public StreamComm(DataInputStream

in, TextArea output) {<BR>

106&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.din

= in;<BR>

107&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.outp

= output;<BR>

108&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.start();

<BR>

109&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf =

new StringBuffer();<BR>

110&nbsp;&nbsp;&nbsp;&nbsp; }<BR>

111<BR>

112<BR>

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

114<BR>

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

116<BR>

117&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(;;) {<BR>

118&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

<BR>

119&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outp.setText(&quot;Reading&quot;);

<BR>

120&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line

= din.readLine();<BR>

121&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if

(line == &quot;end&quot;) break;<BR>

122&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if

(line == null)<BR>

123&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

<BR>

124&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outp.setText(&quot;NULL&quot;);

<BR>

125&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;

<BR>

126&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

127&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf.append(line

+ &quot;\n&quot;);<BR>

128&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outp.setText(buf.toString());

<BR>

129<BR>

130<BR>

131&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} //

try reading<BR>

132&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch

(IOException e) {<BR>

133&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outp.setText(e.toString());

<BR>

134&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

135<BR>

136<BR>

137&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} // for loop<BR>

138&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} // run function<BR>

139<BR>

140 } // class StreamComm</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

In this Java applet, lines 105 to 109 in the <TT><FONT FACE="Courier">StreamComm</FONT></TT>

constructor copy the values of local variables in the applet.

Line 120 is where the <TT><FONT FACE="Courier">StreamComm</FONT></TT>

thread reads the incoming input and sets it in a string buffer.

The helper class <TT><FONT FACE="Courier">StreamComm</FONT></TT>

loops forever, until the applet is no longer referenced in its

containing HTML document.

<H2><A NAME="ForMoreInformationonHowtoProgrami"><B><FONT SIZE=5 COLOR=#FF0000>For

More Information on How to Program in Java</FONT></B></A></H2>

<P>

The online information on Java is perhaps the best source for

information on how to write Java applets. Here are the sites to

visit:

<UL>

<LI><TT><FONT FACE="Courier">www.javasoft.com</FONT></TT>

<LI><TT><FONT FACE="Courier">www.sun.com</FONT></TT>

</UL>

<P>

The online Application Programming Interface (API) on the sites

is usually the most current one to use. There are some texts already

on the market. I used the following books as references for my

work on this chapter and in Java programming:

<UL>

<LI><I>Teach Yourself Java in 21 Days</I>, Laura Lemay and Charles

Perkins, Sams Publishing, 1996.

<LI><I>Hooked on Java</I>, Aurthor Van Hoff, Sami Shaio, Orca

Starbuck, and Sun Microsystems Inc., Addison Wesley Publishing

Co., 1996.

<LI><I>Java in a Nutshell</I>, David Flanagan, O'Reilly and Associates,

1996.

<LI><I>Java!</I>, Tim Ritchey, New Riders Publishing, 1995.

</UL>

<H2><A NAME="Summary"><B><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></B></A>

</H2>

<P>

This chapter introduced you to interfacing your Perl scripts to

Java applets by using files or sockets. Each method has its own

merits. Using files gives you a semi-permanent record of what

was sent, and multiplexing is easy because multiple socket/child

process pairs do not have to be opened up to each client. The

downside is the use of the disk and slower processing overall.

Using sockets provides security, speed, and more efficient use

of resources. However, the con side of socket usage is the limitation

on the number of connections your server can use to transfer data

to clients. If you intend to transfer data to a large number of

clients, consider using files. If security is an issue, use sockets.

<P>

The methods discussed here are not the only ones available to

you. Using the Perl/Tk toolkit, you can draw comparable analogies

to display data via sockets as well, but you'll have to write

your own process and input-handling routines. (The Perl/Tk toolkit

is discussed in <A HREF="ch17.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch17.htm" >Chapter 17</A>, &quot;GUI

Interfaces with Perl/Tk.&quot;) If there is a browser that will

do most of the work for you, why not use it instead? You should

base your design decisions on using the best tools available.

<P>

<HR WIDTH="100%"></P>



<CENTER><P><A HREF="ch27.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch27.htm"><IMG SRC="pc.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/index.htm"><IMG SRC="hb.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch29.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch29.htm"><IMG 

SRC="nc.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>



<P>

<HR WIDTH="100%"></P>



</BODY>

</HTML>

⌨️ 快捷键说明

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