我正在尝试使用flex worker来管理我的套接字连接,因为在接收大量数据时,它会冻结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();
}
}
我不知道出了什么问题,我做错了什么?
提前致谢。
答案 0 :(得分: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;
}
}
}