📄 ch28.htm
字号:
79 }<BR>
80 close NEWSOCKET;<BR>
81<BR>
82 exit(0);<BR>
83<BR>
84 sub cleanup {<BR>
85 print "\n Being killed ";<BR>
86 close MY_SOCKET;<BR>
87 die "$0: Cleanup : $!\n";<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"> 1<BR>
2 <HTML><BR>
3 <BODY><BR>
4 <TITLE> Test<BR>
5 </TITLE><BR>
6 <applet code=cl28.class width=300 height=100><BR>
7 <param name=HOST value="ikra.com"><BR>
8 <param name=PORT value="6783"><BR>
9 </applet><BR>
10 </BODY><BR>
11 </HTML></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"> 1 // --------------------------------------------------------
<BR>
2 // Using
Sockets<BR>
3 // --------------------------------------------------------
<BR>
4 // Sample Java program to collect text from a remote
site<BR>
5 // --------------------------------------------------------
<BR>
6 //<BR>
7 // The following lines are required classes for
this program<BR>
8<BR>
9 import java.applet.*;<BR>
10 import java.awt.*;<BR>
11 import java.io.*;<BR>
12 import java.net.*;<BR>
13<BR>
14<BR>
15 //<BR>
16 // The applet begins here.<BR>
17 //<BR>
18 public class cl28 extends Applet {<BR>
19<BR>
20 //<BR>
21 // Declare the socket,port
tuple here<BR>
22 //<BR>
23 public int PORT;<BR>
24 Socket s;<BR>
25 DataInputStream din;<BR>
26 TextArea txtOut;<BR>
27<BR>
28 //<BR>
29 // This class is derived
from the<BR>
30 // "Java
in a Nutshell" book<BR>
31 //<BR>
32 StreamComm listener;<BR>
33<BR>
34 public void init() {<BR>
35<BR>
36 String host = getParameter("HOST");
<BR>
37 //<BR>
38 // DO NOT FORGET TO chANGE
THE HOST NAME FROM<BR>
39 // ikra.com TO THE NODE
NAME YOUR SERVER IS ON!!<BR>
40 //<BR>
41 if (host == null) { host
= "ikra.com"; }<BR>
42 String port = getParameter("PORT");
<BR>
43<BR>
44 //<BR>
45 // Set the default at 6970
<BR>
46 //<BR>
47 if (port == null) { PORT
= 6970; }<BR>
48 else {<BR>
49 try {<BR>
50 PORT
= Integer.parseInt(port);<BR>
51 }
catch (NumberFormatException e) {<BR>
52 PORT
= 6970;<BR>
53 }
<BR>
54<BR>
55 }<BR>
56<BR>
57<BR>
58 //<BR>
59 // Use the socket "s"
to connect to server.<BR>
60 //<BR>
61 try {<BR>
62 s
= new Socket(host,PORT);<BR>
63 din
= new DataInputStream(s.getInputStream());<BR>
64<BR>
65 txtOut
= new TextArea("txtOut", 20, 25);<BR>
66 this.add(txtOut);
<BR>
67<BR>
68<BR>
69 listener
= new StreamComm(din, txtOut);<BR>
70<BR>
71 showStatus("Connect:"
+<BR>
72 s.getInetAddress().getHostName()
+<BR>
73 ":"
+ s.getPort());<BR>
74<BR>
75 } // try<BR>
76 catch (IOException e) {
<BR>
77 showStatus(e.toString());
<BR>
78 } // catch<BR>
79 } // init<BR>
80<BR>
81 //<BR>
82 // When your thread stops
<BR>
83 //<BR>
84 public void stop() {<BR>
85 try {<BR>
86 s.close();
<BR>
87 }
// try<BR>
88 catch
(IOException e) {<BR>
89 showStatus(e.toString());
<BR>
90 }
// catch<BR>
91 }<BR>
92<BR>
93 } // class cl28<BR>
94<BR>
95<BR>
96 //<BR>
97 // Class to put some wrapper around the Socket reading
<BR>
98 // code.<BR>
99 class StreamComm extends Thread {<BR>
100<BR>
101 DataInputStream din;<BR>
102 TextArea outp;<BR>
103 StringBuffer buf;<BR>
104<BR>
105 public StreamComm(DataInputStream
in, TextArea output) {<BR>
106 this.din
= in;<BR>
107 this.outp
= output;<BR>
108 this.start();
<BR>
109 buf =
new StringBuffer();<BR>
110 }<BR>
111<BR>
112<BR>
113 public void run() {<BR>
114<BR>
115 String line;<BR>
116<BR>
117 for(;;) {<BR>
118 try {
<BR>
119 outp.setText("Reading");
<BR>
120 line
= din.readLine();<BR>
121 if
(line == "end") break;<BR>
122 if
(line == null)<BR>
123 {
<BR>
124 outp.setText("NULL");
<BR>
125 break;
<BR>
126 }
<BR>
127 buf.append(line
+ "\n");<BR>
128 outp.setText(buf.toString());
<BR>
129<BR>
130<BR>
131 } //
try reading<BR>
132 catch
(IOException e) {<BR>
133 outp.setText(e.toString());
<BR>
134 }<BR>
135<BR>
136<BR>
137 } // for loop<BR>
138 } // 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>, "GUI
Interfaces with Perl/Tk.") 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 + -