Hi, got a problem...
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:
//==============CountServer 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(); } } } //===========================AppletClient 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? And pls gimme something useful..I posted in another forum and i get replied with an Aesop fable quote...=.=