Confident
Confident

Reputation: 321

Getting Java Sockets Connecting

I have a problem that started occuring after I updated Java (I think) When I try and connect to my Cpp server (that seems to work just fine) I get an error message I can't find any help with.

Below are the error messages

java.security.AccessControlException: access denied
("java.net.SocketPermission" "127.0.0.1:4000" "connect,resolve")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
    at java.security.AccessController.checkPermission(AccessController.java:555)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.SecurityManager.checkConnect(SecurityManager.java:1051)
    at java.net.Socket.connect(Socket.java:574)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at jclientbare.init(jclientbare.java:27)
    at sun.applet.AppletPanel.run(AppletPanel.java:434)
    at java.lang.Thread.run(Thread.java:722)

The Java source code follows

import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class jclientbare extends Applet {


    static BufferedReader in;
    static PrintStream out;

    public void init()  {

         try    {
                System.out.println("Test NN");
            Socket socket = new Socket( "localhost", 4000 );
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintStream( socket.getOutputStream(), true);
         }

         catch (UnknownHostException e) {

        JOptionPane.showMessageDialog(null, "Unknown Host..");
            System.out.println("Unknown host: kq6py");

           } catch  (IOException e) {

        JOptionPane.showMessageDialog(null, "NO IO.");
            System.out.println("No I/O");
         }
     }

}

The Java applet doesn't register a connection it just bombs with the error message. Any help would be appreciated! I think it may be a issue with the java.policy but I don't know exactly how to fix it.

Uhh, how do you do a stack trace?

Upvotes: 0

Views: 944

Answers (3)

Confident
Confident

Reputation: 321

Aha! I learned you cannot run a applet on the same source that sent you the class files. After installing Lighttpd I can get to my applet with http://localhost/index.htm. Hope this will help others!

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168845

System.exit(1);

Even a trusted applet cannot call System.exit(int). An applet with no security manager should not call for the VM to end. It is like the guest burning down the guest house.

It would be better to do something like:

URL crash = new URL(getCodeBase(), "crash.html");
getAppletContext().showDocument(crash);

And ignore the stuff about policy files. They do not solve anything for real world (wild web) deployment.

Upvotes: 2

sandyiscool
sandyiscool

Reputation: 561

Try this to give permissions:

grant 
{
  permission java.net.SocketPermission 
  "127.0.0.1:4000", "connect,resolve";
};

For detailed information about granting permissions, you can check the following link : http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html

The article explains nicely where the policy file(s) are located and how to run an application with specific policy file.

The following link details where to find the policy files and the order in which the policy files are loaded : http://download.java.net/jdk8/docs/technotes/guides/security/PolicyFiles.html#DefaultLocs

As far as I understand, if you are in windows, then you can place a file '.java.policy' in your 'my documents' folder. When you run an applet in your browser, it will find this policy file as the user policy file.

Upvotes: 2

Related Questions