2013-01-02 97 views
0

我試圖使用flex工作人員來管理我的套接字連接,因爲在接收大量數據時,它凍結了用戶界面。工作人員flex loadpolicy

但是由於端口843未打開,我遇到了策略問題。所以我嘗試使用其他端口加載策略,但我仍然有安全錯誤,說應用程序試圖訪問端口843上的策略文件。

這是我在工作人員內部使用來加載策略:

Security.loadPolicyFile("xmlsocket://myServer:8888"); 

我試着服務政策,一個Java應用程序:

public static void main(String[] args) 
{ 


    String policy = "<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">"; 
    policy += "<cross-domain-policy>"; 
    policy += "<allow-access-from domain=\"*\" to-ports=\"*\"/>"; 
    policy += "</cross-domain-policy>"; 


    ServerSocket s; 
    try 
    { 
     s = new ServerSocket(8888); 



    while(true) 
    { 
     try 
     { 
      Socket cli = s.accept(); 
      BufferedReader in = new BufferedReader(new InputStreamReader(cli.getInputStream())); 
      OutputStreamWriter out = new OutputStreamWriter(cli.getOutputStream()); 

      System.out.println(in.read()); 

      out.write(policy+"\0"); 
      out.flush(); 
      cli.shutdownInput(); 
      cli.shutdownOutput(); 
      cli.close(); 
      System.out.println("Policy response sent"); 
      System.out.println(policy); 
      System.out.println("Waiting for new request"); 

     } 
     catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    } 
    } 
    catch (IOException e1) 
    { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 


} 

我不知道發生了什麼錯,是不是我做錯了?

在此先感謝。

回答

0

爲了使它工作得很好,我只是將「out」類型更改爲PrintWriter。

這裏是工作代碼:

public class FlexPolicy extends Thread { 



private ServerSocket s; 

private StringBuffer policyBuffer = new StringBuffer(); 
public static final String POLICY_REQUEST = "policy-file-request"; 

private Logger log = Logger.getLogger(FlexPolicy.class); 

public FlexPolicy() 
{ 
    String allowedPorts = MyConfig.getInstance().getValue("ports"); 
    policyBuffer.append("<?xml version=\"1.0\"?><cross-domain-policy>"); 
    policyBuffer.append("<allow-access-from domain=\"*\" to-ports=\""+allowedPorts+"\" />"); 
    policyBuffer.append("</cross-domain-policy>"); 
} 


@Override 
public void run() 
{ 
    try 
    { 
     s = new ServerSocket(Integer.parseInt(MyConfig.getInstance().getValue("socket"))); 

     while(true) 
     { 
      log.debug("Waiting for policy request"); 
      Socket cli = s.accept(); 
      BufferedReader in = new BufferedReader(new InputStreamReader(cli.getInputStream())); 
      PrintWriter out = new PrintWriter(cli.getOutputStream()); 

      String request = receive(in); 
      log.debug("received request : |"+request+"| |"+POLICY_REQUEST+"|"); 
      log.debug(request.indexOf(POLICY_REQUEST)); 

      if(request.indexOf(POLICY_REQUEST)!=-1) 
      { 
       log.debug("policy matches"); 
       sendPolicy(out); 
      } 

      try 
      { 
       cli.shutdownInput(); 
       cli.shutdownOutput(); 
       cli.close(); 
      } 
      catch (Exception e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

    } 
    catch(Exception e) 
    { 

    } 
} 


public void sendPolicy(PrintWriter out) 
{ 
    out.println(policyBuffer.toString()+"\0"); 
    out.flush(); 
    log.debug("Policy sent"); 
} 


public String receive(BufferedReader input) throws Exception 
{ 
    String requete = ""; 
    int taille = 0; 
    char[] cbuf = new char[1000]; 

    taille = input.read(cbuf, 0, cbuf.length); 
    if (taille == -1) 
    { 
     return null; 
    } 
    else 
    { 
     requete += new String(cbuf).substring(0, taille); 
     return requete; 
    } 
} 
}