Solved

AppletSecurityException: checkRead

Posted on 1998-07-19
11
152 Views
Last Modified: 2010-03-30
I've created a Yahtzee simulation game.  I am trying to update and include a method that keeps track of High Scores.  The way that I am doing this is creating a Sequential access file ('scores.dat') and reading the scores to and from it.  However when I try to view the applet with netscape 4.05 I get a netscape.security AppletSecurity Exception: checkRead: read 'scores.dat' not permitted.  The scores.dat file is in the same directory as the .class file for the game.  I can't seem to figure out how to fix this problem.  Can someone please help....I thought this was going to be an easy but fun add on, man was I wrong!
0
Comment
Question by:ata071998
  • 6
  • 5
11 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 120 total points
ID: 1227070
ata, what you want to do is write and read file on the server. Reading a file from your class location is realatively easy. You just open a URL to that file and read it. However writing may prove to be a bit tricky. The main problem is that any number of players might want to update the same file on the server in the same time. This file should be synchronized somehow. There are couple of options to write the file to the server:
1. Use ftp from the client game to the server.
2. Open a URL for writing.
3. Run a tiny high score server on the server machine.
1 and 2 are problematic because of the synchronization problem. 3 is the best in my opinion, but the only question is - can you run a process on the server machine? Tell me what is your preferred way and we will continue.
0
 

Author Comment

by:ata071998
ID: 1227071
How do I just open a URL to it and read.  I thought that was what I was trying to do that gives me the error AppletSecurityException in the first place.  I don't really want to worry about the next part yet, even though I like the idea of a small server, because I haven't even been able to read in the scores yet and check for logic errors, etc.  
0
 
LVL 6

Expert Comment

by:gadio
ID: 1227072
About the server thing: if you want to do that its quite simple. In java this server will be very fast to write.
About the reading: Well, just use the following scheme:

URL score_url = new URL( myapplet.getCodeBase(), "scores.dat" );
InputStream is = score_url.openStream();
//... read the data ...
is.close();

Please note that this url *must* be on the same machine as the applet comes from (according to what you did, thats ok), and check the file's permitions (make is readable for all).
If this still fails, try and paste in the problematic code.

0
 

Author Comment

by:ata071998
ID: 1227073
Alright, I have all of the info reading in correctly.....now let's talk servers.  I don't have any experience yet with servers, but there is no time like the present to start.  The only initial question I have is can the server wait to be started untill a client either starts the applet or when he connects....Or does a server continuously run.  The latter would be bad.
0
 

Author Comment

by:ata071998
ID: 1227074
I've been consulting some of the different materials have available to me concerning client/server interaction within java.  I understand the basics and the background material.  I guess I'm not sure on how you had planned on implementing it for updating the highscores file.  You suggested that it should be relatively quick and simple to write.  Could we go inot a little more detail so that I can wrap my brain around it.  I'm itchin to get this working so that I can move on with my life!  Thanks for your help:-)
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:ata071998
ID: 1227075
gadio: are you still out there?
0
 
LVL 6

Expert Comment

by:gadio
ID: 1227076
Sorry about the response time... Long weekend UC    ;-)
I'll get back to you soon with some examples.
0
 

Author Comment

by:ata071998
ID: 1227077
not a problem, i was just getting worried that you disappeared!  hopefully the long weekend was for pleasure and you had fun!  
0
 
LVL 6

Expert Comment

by:gadio
ID: 1227078
Ok ata. The server is realy a simple problem. I'd like to start with the more theoretical problem: the synchronization of a resource. I'd recommend to use the readers writers pattern. Have a look in the following code and tell me if you have any Qs. If you run the test programe (java ReadersWritersSynchronizer), you can see example of how it works.

1. file ReadersWritersSynchronizer.java



/****************************************************************************************
 *
 * Readers Writers
 * ---------------
 *
 * The folowing code deals with the known readers writers problem. Any number of readers
 * can read the data. Once there is someone waiting for write, the readers amount must
 * go down to 0. When this happens, a writer comes in and write. This goes on until
 * there is no more writers waiting.
 *
 ****************************************************************************************/


public class ReadersWritersSynchronizer {
    Object data;
    int readers_working = 0;
    int writers_waiting = 0;
    public boolean debug = false;

    ReadersWritersSynchronizer( Object the_data ) {
        data = the_data;
        showDebugInfo();
    }

    public void requestRead( ReaderWriter reader ) {
        synchronized(this) {
            while( writers_waiting != 0 ) {
                // someone is writing now.
                try {
                    wait();             // go to wait state, release the monitor.
                    // reaquire the monitor (the number of writers may have changed).
                } catch( Exception e ) {}
            }
            readers_working++;
            showDebugInfo();
        }

        // do the actual work:
        reader.read(data);

        synchronized(this) {
            readers_working--;
            showDebugInfo();
            notifyAll();
        }
    }


    public void requestWrite( ReaderWriter writer ) {
        synchronized(this) {
            writers_waiting++;
            showDebugInfo();
            while( readers_working != 0  ) {
                // there is some reading going on.
                try {
                    wait();                  // go to wait state, release the monitor.
                    // reaquire the monitor (the number of writers and readers may have changed).
                } catch( Exception e ) {}
            }
            // At this point, the current thread is the sole writer.
        }

        // do the actual work:
        writer.write( data );

        synchronized(this) {
            writers_waiting--;
            showDebugInfo();
            notifyAll();
        }
    }

    private void showDebugInfo() {
        if( debug ) System.out.println( "Readers working: "+readers_working+"   Writers waiting:"+writers_waiting);
    }

    public static void main( String args[] ) {
        int TESTERS = 50;
        Object data = new Object();
        ReadersWritersSynchronizer sync = new ReadersWritersSynchronizer( data );
        sync.debug = true;

        ReadersWritersTestObject tobjs[] = new ReadersWritersTestObject[TESTERS];
        for (int i = 0; i < TESTERS; i++ ) {
            tobjs[i] = new ReadersWritersTestObject(sync);
        }
        for (int i = 0; i < TESTERS; i++ ) {
            (new Thread(tobjs[i])).start();
        }
    }
}


class ReadersWritersTestObject implements Runnable, ReaderWriter {
    ReadersWritersSynchronizer sync;

    ReadersWritersTestObject( ReadersWritersSynchronizer s ) {
        sync = s;
    }

    public void run() {
        while( true ) {
            if( Math.random() > 0.1 ) sync.requestRead( this );
            else sync.requestWrite( this );
        }
    }

    public void read( Object o ) {
        System.out.println( ""+this+" reading.");
        try {
            Thread.currentThread().sleep( (long)(Math.random() * 3000) + 100 );
        } catch (Exception e) {}
    }

    public void write( Object o ) {
        System.out.println( ""+this+" writing.");
        try {
            Thread.currentThread().sleep( (long)(Math.random() * 3000) + 100 );
        } catch (Exception e) {}
    }
}



2. file ReaderWriter.java


public interface ReaderWriter {
    public void write( Object dat );
    public void read( Object dat );
}

0
 
LVL 6

Expert Comment

by:gadio
ID: 1227079
No Qs IC. Cool. Based on this we can develop a server that will do somthing close to what you want:


1. file readers_writers_server.java


/****************************************************************************************
 *
 * readers_writes_server
 *
 ****************************************************************************************/


import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Hashtable;

public class readers_writes_server implements Runnable {
    int port;
    String filename;
    Hashtable data;
    Hashtable dedicated_servers;
    ReadersWritersSynchronizer sync;
    Thread mythread;

    /**
     * Constructor.
     */
    public readers_writes_server ( int port, String filename ) {
        this.port = port;
        this.filename = filename;
        data = loadData(filename);
        dedicated_servers = new Hashtable();
        sync = new ReadersWritersSynchronizer( data );
        mythread = new Thread(this);
        mythread.start();
    }

    public void run() {
        try {
            ServerSocket srvs = new ServerSocket( port );
            while(1) {
                Socket inconnect = srvs.accept();
                DedicatedServer ds = new DedicatedServer( inconnect, this, filename, sync );
                dedicated_servers.put( ds, ds );
            }
        } catch( IOException e ) {
            System.out.println(e);
        }
    }

    public Hashtable loadData( String filename ) {
        try {
            FileInputStream fin = new FileInputStream(filename);
            ObjectInputStream oin = new ObjectInputStream(fin);
            Object obj = oin.readObject();
            oin.close();
            return (Hashtable)obj;
        } catch( Exception e ) {
            System.out.println(e);
        }
        return new Hashtable();
    }

    public void saveData( String filename, Object data ) {
        try {
            FileOutputStream fout = new FileOutputStream(filename);
            ObjectOutputStream oout = new ObjectOutputStream(fout);
            oout.writeObject(data);
        } catch( Exception e ) {
            System.out.println(e);
        }
    }

    public void dedicatedServerEnd(DedicatedServer ds) {
        dedicated_servers.remove( ds );
    }

    private static void printUsage() {
        System.out.println( "Usage:" );
        System.out.println( "java readers_writes_server [help | test | filename]" );
    }

    public static void main(String args[]) {
        if( args.length == 0 || args[0].equals("help") || args[0].equals("h") ) {
            printUsage();
            System.exit(0);
        }
        else if( args[0].equals("test") ) {
            //Many writers, one reader
            readers_writes_server server = new readers_writes_server( 2222, "testFile" );
            for(int i = 0; i < 3; i++ ) new TestClient(false);
            new TestClient(true);
        }
        else {
            readers_writes_server server = new readers_writes_server( 2222, args[0] );
        }
        while(true) try {Thread.currentThread().sleep(10000);} catch(Exception e){}
    }

}

/****************************************************************************************
 *
 * TestClient
 *
 ****************************************************************************************/

class TestClient implements Runnable {
    boolean reader;
    Thread mythread;
    static int client_number = 1;
    int mynumber;
    ObjectInputStream oin;
    ObjectOutputStream oout;

    TestClient( boolean reader ) {
        mynumber = client_number++;
        mythread = new Thread(this);
        mythread.start();
        this.reader = reader;
    }

    public void run() {
        String ip = "127.0.0.1"; // the localhost;

        try { Thread.currentThread().sleep( (int)(2000) ); }
        catch( Exception e ) {}

        // In an applet client you need only this section (start)
        try{
            while(true) {
                Socket conn = new Socket(ip,2222);
                oout = new ObjectOutputStream( conn.getOutputStream() );
                oin = new ObjectInputStream( conn.getInputStream() );
                if( reader ) read();
                else write();
                Thread.currentThread().sleep( (int)(Math.random() * 2000 + (reader?100:6000)) );
                conn.close();
            }
        } catch( Exception e ) {
            System.out.println(e);
        }
        // In an applet client you need only this section (end)
    }

    private void read() {
        System.out.println("testClient_"+mynumber+" reading");
        try {
            oout.writeObject( new Request( Request.COMMAND_UPDATE_CLIENT, null, null ));
            Hashtable hash = (Hashtable)oin.readObject();
            System.out.println( "read from the server:" );
            System.out.println( hash );
        } catch( Exception e ) {
            System.out.println(e);
        }
    }

    private void write() {
        System.out.println("testClient_"+mynumber+" writing");
        try {
            oout.writeObject( new Request( Request.COMMAND_WRITE_VAL, "TestClient_"+mynumber, "num_"+Math.random() ));
        } catch( Exception e ) {
            System.out.println(e);
        }
    }
}

/****************************************************************************************
 *
 * This class is a server that works with one client.
 *
 *
 ****************************************************************************************/


class DedicatedServer implements Runnable, ReaderWriter {
    readers_writes_server server;
    String filename;
    ReadersWritersSynchronizer sync;
    Socket connection;
    InputStream in;
    OutputStream out;
    ObjectInputStream o_in;
    ObjectOutputStream o_out;
    Thread mythread;

    DedicatedServer( Socket connection, readers_writes_server server, String filename, ReadersWritersSynchronizer sync ) {
        this.server = server;
        this.filename = filename;
        this.sync = sync;
        this.connection = connection;

        try {
            in = connection.getInputStream();
            out = connection.getOutputStream();
            o_in = new ObjectInputStream(in);
            o_out = new ObjectOutputStream(out);
        } catch( Exception e ) {
            System.out.println( e );
        }
        mythread = new Thread(this);
        mythread.start();
    }

    public void run() {
        try {
            Object obj = o_in.readObject();
            if( obj instanceof Request ) {
                Request req = (Request)obj;
                switch( req.command_type ) {
                    case Request.COMMAND_WRITE_VAL:
                        sync.requestWrite( this, req );
                        break;
                    case Request.COMMAND_UPDATE_CLIENT:
                        sync.requestRead( this, req );
                        break;
                    default:
                        break;
                }
            }
            else {
                System.out.println( "Client sent giberrish:"+obj);
                // this server is going out.
            }
            //closing down...
            in.close();
            out.close();
            connection.close();
            server.dedicatedServerEnd(this);
        } catch( Exception e ) {
            System.out.println( e );
        }
    }

    public void read( Object data, Object req_obj ){
        // Send the data to the client
        try{
            o_out.writeObject(data);
        } catch( Exception e ){
            System.out.println( e);
        }
    }

