Client/Server Using Java Applets and Perl
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.
The information in this chapter is based on the assumption that you have read Chapter 12, "Using Sockets." In this chapter you'll learn how to connect a Java applet to your socket. This chapter is not 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."
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 FORM or simply via a URL. The output of your CGI script can even exist on disk in the form of a file.
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.
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.
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.
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.
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.
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.
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.
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.
Listing 28.1. The Perl script to handle server-side details.
Listing 28.2. Using URLs to display the data.
The Java programming language was developed at Sun Microsystems Inc., Mountain View, California, by a group headed by James Gosling. Java is a platform-independent, object-oriented language developed primarily for use in household appliances. Java's usefulness extends far beyond the initial intended use for developing it. Java appeared at the same time as the World Wide Web (WWW) and was used by Sun to develop WebRunner, a Web browser written entirely in Java. (WebRunner was later renamed HotJava to avoid copyright problems.)
The Java Programming Language is very similar to, but much simpler than, C++. Numbers and Boolean types serve as basic building blocks for developing Java classes for object-oriented programming. Programmers can create their own objects as classes. All classes in Java are restricted to single-level inheritance only, because a class in Java may only inherit from one class at a time. Abstract classes in Java allow definitions of interfaces to permit multiple inheritance.
A Java applet is the result of running a Java compiler on Java source code. Basically, in order to create a Java applet, you need to have a Java Developer's Kit (JDK) on your system. You can get a JDK for your machine from the Web site http://www.javasoft.com. Included in a JDK distribution are compilers, documentation, and source code for some sample applets that folks were kind enough to donate for use, with limited copyright restrictions of course, to the general computing community.
A Java source file can consist of several objects to implement a certain set of functionality. The source code for an applet when run through a Java compiler, javac, produces one or more .Class files, one for each type of object defined in the source file. Each .Class file is a series of byte codes that are then referred to as "applets."
Applets reside on servers and are downloaded to clients when referenced via a URL. Applets execute on remote sites under the auspices of the browser that downloaded them. The address space and code execution are modeled under a process known as the Java Virtual Machine (JVM). The JVM describes how to implement Java applet code and addresses issues regarding the ways to parse the byte codes in an applet. Java is designed to be secure and robust enough to prevent any applet's byte code's instructions to compromise the client machine's system. By using the JVM model, byte codes can be verified by javac to ensure that the resulting applet will not compromise system integrity.
Java has support for multithreaded applications built into the language. A browser running an applet can manage multiple threads for a Java applet. Java threads are mapped by the browser into the underlying operating system threads if the underlying system supports it. Java-enabled browsers are designed and built for each type of platform, such as Windows NT, 95, Macintosh, and UNIX workstations.
Java also has support for network communication via the use of TCP/IP or UDP sockets as well as with the use of URLs. In essence, Java is a distributed language because Java applets can retrieve data from any accessible node on the Internet. The permissions available for a Java applet to access a file on the Internet can be the same as those of the browser running the applet. Java applets cannot have file access permissions that are greater than that of their host browser for security reasons. Items accessed via URLs include other HTML pages containing applets, raw files or images, or links to other sites. Socket support under Java is implemented using Berkeley (BSD) Socket Extensions.
For more information about programming in Java, please consult the books in the section "For More Information on How to Program in Java," later in this chapter.
Line 18 of Listing 28.2 starts a thread for the applet and declares the l28 class. The Java program gets the parameters to itself in lines 26 and 35. A TextArea (ta) widget is declared and added to the applet for the applet to display the incoming data.
Lines 26 and 32 show the code to extract the two input parameters to the applet: URL and DELTA. The URL parameter is a string containing the URL of where the data is located. The DELTA parameter is used to determine the time interval between successive tries to extract the data. Default values are used when incoming values are absurd or absent.
The start() and stop() functions in lines 63 and 74 are called when the applet thread starts and stops. The code in line 63 is called when a background thread is created in the applet to do socket communication. The subroutine in line 74 is called when the thread stops executing.
The bulk of the work is done in the run() subroutine in line 84, where the running thread connects to the server in lines 95 to 97, extracts the data in the while loop in line 99, and then displays the received text in a text area for the applet in line 102.
If there is an exception, it's of the type IOException and is caught at line 104, where a message is displayed on the system status bar. The try-and-catch exception-handling mechanism shown in lines 94 to 105 to fetch data is very common in Java applet source code. The code in line 105 notifies the applet user of exceptions like missing data or a bad URL value. The text area actually updates the screen in line 112 with the repaint() call.
The loop is repeated starting from the section in line 117 to fetch the next updated URL.
Once you have typed the code for the applet, you have to compile it with the javac compiler. The Java applet is compiled to a l28.class with the following command:
The result of the command is a file called l28.class in the same directory. The prefix of the file and the class name must be the same.
To use the applet in your HTML document, you have to use <APPLET> and </APPLET> tags. A sample usage is shown Listing 28.3.
Listing 28.3. HTML page for using an applet to get text from a server.
The code in line 7 shows how to include the applet code. The width and height of the applet are required parameters. The applet will not load if you do not specify the height and width. Line 8 sets the parameter URL to the applet and gives it a value of a complete URL. You can specify relative URLs as well. Line 9 specifies a second parameter for the time interval between updates as a string. The interval can be specified as a string, but this way you'll see how to use code in the applet to convert from a string to an int. The applet is closed out at line 10 with the </applet> tag.
At line 117 of Listing 28.2, the program sleeps for the DELTA period specified. As just seen with I/O handling, this sleep call also catches the exception of the type InterruptedException in cases of errors. Note that the sleep time in applets is in milliseconds, whereas in Perl it's in seconds.
The run() function loops forever until the applet is destroyed by the user selecting another HTML page.
As stated earlier, this chapter is not about teaching you to write Java applets. Instead, this chapter shows you how it's possible to interface with applets using CGI. The method shown in this section is a bit crude in that the applet has to wake up and fetch a document every time. Even cruder is the fact that the Perl script at the other end has to open a file and then write to and close it every so many seconds. You might consider keeping the file open at all times in the Perl server script.
Another alternative to the scheme shown here is to use UNIX system facilities to use sockets.
The second example of using Perl and Java together involves using sockets for client/server communication. The server is written in Perl, and the client is a Java applet on a client browser.
The approach to using sockets over files has the obvious advantage of speed and more efficient use of system services. Of course, your advantage is minimized a lot if you are echoing what you write to the socket back onto the disk.
Also, Perl is portable to platforms that might not support sockets. For example, Windows-based systems might not have Winsock loaded correctly, or they might have the wrong version loaded, with which they can run the browser but cannot access the socket's features. In such a case where sockets are not supported on your server side (a rare but true possibility), the first method of using URLs alone will have to do.
The server application is pretty straightforward. It binds itself to a port and listens for a connection. After getting a connection, the server in this example sends 1,000 data items back to the connecting client. Each data item sent back has an identifier that is set to the current time. The identifier is often referred to as the time stamp for this packet. (See Listing 28.4.)
The server in this example can be extended to use the fork system call and handle requests via children. The sample shown here simply bails out after it's done. For a discussion of how this server works, refer to Chapter 12. See Listing 28.4 for a sample Perl server that simply listens to a socket and, on receiving a connection, sends some dummy data back to the calling client.
Listing 28.4. Connection-oriented server using Perl.
Note that in line 24 in the server script shown in Listing 28.4, all output is flushed immediately to the socket with the $|=1; command. The client Java applet to talk to the server is also shown. Two classes are defined in this source file:
After you compile the Java program in Listing 28.4, you'll get two files in your directory:
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.
The applet itself is included in an HTML file, as illustrated by Listing 28.5.
Listing 28.5. Using the socket applet.
Lines 6 through 9 define the parameters into the new applet, called cl28.class. The required width and height parameters are also provided where the applet is declared.
The code for the applet itself is shown in Listing 28.6.
Listing 28.6. Java applet using sockets.
1 // --------------------------------------------------------
In this Java applet, lines 105 to 109 in the StreamComm constructor copy the values of local variables in the applet. Line 120 is where the StreamComm thread reads the incoming input and sets it in a string buffer. The helper class StreamComm loops forever, until the applet is no longer referenced in its containing HTML document.
The online information on Java is perhaps the best source for information on how to write Java applets. Here are the sites to visit:
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:
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.
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 Chapter 17, "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.
With any suggestions or questions please feel free to contact us