applets no longer have permission to access "localhost"???

recently i was trying to test a game i have been working on. i have been working on this game on and off for a while and it involves sockets. to test the game i usually just run the server application and the applet connects to “localhost”. however it seems with a recent update of java, applets loaded on the same machine no longer have permission to access “localhost”. whats the best way around this for me to keep testing my game? if at all possible i do not want to modify the permissions file. if i ever put this game on the net applets are still allowed access back to the host server without modification of the permissions file right?

thanks,
scov

Being able to access both localhost and the server the applet was loaded from sounds like a pretty huge security flaw, so I wouldn’t count on being able to do that ever again.
Were you testing it on a locally hosted server before?

Make sure you only use the hostname provided by applet.getDocumentBase().getHost().

If you try to connect to 127.0.0.1 but you’re serving from 192.168.x.x or 10.0.0.x, then you get a AccessControlException, like you should.

im still relatively new to java but a month or two ago i could run my server application and the game applet all on my computer. Im using sockets and the code in the server looks like

ssock = new ServerSocket(4444); 

4444 or any arbitrary port number used to work.

and the applet has code like

sock = new Socket("localhost",4444);

i dont have a server i was just trying to simulate internet play all on the same machine so i could test my game. this method worked a month or two ago and i recently tried to run my game again(no changes to any code) and i get the AccessControlException…

whats the best way for me to keep simulating internet play all on the same machine?

If you want to use sockets you’ll need to sign the applet to give it permission to do so.
Sockets are not ideal for applets for this reason, but if people are prepared to accept your certificate (a la Runescape) it’ll work fine.

I agree that you should use something like

new Socket(applet.getDocumentBase().getHost(),4444)

instead of localhost. It should wind up being localhost if you are running it all on the same machine.

even with getHost() i dont think it will work because of a recent java update. as for signing i thought applets could connect back to the host server without being signed???

if sockets are not ideal for applets what is a better approach? and if the applet must be signed what exactly does that mean and how do you do it?

For the sake of fluffy bunnies… try it.

i did and it still doesn’t work…

this is what i tried…

the applet looks like this…

import java.applet.*;
import java.net.*;
import java.io.*;

public class testing extends Applet
{
    Socket s = null;
       
    public void init()
    {
        try
        {
            s = new Socket(getDocumentBase().getHost(),4444);
        }
        catch (IOException e)
        {}
        
    }
    
}
           

and the server application looks like this…

import java.applet.*;
import java.net.*;
import java.io.*;

public class server
{
    public static void main (String args[])
    {
        ServerSocket s = null;
        Socket c = null;
        
        try
        {
            s = new ServerSocket(4444);
        }
        catch(IOException e)
        {
            System.out.println("error");
            System.exit(1);
        }
        
        try
        {
            c = s.accept();
        }
        catch (IOException ex)
        {
            System.out.println("error2");
            System.exit(1);
        }
        
        System.out.println("someone successfully connected");
        
    }
}
          

if this was successfull i should see a message saying someone connected and i do not…

also no exceptions are thrown on the application side but i guess you wouldn’t expect any there…

Sorry, you are also restricted to the port you are hosting your applet on.

If your applet is loaded on a webpage on port 80, you can only connect to port 80

ok well since my applet isn’t loaded on any webpage what does that mean? im just generating the applet using an applet viewer (ie. BlueJ). i tried port 80 it still does the same thing. i dont understand why this is so complicated. does no one create internet games using applets? if so how do they test them?

I have an applet that connects to its host:

http://www.indiespot.net/files/spooky_applet.html

Sourcecode provided.

And that’s why you can connect to a sql server on a totally different port?

You can only connect to localhost if the applet is hosted on the client. Better said you can only connect to the host that hosts the applet. Only if the host that hosts the applet happens to be the localhost can you connect to it.

There is a difference between:

  • where the code is run, dictates what localhost is
  • where the code orientates from, dedicates where you can connect to.

given the code i posted above and what you just said mr.light shouldn’t my program run?

* Riven screwed up. I was confused with AJAX

An applet can connect back only to it’s host and only on ‘the web’ port 80. Signed applets can connect to any host, any port, anywhere.
Free server & signed applets - if you want fast comms you’ll need both!

Are you sure? When did this change?

Try to replace:
s = new ServerSocket(4444);

With:
int port = 4444; int backlog = 50; String host = "localhost"; new ServerSocket(port, backlog, InetAddress.getByName(host));

The first line will bind to your router/switch (probably 192.168.x.x or 10.0.0.x, not 127.0.0.1)

There’s a difference between getting a program to run and making it work. :wink:

Anyway, try actually doing something in that catch block, as that probably leads you to the solution quicker.

i tried what you suggested Riven and it still does the same thing. the code i posted used to work and now it doesn’t even though i didn’t change anything. however, i did change my computer from xp to vista… would that maybe have something to do with it?