The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Writing Applets
Lesson: Practical Considerations when Writing Applets

## A Simple Network Client Applet

> The Writing a Datagram Client and Server section of the Custom Networking and Security trail contains example code for two applications, a client and a server. This section rewrites the client to be an applet. Here's the `applet source code`. (Here's the corresponding `1.0 source code`.) The client has been changed not only to communicate with the host the applet came from, but also to have a graphical UI, and to have a loop so that it can get as many quotes as you like. You can run the applet by including it in a page with the following HTML code:

> >     <APPLET CODE=QuoteClientApplet.class WIDTH=500 HEIGHT=100>
> >     </APPLET>

> Here's a link to a page that contains the above HTML code. By saving this page to a file on your local HTTP server, you can use it to communicate with the server-side application that will be running on the HTTP server. You must also save the compiled form of the applet to the same directory.

> Before the applet can get quotes, you need to run the server on the host that the applet came from. You then need to note the number of the port that the server is listening on. After you enter this port number into the applet, it will hook up with the server and you'll be able to get one-line quotations. Below are detailed instructions, followed by pictures of the server and the applet in action.

> 1. Compile `QuoteServer.java` and `QuoteServerThread.java`. Here's a text file (one-liners.txt) that should be in the same directory as the resulting class files.
> 2. On the computer that serves the applet class file (through HTTP), invoke the interpreter on the `QuoteServer` class. For example, if you view the applet's page with the URL http://mymachine/quoteApplet.html, then you need to run the server on the host named mymachine.
> 3. Record the port number that the quote server displays.
> 4. Enter this number into the applet's text field.
> 5. Press the Send button to request a quote from the server. You should see a quote appear in the text area.
> Here's a picture of the applet in action:

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.