![]() ![]() ![]() ![]() |
Communicating with Other Programs |
As the Understanding Applet Capabilities and Restrictionstrail explains, applets are subject to many security restrictions. For example, they can't perform file I/O, they can't make network connections except to their original host, and they can't start programs.
One way of working around these restrictions is to use a server application that executes on the applet's host. The server won't be able to get around every applet restriction, but it can make more things possible. For example, a server can't save files on the host the applet's running on, but it'll be able to save files on the host the applet originated from.
This page features an example of a server that allows two applets not on the same page to communicate with each other. The applets can be executing on the same computer or on two different computers -- as long as they originate from the same computer, they can communicate through the server that's running on that originating computer. The example uses sockets, which are documented in All about Sockets.
![]()
Note: For some reason, this example works with Applet Viewer but not with Netscape Navigator. In Netscape, the rendezvous is successful, but the applets always say "NOTE: Couldn't read from socket." (which means an IOException occurred). The output stream seems to work, though, because the applet data gets to the server. (The server seems to see nothing wrong.) Perhaps Netscape has a bug on reading DataInputStream?
Here are the source files:
- TalkClientApplet.java
- The source file for the client applets. (Both applets use the same source code.) After you compile it, you can run it by including it in an HTML page with this tag:
<applet code=TalkClientApplet.class width=550 height=200> </applet>- TalkServer.java and TalkServerThread.java
- The source files for the server. After compiling both files, you can run the server on the applets' originating host by invoking the interpreter on the TalkServer class.
Here's a picture of the server in action:
![]()
Here are pictures of the applets in action:
![]()
![]()
![]() ![]() ![]() ![]() |
Communicating with Other Programs |