简体   繁体   English

无法为简单的Flash应用程序创建Java策略服务器

[英]Trouble creating a Java policy server for a simple Flash app

I'm trying to create a simple Flash chat application for educational purposes, but I'm stuck trying to send a policy file from my Java server to the Flash app (after several hours of googling with little luck). 我正在尝试创建一个简单的Flash聊天应用程序用于教育目的,但是我一直试图将策略文件从Java服务器发送到Flash应用程序(经过几小时的小运气之后,一直苦苦搜寻)。

The policy file request reaches the server that sends a harcoded policy xml back to the app, but the Flash app doesn't seem to react to it at all until it gives me a security sandbox error. 策略文件请求到达服务器,该服务器将经过编码的策略xml发送回应用程序,但是Flash应用程序似乎完全不响应它,除非它给我一个安全沙箱错误。


I'm loading the policy file using the following code in the client: 我正在客户端中使用以下代码加载策略文件:

Security.loadPolicyFile("xmlsocket://myhostname:" + PORT);

The server recognizes the request as "< policy-file-request/> " and responds by sending the following xml string to the client: 服务器将请求识别为“ < policy-file-request /> ”,并通过将以下xml字符串发送给客户端进行响应:

public static final String POLICY_XML =
    "<?xml version=\"1.0\"?>"
  + "<cross-domain-policy>"
  + "<allow-access-from domain=\"*\" to-ports=\"*\" />"
  + "</cross-domain-policy>";

The code used to send it looks like this: 用于发送它的代码如下所示:

try {
    _dataOut.write(PolicyServer.POLICY_XML + (char)0x00);
    _dataOut.flush();
    System.out.println("Policy sent to client: " + PolicyServer.POLICY_XML);
} catch (Exception e) {
    trace(e);
}

Did I mess something up with the xml or is there something else I might have overlooked? 我是不是把xml弄乱了,还是有可能被我忽略了?

I've seen your approach and after some time trying i wrote a working class, listening on any port you want: 我已经看到了您的方法,经过一段时间的尝试,我编写了一个工作类,侦听您想要的任何端口:

package Server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class PolicyServer {
    public static final String POLICY_XML =
            "<?xml version=\"1.0\"?>"
                    + "<cross-domain-policy>"
                    + "<allow-access-from domain=\"*\" to-ports=\"*\" />"
                    + "</cross-domain-policy>";

    public PolicyServer(){
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(843);
        } catch (IOException e) {e.printStackTrace();}
        while(true){
            try {
                final Socket client = ss.accept();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            client.setSoTimeout(10000); //clean failed connections
                            client.getOutputStream().write(PolicyServer.POLICY_XML.getBytes());
                            client.getOutputStream().write(0x00); //write required endbit
                            client.getOutputStream().flush();
                            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                            //reading two lines emties flashs buffer and magically it works!
                            in.readLine();
                            in.readLine();
                        } catch (IOException e) {
                        }
                    }
                }).start();
            } catch (Exception e) {}
        }
    }
}

尝试在策略xml的末尾添加\\n

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

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