    public void write( Object data, Object req_obj ){
        // This thread is now the sole writer.
        Hashtable hash = (Hashtable)data;
        Request req = (Request)req_obj;
        hash.put(req.data1, req.data2);
        server.saveData(filename, hash);
    }
}



2. file ReaderWriter.java

public interface ReaderWriter {
    public void write( Object dat, Object info );
    public void read( Object dat, Object info );
}


3. file Request.java


import java.io.Serializable;

public class Request implements Serializable {
    public static final int COMMAND_UPDATE_CLIENT = 1;
    public static final int COMMAND_WRITE_VAL = 2;

    public int command_type;
    public String data1;
    public String data2;

    public Request( int command_type, String data1, String data2 ) {
        this.command_type = command_type;
        this.data1 = data1;
        this.data2 = data2;
    }
}



4. ReadersWritersSynchronizer.java



/****************************************************************************************
 *
 * Readers Writers
 * ---------------
 *
 * The folowing code deals with the known readers writers problem. Any number of readers
 * can read the data. Once there is someone waiting for write, the readers amount must
 * go down to 0. When this happens, a writer comes in and write. This goes on until
 * there is no more writers waiting.
 *
 ****************************************************************************************/


public class ReadersWritersSynchronizer {
    Object data;
    int readers_working = 0;
    int writers_waiting = 0;
    public boolean debug = false;

    ReadersWritersSynchronizer( Object the_data ) {
        data = the_data;
        showDebugInfo();
    }

    public void requestRead( ReaderWriter reader, Object info ) {
        synchronized(this) {
            while( writers_waiting != 0 ) {
                // someone is writing now.
                try {
                    wait();             // go to wait state, release the monitor.
                    // reaquire the monitor (the number of writers may have changed).
                } catch( Exception e ) {}
            }
            readers_working++;
            showDebugInfo();
        }

        // do the actual work:
        reader.read( data, info );

        synchronized(this) {
            readers_working--;
            showDebugInfo();
            notifyAll();
        }
    }


    public void requestWrite( ReaderWriter writer, Object info ) {
        synchronized(this) {
            writers_waiting++;
            showDebugInfo();
            while( readers_working != 0  ) {
                // there is some reading going on.
                try {
                    wait();                  // go to wait state, release the monitor.
                    // reaquire the monitor (the number of writers and readers may have changed).
                } catch( Exception e ) {}
            }
            // At this point, the current thread is the sole writer.
        }

        // do the actual work:
        writer.write( data, info );

        synchronized(this) {
            writers_waiting--;
            showDebugInfo();
            notifyAll();
        }
    }

    private void showDebugInfo() {
        if( debug ) System.out.println( "Readers working: "+readers_working+"   Writers waiting:"+writers_waiting);
    }

    public static void main( String args[] ) {
        int TESTERS = 50;
        Object data = new Object();
        ReadersWritersSynchronizer sync = new ReadersWritersSynchronizer( data );
        sync.debug = true;

        ReadersWritersTestObject tobjs[] = new ReadersWritersTestObject[TESTERS];
        for (int i = 0; i < TESTERS; i++ ) {
            tobjs[i] = new ReadersWritersTestObject(sync);
        }
        for (int i = 0; i < TESTERS; i++ ) {
            (new Thread(tobjs[i])).start();
        }
    }
}


class ReadersWritersTestObject implements Runnable, ReaderWriter {
    ReadersWritersSynchronizer sync;

    ReadersWritersTestObject( ReadersWritersSynchronizer s ) {
        sync = s;
    }

    public void run() {
        while( true ) {
            if( Math.random() > 0.1 ) sync.requestRead( this, null );
            else sync.requestWrite( this, null );
        }
    }

    public void read( Object o, Object info ) {
        System.out.println( ""+this+" reading.");
        try {
            Thread.currentThread().sleep( (long)(Math.random() * 3000) + 100 );
        } catch (Exception e) {}
    }

    public void write( Object o, Object info ) {
        System.out.println( ""+this+" writing.");
        try {
            Thread.currentThread().sleep( (long)(Math.random() * 3000) + 100 );
        } catch (Exception e) {}
    }
}



That can be a good base for you to start with. Tell me if you have any questions.
G.

0
 
LVL 6

Expert Comment

by:gadio
ID: 1227080
In order to implement a high score logic, use the code in the test client in your applet and change its logic.

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now