Connecting a normal app and an applet to the server within the same project?

i did a little background check on how applet can talk with servers. well they can only talk with the server they are loaded from without giving any special permissions.

but here starts my problem. i’m writing a normal SimpleApplication but also want to export it as an applet. if the server runs on the same machine as the http-server there shouldn’t be a problem for the applet.

well i got the stand-alone application working with the java.rmi and the jmonkeyzone way. but either don’t work in the applet too.

but i just can’t figure out how i should write my code to let the stand alone application connect from any pc to my server and at the same time tell my applet that it should connect to the server it was loaded from, because i don’t want to do the work twice to creating 2 separate projects with just one code line thats different.

normally if u write an applet u would extend from JApplet and use the applet function to get the server address and just send the information back from where it came. but with the normal application in mind i think i have to use the direct way to connect to an ip address.

and another thing i-was wondering about. if i start the generated html file without a http-server behind it i can connect my applet with java.rmi to the server. with jmonkey i don’t get the popup which asks if i really wanna grant permissions to connect to the address. same connection problem happens if i start the applet (java.rmi and jmonkey connection) via apache.

in short:

a) how do i write the connection function for my applet and stand alone app at the same time without needing special permissions?

b) how can i enable the popup which asks if i wanna run the applet or granting it permissions?

anyone has an idea how to solve my problem?

PS: hope it isn’t too confusing ^^

Well depending on your description I suggest to use webstart, however

alternativly you can let the applet request additional permissions (like file access and networking)

well and how do i get additional permissions?

i tried setting the policy anew or disable the security manager and also AccessController.doPrivileged… but i always get an exception and nothing more. no popup with buttons to grant or deny access. of course i tried signing applets but it always end up the same way.

any other ideas? beside telling the user to create a policy file in the home directory?

You don’t need any permissions to connect to the server where the applet is hosted so there’s no need to ask for any. If there’s an exception, that could be for different reasons. Also you do not need to sign your applet.

You can access the JApplet/Applet that was used to load the application by using AppletHarness.getApplet(), if its null then you know you’re running as a standalone application. You can also get the server’s IP address using that method

mmh ok i will try that. thx

Update: kk i got it running. The thing from Momoko_Fan really worked well.

A little tip for everyone who gets the same problem and tries to do the connection with java.rmi

Applet a = AppletHarness.getApplet(main);
if (a == null){
Registry reg = LocateRegistry.getRegistry("", port);
ni = (NetworkInterface)reg.lookup("ExampleFunction");
Registry reg = LocateRegistry.getRegistry(a.getCodeBase().getHost(), port);
ni = (NetworkInterface)reg.lookup("ExampleFunction");
Object obj = ni.callWhatEverFunctionYouWannaCall();

it was really frustrating just figuring out these little lines but at least i know a little bit more about applets after all.
IMPORTANT NOTICE FOR LOCAL DEVELOPING! Clicking the html file won't work with this method. I use xampp but other webserver might work as well. the applet only runs if u put the files in the http-folder of your webserver(how surprising) and call the website with your network ip! this is a little tricky because lwjgl applet manager doesn't accept or localhost as a webserver-address. so instead of using http://localhost/index.html u have to use http: for example.
If u try it with localhost or it can get really frustrating because u get a connection but no data will be transmitted (maybe even get an AccessControlException).