Applet network and file writing problem

Hey guys,

Here is the thing : I have an applet built with jMonkey and this applet would allow the client to connect to a server. The server would load some file on its hard disk and if this file doesn’t exists, the server writes some stuff in a new file and saves it on the hard disk).

For the moment, I don’t have any server set, so I run the server side of the game on the client, which means that I make the client listen to the 127.0.0.1 host and that if the server wants to save a file, he saves it where the java app is called from, right ?

Now, when I visit my applet page, nothing appears. The Java console gives me this :

[java]Exception in thread “Thread-16” java.security.AccessControlException: access denied (“java.net.SocketPermission” “127.0.0.1:4856” “connect,resolve”)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at sun.awt.AWTSecurityManager.checkPermission(AWTSecurityManager.java:959)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at sun.plugin2.applet.SecurityManagerHelper.checkConnectHelper(Unknown Source)
at sun.plugin2.applet.AWTAppletSecurityManager.checkConnect(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 com.test.testproject.network.Client.tick(Client.java:133)
at com.test.testproject.network.ClientThread.run(Client.java:230)

Exception in thread “LWJGL Renderer Thread” java.security.AccessControlException: access denied (“java.io.FilePermission” “savedgame1” “read”)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at sun.awt.AWTSecurityManager.checkPermission(AWTSecurityManager.java:959)
at java.lang.SecurityManager.checkRead(Unknown Source)
at java.io.File.exists(Unknown Source)
at com.test.testproject.Engine.loadSavedWorld(Engine.java:494)
at com.test.testproject.Engine.simpleInitApp(Engine.java:196)
at com.test.testproject.Engine.simpleInitApp(Engine.java:239)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:228)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:205)
at java.lang.Thread.run(Unknown Source)[/java]

If I got it right, I have two problems here : the applet won’t allow to connect to the local loop and something is wrong when reading a saved file.

So my question is simple : how to solve these two problems ? I guess I forgot something for the first problem. Maybe something with passing some parameters in the applet to allow it to communicate.
For the second problem, it’s more tricky. If I run my game directly from the IDE, the files are saved withtout problems in the directory where I saved my whole project. I guess If I launch my project via an applet, the file would be saved where the cache data downloaded by java are stored: some directory inside another system directory. So, why can’t I save a file to this directory ?

Thanks for the help, guys.

If I’m understanding correctly, it looks like you’re trying to launch the server from within the applet code. Applets are security restricted though, and in particular they are not allowed to write files (I don’t believe they’re allowed to read them either - never played with applets though, so I don’t know all the security gotchas related to them off the top of my head). You’re able to write the file when running from the IDE because the IDE is running it as a normal Java application without the applet restrictions. I don’t know why it’s not allowing you to establish a socket connection, but since it’s a security exception as well it’s most likely related to applet permissions. You can get around the first error by running your server separately from the applet, but I can’t help you with the second problem.

From other non-JME applets, I write to files all the time but you have to request full access in the applet and the user gets a nice big “Do you want to allow it?” message when you run the applet. I would have thought JME applets were already running elevated since they use native code. All security bets are off once native code exists so there shouldn’t be any security restrictions after that.

…but that’s certainly what it looks like.

I don’t know anything about the SDK’s applet support, but somehow you need to get the <all-permissions/> into the security settings for the applet. I’m so clueless I don’t even know if the SDK is using a jnlp file or not for running the applet (the modern way to configure applets).