Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Applet can't connect to own host (in Firefox)  (Read 6341 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Posted 2008-05-15 10:43:49 »

I had an applet hosted on IP address xx.xx.xx.xx, and the applet could do a
URL("http://xx.xxx.xx.xx/resource").openConnection() and do I/O without problems.


Now I have the applet hosted on domain www.abcdef.com which resolves to xx.xx.xx.xx, yet
URL("http://www.abcdef.com/resource").openConnection() works fine in MSIE, but fails in Firefox with:
1  
java.security.AccessControlException: access denied (java.net.SocketPermission xx.xx.xx.xx:80)


I think Firefox turns the domain into its IP-address... then checks whether or not "xx.xx.xx.xx" textually equals "www.abcdef.com" Huh

This is such a common case, that I'm wondering why I fail to get it right...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #1 - Posted 2008-05-15 11:03:04 »

I fail to see what the browser has to do with the runtime ?
are they using the same jre when the applet is running ?

does the ip match for both www.abcdef.com and abcdef.com ?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2008-05-15 11:38:44 »

Thanks for replying.


I fail to see what the browser has to do with the runtime ?
I fail to see it too... yet it only throws an Exception in Firefox

(it's like Opera doesn't properly load *.jar.pack.gz while you'd say that's up to the JRE as well...)



are they using the same jre when the applet is running ?
Yup, same version, not JRE process ofcourse.


does the ip match for both www.abcdef.com and abcdef.com ?
Yes. At first it wasn't, so I changed the DNS and did a "ipconfig /flushdns" followed by "ping abcdef.com" to verify the (restarted) browser would see the right IP. Anyway, it resolved to xx.xx.xx.xx anyway, which is the right IP.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2008-05-15 12:26:19 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
System.out.println(applet.getCodeBase());
--> "http://www.abcdef.com/"

String action = applet.getCodeBase() + "resource/";
URL url = new URL(action);
URLConnection con = url.openConnection();

con.setDoInput(true);
con.setUseCaches(false);

if(postData != null)
{
   con.setDoOutput(true);
   con.getOutputStream().write(postData); // this throws AccessControlException: access denied (SocketPermission xx.xx.xx.xx connect,resolve)
}

int len = con.getContentLength(); // this throws AccessControlException: access denied (SocketPermission xx.xx.xx.xx connect,resolve)
if(len == -1)
{
   // http 1.1 chunked
}
else
{
  // http normal
}


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2008-05-15 14:38:33 »

Bugger...

I have 2 systems, both are identical:
FireFox 2.0.0.14
Java Plug-in 1.6.0_05
Using JRE version 1.6.0_05 Java HotSpot(TM) Client VM


One works, one doesn't.


Write once, run anywhere, with varying results

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline brackeen

Junior Member





« Reply #5 - Posted 2008-05-15 17:57:17 »

Are you using LiveConnect? It sounds like it might be this bug:
http://bugs.sun.com/view_bug.do?bug_id=6669818
The bug existed 1.6.0_03, fixed in 6u10

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2008-05-15 18:42:42 »

Thanks for replying!

Although I'm not using LiveConnect, I found this on the bug-page you gave me:
-> http://forum.java.sun.com/thread.jspa?threadID=5230759&start=30
---> http://oueb.org/AppletFireFoxLiveconnectBug.html
-----> http://oueb.org/AppletFireFoxLiveconnectBug.java

There, the button *within* the applet successfully reads from an URLConnection in Firefox (yay!)


So I guess I'll be playing around with the provided sourcecode and see what's the diff (if any...)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2008-05-15 18:54:16 »

So........... it works now, but it does not make sense.

I simply put the given example code in Applet.start()
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
   public void start()
   {
      try
      {
         URL url = new URL( "http://www.abcdef.com/" );
         URLConnection conn = url.openConnection();
         conn.setDoOutput( false );
         BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
         String line;
         StringBuffer response = new StringBuffer();
         while( (line = br.readLine()) != null )
            response.append( line );
         br.close();
      }
      catch (Exception exc)
      {
         exc.printStackTrace();
      }
   }



