0

我正在尝试使用弹性工作者来管理我的套接字连接,因为在接收大量数据时,它会冻结 UI。

但是由于端口 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();
    }


}

我不知道出了什么问题,是我做错了什么吗?

提前致谢。

4

1 回答 1

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;
    }
}
}
于 2013-02-22T14:55:03.860 回答