Results 1 to 3 of 3
Thread: AccessControlException
- 03-15-2011, 01:07 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
AccessControlException
Hi, got a problem...:confused:
So, I've got a CountServer class (server) and an AppletClient (client), basically, I run the server in JCreator, then open up the client with HTML. Server is supposed to keep track of the number of times it is accessed by a client.
Below is the code for both:
//=============================
import java.io.*;
import java.net.*;
public class CountServer {
private RandomAccessFile raf;
private int count;
public static void main(String[] args){
new CountServer();
}
public CountServer() {
try{
ServerSocket serverSocket=new ServerSocket(8000);
System.out.println("Server started");
raf=new RandomAccessFile("count.dat","rw");
if(raf.length()==0){
count=0;
}
else{
count=raf.readInt();
}
while(true){
Socket socket=serverSocket.accept();
DataOutputStream outputToClient=new DataOutputStream(socket.getOutputStream());
count++;
outputToClient.writeInt(count);
raf.seek(0);
raf.writeInt(count);
}
}
catch(Exception x){
x.printStackTrace();
}
}
}
//===========================
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
public class AppletClient extends java.applet.Applet {
/** Initialization method that will be called after the applet is loaded
* into the browser.
*/
private JLabel jlblCount=new JLabel();
private boolean isStandAlone=false;
private String host="localhost";
public void init() {
// TODO start asynchronous download of heavy resources
add(jlblCount);
try{
Socket socket;
if(isStandAlone){
socket=new Socket(host,8000);
}
else{
socket=new Socket(getCodeBase().getHost(),8000);
}
DataInputStream inputFromServer=new DataInputStream(socket.getInputStream());
int count=inputFromServer.readInt();
jlblCount.setText("You are visitor number "+count);
inputFromServer.close();
}
catch(Exception x){
x.printStackTrace();
}
}
public static void main(String[] args){
JFrame frame=new JFrame("Applet Client");
AppletClient applet=new AppletClient();
applet.isStandAlone=true;
if(args.length==1){
applet.host=args[0];
}
frame.getContentPane().add(applet,java.awt.BorderL ayout.CENTER);
applet.init();
applet.start();
frame.pack();
frame.setVisible(true);
}
}
//==================
so there...and I get the following exception when I open the HTML:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:8000 connect,resolve)
at java.security.AccessControlContext.checkPermission (Unknown Source)
at java.security.AccessController.checkPermission(Unk nown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at sun.plugin2.applet.Applet2SecurityManager.checkCon nect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at AppletClient.init(AppletClient.java:37)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Error is at the following line:
socket=new Socket(getCodeBase().getHost(),8000);
why is that? P.S, the code is from my lecture notes...@.@
- 03-15-2011, 02:16 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
Global Eco Journey
Beware lest you lose the substance by grasping at the shadow.
- 03-15-2011, 10:37 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
java.security.AccessControlException in Applet
By gopimano in forum New To JavaReplies: 0Last Post: 12-07-2010, 10:57 AM -
How to solve java.security.AccessControlException
By chyrl in forum Advanced JavaReplies: 0Last Post: 09-24-2010, 08:49 PM -
Applet with Signed JAR throws AccessControlException in AppletViewer
By Technolithic in forum Java AppletsReplies: 1Last Post: 07-27-2009, 11:59 AM -
accesscontrolexception in applets
By nishant.4545 in forum Java AppletsReplies: 0Last Post: 07-04-2009, 11:35 AM -
java.security.AccessControlException
By cecily in forum Java AppletsReplies: 1Last Post: 08-06-2007, 02:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks