# [java] Java - Testing Socket Communications Fails

## Recommended Posts

Haytil    525
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:

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)

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 on other sites
Haytil    525
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 on other sites
SimonForsman    7642
Quote:
 Original post by Gauvir_MuccaAlso, 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.

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

##### Share on other sites
Haytil    525
Thanks, I guess that explains it.

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

##### Share on other sites
SimonForsman    7642
Quote:
 Original post by Gauvir_MuccaThanks, 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.