by Rinaldo Digiorgio

Tin cans and string

how-to
Mar 1, 19965 mins

Four methods of inter-applet communication

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:

Enter the following on a server:

nick% java UDPListener
UDPListener Server listening on port:35643

Enter the following on the client:

nack% java UDPTalk nick 35643
UDPTalk Client sending on port:35643

You should see the following output on the server:

Received from inet address: 100.100.200.10 port number: 35644
0
1
2
3
4
5
6
7
8
9

Communication between two Java programs using pipes

This applet demonstrates how to implement a communication method between two threads. The important points are spawning a thread, running the two threads in parallel, creating the input and output streams, and the subsequent tear-down so that no zombie processes are left running. The main thread in the applet creates two pipes and passes them as formal parameters to the instantiated thread class. Click here to run the program; click here to see its source code.

Quote Server from previous SunWorld article

This example has been changed to function with the JDK 1.0 and is included for completeness. It also demonstrates how to integrate communication with a server and a user interface. It is best to download the zip or tar file of this monthโ€™s source code and run it locally. The backend program QuoteServerMain answers requests made by the front end Java client.

Click here to see the source code for the user interface using GridBagLayout.

Click here to see the source code for the helper class to open a TCP connection and get a price.

Click here to see the source code for the background server that supplies the requested data.

Rinaldo S. DiGiorgio works for Sun Microsystems as a systems engineer in the New York City office and provides frequent demonstrations of Java Technology. DiGiorgio is currently working on the integration of many technologies into HotJava/Java. Some of these technologies are database connectivity, portfolio management, low-cost video, and analytical applications in the financial and emerging genetics market. DiGiorgio has been using Unix-based operating systems since 1979, when he was deploying Unix solutions in paper mills. He sees HotJava/Java as the technology to minimize two great cost factors in the computer industry: distribution and code development.