2008年11月26日 星期三

flashplayer 9.0.124.0 跨網域無法存取 XMLSocket 解決

這篇文章之前已經發佈過一次(我還沒被砍掉的一百多篇中...), 現在把它補回來!
flashplayer 9.0.124.0 由於SandBox存取規則變更, 所以會先從843port開始找, 如找不到在依actionscript中指定路徑或根目錄底下尋找crossdomain.xml。
但不知為何9.0.124.0版本如果沒有置放843port服務的話, 自己並不會去尋找crossdomain.xml, 這就等於逼著大家放一個驗證policy的server在伺服器上。
這裡給大家一個java寫的範本參考, 也是我目前在線上運作的版本。

SecurityServer:
import java.net.*;
import java.io.*;

public class SecurityServer implements Runnable
{
    private ServerSocket server;
    public static String xml;

    // Constructor
    public SecurityServer()
    {
        xml = " ";
        createServerSocket(843);
        new Thread(this).start();
    }

    // Create Server
    private void createServerSocket(int port)
    {
        try {
            server = new ServerSocket(port);
            System.out.println("Server created successful...");
        } catch (IOException e) {
            System.err.println("Already had a server on " + port + " !!");
            System.exit(1);
        }
    }

    // Runnable Thread
    public void run()
    {
        while (true) {
            Socket client = null;
            try {
                client = server.accept();
                new AcceptThread(client);
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    if (client != null) {
                        client.close();
                        client = null;
                        System.out.println("Connection close");
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    System.gc();
                }
            }
        }
    }

    public static void main(String[] args)
    {
        new SecurityServer();
    }
}
AcceptThread:
import java.io.*;
import java.net.*;

public class AcceptThread implements Runnable
{

    private Socket connection;
    private BufferedReader reader;
    private BufferedWriter writer;
    private String xml;

    // Constructor
    public AcceptThread(Socket connection)
    {
        try {
            this.connection = connection;
            this.connection.setSoTimeout(5000);
            InputStreamReader input = new InputStreamReader(connection.getInputStream(), "UTF-8");
            reader = new BufferedReader(input);
            OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
            writer = new BufferedWriter(output);
            xml = SecurityServer.xml;
            new Thread(this).start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // Runnable Thread
    public void run()
    {
        try {
            StringBuilder data = new StringBuilder();
            int c = 0;
            while ((c = reader.read()) != -1) {
                if (c != '\0')
                    data.append((char) c);
                else
                    break;
            }
            String info = data.toString();
            System.out.println("Communication info: " + info);
            if (info.indexOf("policy-file-request") >= 0) {
                writer.write(xml + "\0");
                writer.flush();
                // System.out.println(xml);
                System.out.println("Client Request: " + connection.getInetAddress());
            } else {
                writer.write("Unrecognized request\0");
                writer.flush();
                System.out.println("Unrecognized request: " + connection.getInetAddress());
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                if (connection != null) {
                    connection.close();
                    connection = null;
                    System.out.println("Connection close");
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                System.gc();
            }
        }
    }
}

1 則留言:

  1. SecurityServer 第12行xml中雙引號中的雙引號實際編譯時請改為\", 因為是脫溢字元

    回覆刪除