After that, all my further URLConnections work....!

And if I remove that (useless) code in Applet.start(), it breaks down instantly.

Seems to me like some threading-issue. Appearantly you have to be in the thread that calls applet.start() to do the first URLConnection, after that it doesn't matter which Thread does the I/O.. Roll Eyes

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline timfoden

Junior Member


Projects: 2



« Reply #8 - Posted 2008-05-18 15:55:25 »

I've just started to see exactly the same problem.  I've got an applet (Pipe Extreme, http://games.7sun.com/applet.php?game=50) which loads some resources from the host after the applet has started.  It has worked fine for ages.  Now though, it works OK the first time firefox loads it.  But if I then clear the applet cache (x in console) and hit ctrl-F5 in firefox... the resources fail to load.... arrrrgggh!!

Clear classloader cache ... completed.
java.security.AccessControlException: access denied (java.net.SocketPermission 65.254.51.202:80 connect,resolve)
   at java.security.AccessControlContext.checkPermission(Unknown Source)
   at java.security.AccessController.checkPermission(Unknown Source)
   at java.lang.SecurityManager.checkPermission(Unknown Source)
   at java.lang.SecurityManager.checkConnect(Unknown Source)
   at java.net.Socket.connect(Unknown Source)
etc..

I noticed it as I'm doing some development at the moment to add an online highscore facility, so I'm testing the applet a lot in firefox.  Bizarrely it seems to me to have only broken recently... Is it perhaps due to firefox updating to 2.0.0.14?

Or maybe its the java runtime... I've tried jre versions 1.6.0_03 and 1.6.0_05?

BTW, Riven, your workaround above doesn't work for me.

Cheers, Tim.

Try Pipe Extreme -- can you get to the end of the pipe?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2008-05-18 16:20:44 »

Does this one work:
http://oueb.org/AppletFireFoxLiveconnectBug.html

click the button IN the applet, that one works for me.. which is enough, as I don 't care about LiveConnect

It still works after I press 'x' in the JavaConsole

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline timfoden

Junior Member


Projects: 2



« Reply #10 - Posted 2008-05-19 11:16:27 »

http://oueb.org/AppletFireFoxLiveconnectBug.html
It still works after I press 'x' in the JavaConsole

.. did you try Ctrl-F5 before clicking the java button again?

The sequence below replicates what I'm seeing in my applet:
  Click Java button.  -- This works fine.
  Click JavaScript button.  -- This produces an ACE, as expected.
  'x' in Java console window.
  Ctrl-F5 in Firefox to reload page.
  Click Java button. -- This now produces an ACE.  Close and restart Firefox completely to get it to work again.

That this started happening with my applet late yesterday afternoon is a complete mystery to me, as I had been doing the 'x', Ctrl-F5 sequence all weekend to reload my applet, and it worked fine (loading resources and high scores from the server).  Just as I got it to the state where I was doing final testing to release it, it started to fail every time (due to this bug of course).  What a pain.  Smiley

I think I'm going to have to start using IE to develop with until this bug is fixed.  doh!

Cheers, Tim.

Try Pipe Extreme -- can you get to the end of the pipe?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2008-05-19 15:20:53 »

Yes, it throws an ACE with that exact sequence.


But *without* that LiveConnect button, it works, even with 'x' and CTRL+F5

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline timfoden

Junior Member


Projects: 2



« Reply #12 - Posted 2008-05-19 17:44:41 »

Yes, it throws an ACE with that exact sequence.


But *without* that LiveConnect button, it works, even with 'x' and CTRL+F5

True.  Up till now I've been using LiveConnect in my applet to save the "local" high scores (via a cookie).  This is the only LiveConnect code used.  I guess that once I've finally got server side high scores working I could just remove the local ones.  It's a shame though, as I'd like to keep them.

I don't suppose anyone knows some way, other than using LiveConnect with Cookies, of persisting data from an unsigned applet?

Cheers, Tim.

Try Pipe Extreme -- can you get to the end of the pipe?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2008-05-19 18:05:46 »

Applet => website

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public void executeScript(String js)
   {
         try
         {
            String path = "javascript:eval(hexToStr('" + strToHex(js) + "'));";
            this.getAppletContext().showDocument(new URL(path), "_self");
         }
         catch (Exception exc)
         {
            // probably Opera
        }
   }




Website => Applet

- Website: write into Applet <param name="myProperty" value=""> using Javascript / DOM
- Applet: polling applet.getParameter("myProperty") every N ms

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2008-05-19 18:07:56 »

Just to clearify, you need to 'encode' and 'decode' the javascript you want to execute in the browser because is won't probably be a valid URL.





Here is some incredibly ineffictient (who cares) javascript for strToHex() and hexToStr()

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
function strToHex(str)
{
   var hex = "";
   for(var i=0; i<str.length; i++)
      hex += itoh(str.charCodeAt(i));  
   return hex;
}

function hexToStr(hex)
{
   var str = "";
   for(var i=0; i<hex.length; i+=2)
      str += String.fromCharCode(htoi(hex.substring(i, i+2)));
   return str;
}

function itoh_(i)
{
   return "0123456789ABCDEF".charAt(i);  
}

function itoh(i)
{
   var hi = (i >> 4) & 15;
   var lo = (i >> 0) & 15;
   return itoh_(hi)+""+itoh_(lo);
}

function htoi_(h)
{
   return "0123456789ABCDEF".indexOf(h);
}

function htoi(h)
{
   var hi = (htoi_(h.charAt(0)) & 15) << 4;
   var lo = (htoi_(h.charAt(1)) & 15) << 0;
   return hi+lo;
}



Java versions:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
   private static char[] hexTable = "0123456789ABCDEF".toCharArray();

   public static final String strToHex(String str)
   {
      try
      {
         return rawToHex(str.getBytes("utf8"));
      }
      catch(UnsupportedEncodingException exc)
      {
         return null;
      }
   }

   public static final String hexToStr(String hex)
   {
      try
      {
         return new String(hexToRaw(hex), "utf8");
      }
      catch(UnsupportedEncodingException exc)
      {
         return null;
      }
   }

   public static final String rawToHex(byte[] raw)
   {
      char[] digits = new char[raw.length * 2];
      for (int k = 0; k < raw.length; k++)
         for (int i = 0; i < 2; i++)
            digits[(k << 1) + (1 - i)] = hexTable[(hex[k] >>> (i << 2)) & 0x0F];
      return new String(digits);
   }

   public static final byte[] hexToRaw(String hex)
   {
      byte[] raw = new byte[hex.length() / 2];
      for (int i = 0; i < raw.length; i++)
      {
         char c0 = hex.charAt((i << 1) + 0);
         char c1 = hex.charAt((i << 1) + 1);

         int val = (c0 - (c0 < 'A' ? '0' : 'A')) << 4;
         raw[i] = (byte) (val | (c1 - (c1 < 'A' ? '0' : 'A')));
      }
      return raw;
   }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2008-05-19 18:21:57 »

- see previous page

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline timfoden

Junior Member


Projects: 2



« Reply #16 - Posted 2008-05-20 09:11:33 »

Applet => website
...
Website => Applet
...

That's just great, thanks Riven.  I'll give it a try tonight.

Cheers, Tim.

Try Pipe Extreme -- can you get to the end of the pipe?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2008-05-20 09:52:40 »

Keep in mind I 'manually wrote' (..) that code in the reply (not a copy/paste).

I don't have the sourcecode here right now, only the bytecode Smiley

The concept works though.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline DzzD
« Reply #18 - Posted 2008-05-21 11:29:36 »

So........... it works now, but it does not make sense.

I simply put the given example code in Applet.start()
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
   public void start()
   {
      try
      {
         URL url = new URL( "http://www.abcdef.com/" );
         URLConnection conn = url.openConnection();
         conn.setDoOutput( false );
         BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
         String line;
         StringBuffer response = new StringBuffer();
         while( (line = br.readLine()) != null )
            response.append( line );
         br.close();
      }
      catch (Exception exc)
      {
         exc.printStackTrace();
      }
   }



After that, all my further URLConnections work....!

And if I remove that (useless) code in Applet.start(), it breaks down instantly.

Seems to me like some threading-issue. Appearantly you have to be in the thread that calls applet.start() to do the first URLConnection, after that it doesn't matter which Thread does the I/O.. Roll Eyes

cool, excellent that you find a workaround to this FF bug , great!

Offline timfoden

Junior Member


Projects: 2



« Reply #19 - Posted 2008-06-09 18:03:17 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
    public void executeScript(String js)
    {
        try
        {
            String path = "javascript:eval(hexToStr('" + strToHex(js) + "'));";
            this.getAppletContext().showDocument(new URL(path), "_self");
        }
        ...
   }


I did finally get around to trying this... and it does run the javascript code as advertised.

However, it also changes the document Smiley  Is there some trick to getting the browser to run the javascript, but not change the document?  Or should the javascript just change the document back again... but then I guess the applet would re-start?  Or maybe I need to run it in a (non-visible) frame (but I'd prefer not to use frames at all)?

Hmmm, tricky.

Cheers, Tim.

Try Pipe Extreme -- can you get to the end of the pipe?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #20 - Posted 2008-06-09 20:40:06 »

You can do this:

Have an <iframe src="myEval.html" name="myEval">, with myEval.html like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
<html>
<head>
<script language="javascript">
function runEval()
{
   var ownURL = window.location.href;
   var doEvalHex = ownURL.substring(ownURL.indexOf('?doEvalHex=')+'?doEvalHex='.length);
   var doEvalStr = hexToStr(doEvalHex);

   with(window.parent) // critical
  {
       eval(doEvalStr);
   }
}
</script>
</head>
<body onLoad="runEval();">
</body>
<html>



Run with:
1  
2  
String js = "var anythingGoes = function() { setTimeout(function(){ window.alert("function in function");}, 1000)}";
this.getAppletContext().showDocument(new URL("./myEval.html?doEvalHex="+strToHex(js)), "myEval"); // not _self


I have something like that (as a workaround in Opera, as it doesn't understand the "javascript:..."-protocol). It works, but the provided code is untested.


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline timfoden

Junior Member


Projects: 2



« Reply #21 - Posted 2008-06-11 13:15:54 »

You can do this:

Have an <iframe src="myEval.html" name="myEval">, with myEval.html like this:

Ah, IFRAME... I handn't realised that such a thing existed.  I guess my aversion to FRAMEs doesn't really apply to an IFRAME.  Cool, I'll give it a try.

I have something like that (as a workaround in Opera, as it doesn't understand the "javascript:..."-protocol). It works, but the provided code is untested.

No probs about untested code.

Thanks for your help.  Cheers, Tim.

Try Pipe Extreme -- can you get to the end of the pipe?
Offline DzzD
« Reply #22 - Posted 2008-06-19 16:22:23 »

the "javascript:" protocol, what a good idea Riven !

so good than I decide to make a class for that Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
/** 
 *  A class to execute JavaScript without liveconnect
 *  @version 1.0
 *  @since 1.0
 *  @author Bruno Augier
 *
 *  Copyright Bruno Augier 2008
 */



import java.applet.*;
import java.net.URLEncoder;
import java.net.URL;
import java.net.MalformedURLException;

public class JavaScript
{
 private Applet a;
 public JavaScript(Applet a)
 {
  this.a=a;
 }
 
 public void exec(String js)
 {
  String jsURL="javascript:eval(unescape(\""+URLEncoder.encode(js.replace(" ","%20"))+"\"))";
  System.out.println(jsURL);
  try
  {
   this.a.getAppletContext().showDocument(new URL(jsURL),"_self");
  }
  catch(MalformedURLException mue)
  {
   mue.printStackTrace();
  }
 }
}


Usage in an Applet :

1  
2  
JavaScript js=new JavaScript(this);
js.exec("alert('javascript!')");

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #23 - Posted 2008-06-19 23:10:57 »

Beware it doesn't work in Opera.



Soon I'll publish all code relevant to this 'issue'.

There are so many problems with this trivial stuff that there should be a centralized solution.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!