简体   繁体   中英

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).

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.


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:

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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