简体   繁体   English

获取Java套接字连接

[英]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. 更新Java后,我遇到了一个问题(我认为),当我尝试连接到Cpp服务器时(似乎工作得很好),我收到一条错误消息,找不到任何帮助。

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 Java源代码如下

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. Java applet不会注册连接,而只会显示错误消息。 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. 我认为java.policy可能是一个问题,但我不知道该如何解决。

Uhh, how do you do a stack trace? 嗯,您如何进行堆栈跟踪?

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 有关授予权限的详细信息,您可以检查以下链接: 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 以下链接详细说明了在何处查找策略文件以及加载策略文件的顺序: 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. 据我了解,如果您在Windows中,则可以在“我的文档”文件夹中放置文件“ .java.policy”。 When you run an applet in your browser, it will find this policy file as the user policy file. 在浏览器中运行小程序时,它将找到此策略文件作为用户策略文件。

System.exit(1);

Even a trusted applet cannot call System.exit(int) . 即使是受信任的applet也无法调用System.exit(int) An applet with no security manager should not call for the VM to end. 没有安全管理器的小程序不应要求VM终止。 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. 他们无法解决现实世界(野生网络)的部署问题。

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 . 安装Lighttpd之后,我可以使用http://localhost/index.htm进入小程序。 Hope this will help others! 希望这对别人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM