Results 1 to 3 of 3
  1. #1
    DarthCoffee is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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...@.@

  2. #2
    Ownenueaffows is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Global Eco Journey

    Beware lest you lose the substance by grasping at the shadow.

  3. #3
    DarthCoffee is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    ....uh, thx 4 d advice Mr. Sage but how does an Aesop's fable quote help here? O.o

Similar Threads

  1. java.security.AccessControlException in Applet
    By gopimano in forum New To Java
    Replies: 0
    Last Post: 12-07-2010, 11:57 AM
  2. How to solve java.security.AccessControlException
    By chyrl in forum Advanced Java
    Replies: 0
    Last Post: 09-24-2010, 09:49 PM
  3. Replies: 1
    Last Post: 07-27-2009, 12:59 PM
  4. accesscontrolexception in applets
    By nishant.4545 in forum Java Applets
    Replies: 0
    Last Post: 07-04-2009, 12:35 PM
  5. java.security.AccessControlException
    By cecily in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 03:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •