Socket in Applet

Tony Langford (TLangford@witsys.com)
Mon, 3 May 1999 10:12:54 -0400

From: Tony Langford <TLangford@witsys.com>
To: "'java-security@java.sun.com'" <java-security@java.sun.com>
Subject: Socket in Applet
Date: Mon, 3 May 1999 10:12:54 -0400

I'm having problems opening a socket in an applet that's running in a
browser. Here's my situation: I'm using JDK1.2.1 in JBuilder 2 under
Windows NT. I have added a permission to the policy file to allow any
codebase permission to open a socket to any host on port 3000, i.e....

grant {
permission java.net.SocketPermission "*:3000", "accept, connect, listen,
resolve";
permission java.net.SocketPermission "TLANGFORD:3000", "accept, connect,
listen, resolve";
};

In my code I'm opening a socket in the applet's "start". "TLANGFORD" is a
machine on our intranet...

// Start the applet
public void start ()
{
SecurityManager security = System.getSecurityManager ();

if (security != null)
{
try
{
security.checkConnect ("TLANGFORD", 3000);
}
catch (SecurityException sex)
{
textArea1.append ("no can do on the socket,
dude!" + newline);
return;
}
}

// establish socket connection first off, and create in
and out streams
try
{
echoSocket = new Socket ("TLANGFORD", 3000);
out = new PrintWriter (echoSocket.getOutputStream
(), true);
in = new BufferedReader (new InputStreamReader
(echoSocket.getInputStream ()));
}
catch (NumberFormatException err) // thrown from
"parseInt"
{
return;
}
catch (UnknownHostException err) // from "new
Socket"
{
textArea1.append ("DONT'T KNOW ABOUT HOST" +
newline);
return;
}
catch (IOException err) // from
"new Socket"
{
textArea1.append ("COULDN'T GET I/O FOR CONNECTION"
+ newline);
return;
}
}

When I run the applet in the browser, in addition to the "no can do" message
in my text box, I get this in the Java console:

com.ms.security.SecurityExceptionEx[hello/Hello.start]: cannot access
"TLANGFORD":3000
at com/ms/security/permissions/NetIOPermission.check
(NetIOPermission.java)
at com/ms/security/PolicyEngine.deepCheck (PolicyEngine.java)
at com/ms/security/PolicyEngine.checkPermission (PolicyEngine.java)
at com/ms/security/StandardSecurityManager.chk
(StandardSecurityManager.java)
at com/ms/security/StandardSecurityManager.chkex
(StandardSecurityManager.java)
at com/ms/security/StandardSecurityManager.checkConnect
(StandardSecurityManager.java)
at hello/Hello.start (Hello.java:93)
at com/ms/applet/AppletPanel.securedCall0 (AppletPanel.java)
at com/ms/applet/AppletPanel.securedCall (AppletPanel.java)
at com/ms/applet/AppletPanel.processSentEvent (AppletPanel.java)
at com/ms/applet/AppletPanel.run (AppletPanel.java)
at java/lang/Thread.run (Thread.java)

I have an environment variable "java.home" set to the directory containing
the "\lib\security" directory, which is where the policy file is located.
What else am I supposed to do to get this to run? Anything?

Thanks for any help,
Tony Langford
TLangford@witsys.com