Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Tin cans and string

Four methods of inter-applet communication

  • Print
  • Feedback
The purpose of the applets I provide here is to demonstrate some ways by which applets communicate with each other and to get you started on your own implementations. Four examples demonstrate ways applets can communicate with each other:

  • Peer-to-peer communication
  • Sending simple UDP datagrams
  • Communication between two Java threads in an applet
  • Quote Server (from a previous article in SunWorld) that you can use to deploy your own client/server applications


Also, there are quite a few additional examples related to networking at the Gamelan Web site.

Many implementations make assumptions you may not wish to make. For example, do you really want to use cgi and post to communicate with a server when the particular browser or firewall restricts communication on a port?

The programs here demonstrate the limitations of browsers that do not support peer to peer ability. I expect that there will soon be a market for browsers that allow communication on sockets determined by users. To get a further understanding of these limitations take a hard look at security constraints.

One last note: don't try to implement ICMP directly without a native method. I was unable to specify a protocol other than TCP or UDP. Future articles will examine a basic SNMP ability from an all-Java program. It is unlikely you will see native methods in this column unless there is some great little hardware device that we just have to talk to, like the Persona card from National Semiconductor. I am considering making digital signatures with encryption/decryption available to Java applets. Stay tuned.

When are we getting to the three-dimensional abilities? I am hoping everyone is interested in an article on 2D imaging and some light 3D examples. Here's a little teaser:

You need a Java-enabled Web browser to view this applet.

Click here for the source.

Peer-to-peer communication

The main program, Talk.java (located in Sockets/classes in this month's source tree; click here to see the source code), implements a two-way communication session using TCP sockets. The sender and receiver communicate over a user-defined port. The Java programs are started by typing

% java Talk hostname port_number


on each of the two systems (the java utility is supplied as part of Sun's Java Developers Kit 1.0). The program proceeds to open the port and listen for data. A window is presented to the user that supports the entry of text messages. Browser versions of the programs require that you install the programs in your local file system and run it from there. The same program runs as both an applet and as a Java main program.

Sending simple UDP datagrams

This program provides a very simple example of how to send UDP packets to a port. Two Java main programs are provided. To test the programs, download the zip file of this article's source and break out the files it contains. The two UDP datagram applets are in the UDP/classes directory; here's the source code: UDPListene r and UDPTalk. First start the server program on a machine on your network to which you wish to send UDP packets. It will list the port number that it is listening on. Then start the client program to send data to it:

  • Print
  • Feedback

Resources