Sign in to follow this  

[java] Java - Testing Socket Communications Fails

This topic is 2567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I am writing a Java applet that communicates with a Java application that will be running on a server. The server will also the same server that serves the applet to users, so there should be no issue with opening a socket connection back to the server.

I am having problems testing my code, however. It works from within the Eclise IDE, but fails when I attempt to run the compiled code on my computer for testing purposes.

That is, the following method works:

-Start Server Application in Eclipse on local computer
-Start Applet in Eclipse (another instance of the program) on same local computer. Applet runs just fine for initial functions (not related to socket communication).
-Applet opens socket connection to server application ("localhost") and sends data just fine. Server application (running from within Eclipse) receives the data and handles it just fine as well.

And the following method does not work:

-Start Server Application compiled and exported from Eclipse (into a JAR file) on local computer
-Start compiled applet in Firefox browser on same local computer. Applet runs just fine for initial functions (not related to socket communication).
-Applet attempts to open socket connection to server application ("localhost"), but fails to do so successfully.

I opened up the Java console and saw this in the output:

"Thread-11" java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:4444 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 sun.plugin2.applet.Applet2SecurityManager.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 StopItPanel.Update_Send_Data(StopItPanel.java:625)
at StopItPanel.Update(StopItPanel.java:395)
at StopItPanel.run(StopItPanel.java:236)
at java.lang.Thread.run(Unknown Source)"

My code is very simple. The applet attempts to open a socket connection thusly:


private static String Server_Address = "localhost";
private static int Server_Port = 4444;
...
Socket socket = new Socket(Server_Address, Server_Port);



And my server application's relevant code is as follows:


private static int Server_Port = 4444;
...
try
{
// Start the server and open a port
server = new ServerSocket(Server_Port);
}
catch (IOException e)
{
// Could not start listening to the port as a server
System.out.println("Could not listen on port " + Server_Port);
System.exit(-1);
}

...

server.accept();



How can I test my code outside of Eclipse, and on my local computer?

Thank you.

Share this post


Link to post
Share on other sites
Also, running the server application through Eclipse while running the compiled applet through Firefox fails. However, it works if I run the applet in Eclipse while running the compiled server application from my desktop.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gauvir_Mucca
Also, running the server application through Eclipse while running the compiled applet through Firefox fails. However, it works if I run the applet in Eclipse while running the compiled server application from my desktop.


Read this.

http://stackoverflow.com/questions/850497/socket-connection-to-originating-server-of-an-unsigned-java-applet

There has been a change, untrusted applets are no longer allowed to make localhost connections for security reasons.

Loading the applet from a webserver(using the LAN ip 192.168.0.x should still work even if its on the same machine), if that doesn't help you could try signing your applet

http://download.oracle.com/javase/1.4.2/docs/guide/plugin/developer_guide/rsa_signing.html

Share this post


Link to post
Share on other sites
Quote:
Original post by Gauvir_Mucca
Thanks, I guess that explains it.

Out of curiosity, what does Eclipse do that lets it work?


I'm not sure, i guess the VM assumes applets that run outside of the browser are safe since those are launched explicitly, you can change the default security policy for your own machine by editing or adding policy files (So its also possible that Eclipse or the Applet viewer uses a different default policy than the browser plugin)

you can launch an applet using your own policy file by doing:
appletviewer -J-Djava.security. policy=mypolicy applet.html

Its likely that Eclipse does something similar.

Share this post


Link to post
Share on other sites

This topic is 2567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this