Communicating with Other Programs |
Applets, like other Java programs, can use the API defined in the java.net package to communicate across the network. The only difference is that, for security reasons, the only host an applet can communicate with is the host it was delivered from.It's easy to find out which host an applet came from. Just use the Applet
getCodeBase()
method and the java.net.URLgetHost()
method, like this:If you specify a host name even slightly different from the one the user specified for the applet, you run the risk of a security manager forbidding the communication, even if the two names specify the same host. Using the above code (instead of a hard-coded host name) ensures that your applet will use the correct host name.String host = getCodeBase().getHost();Once you have the right host name, you can use all the networking code that is documented in the Custom Networking and Security trail.
Note: Not all browsers support all networking code flawlessly. Watch the online tutorial for information on the current state of browser networking support.
A Simple Network Client Applet
Here's an example of implementing an applet that's a network client.Using a Server to Work Around Security Restrictions
Here's an example of implementing a server to get around applet security restrictions.
Communicating with Other Programs |