Applet Originate
I'm not exactly sure what you mean when you say, "the applet is
attempting to communicate with the same server from which it
originated".
Lets say you have a java application file called Server1.class, a java
applet called Applet1.class, and an file html called TestApplet.html
all
stored in designated shared folder on the hard-drive of a PC running
Windows XP. The host name for this PC will be JServer.
You physically go to the JServer PC, open a command window, CD into
the
shared folder where the Server1.class resides and start the
application
from the command line, java Server1 <Enter>. Now you go over to
another
PC on the network, which is also running Windows XP and logon to the
network. We will call this machine JClient. After you logon you map
a
drive to the shared folder on JServer. You then launch IE and then
point to and open the TestApplet.html file in the shared directory on
JServer. The TestApplet.html file automatically calls the
Applet1.class
when it is opened and the Applet1.class then attempts to create a
socket
connection to the server process that is running on JServer.
Here's what I believe is occurring at this point. While the
Applet1.class file is stored on the same machine as the Server1.class,
I
do not believe that is where Applet1 will originat. I believe
that the term originate refers to the machine that actually launched
the
applet, which in this case is the JClient machine. Assuming I am
correct with these assumptions, how would you go about getting
the Applet1.class to instantiate (originate) itself on the JServer
machine from the JClient PC?
I have included all of the source code below for this example. I have
also
included the output that occurs when the applet is called from Jserver
and
when it is called from JClient below to illustrate this example.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
<HTML>
<HEAD>
<TITLE>Test Applet Lookup Applet</Title>
<CENTER><H1>Test Applet</H1></CENTER>
</HEAD>
<BODY>
<APPLET CODE=Applet1.class WIDTH=760 HEIGHT=500></APPLET>
</BODY>
</HTML>
/* Program Name: Applet.java
Author: Steven J. Rulison
Date: November, 18 2004
Purpose: Demonstrate a socket connection over the network.
*/
import java.applet.*;
import java.net.*;
public class Applet1 extends Applet
{
//Constructor
public Applet1()
{
try
{
Socket s = new Socket("JServer", 1427);
/*
Flip Larsen suggested that I use the
Applet.getCodeBase().getHost() method. I didn't have
much luck with it. I think I need to see an actual
example of how it's used. This is what I tried:
Socket s = new Socket(Applet.getCodeBase().getHost(),
1427);
I also tried printing to the command console and a null value
returned.
System.out.println(Applet.getCodeBase().getHost());
*/
System.out.print("Socket connection successful.");
}
catch(Exception e)
{
e.printStackTrace();
System.out.print("Socket connection unsuccessful.");
}//End of catch block.
}//Constructor
}//End of class Applet1.
/* Class name: Server1
Author: Steven Rulison
Date: November, 18 2004
Purpose: Demonstrate a socket connection over the network. This is
the Server portion.
*/
import java.net.*;
import java.io.*;
public class Server1 extends Thread
{
public static void main(String[] Args)
{
try
{
ServerSocket ss = new ServerSocket(1427);
Socket s = ss.accept();
}catch(IOException e)
{
e.printStackTrace(System.err);
}
}//End of main.
}//End of class Server1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
Applet console output when the applet is launched from from the
Client:
Java Plug-in 1.5.0
Using JRE version 1.5.0 Java HotSpot(TM) Client VM
User home directory = E:\Documents and Settings\sruliso
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
p: reload proxy configuration
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
java.security.AccessControlException: access denied
(java.net.SocketPermission JServer resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getByName(Unknown Source)
at java.net.InetSocketAddress.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Applet1.<init>(Applet1.java:21)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown
Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Applet console output when the applet is launched from the server:
Java(TM) Plug-in: Version 1.4.2_03
Using JRE version 1.4.2_03 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\ADMINISTRATOR
Proxy Configuration: Automatic Proxy Configuration
URL:
http://webt1resv.ilptab.il.us:8080/array.dll?Get.Routing.Script
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
p: reload proxy configuration
q: hide console
r: reload policy configuration
s: dump system properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------