简体   繁体   English

Java <-> Flash套接字-策略文件问题

[英]Java <-> Flash Socket - Policy File Issue

I know there are many people who already asked this Question, but in all the threads I read I couldn't find 1 solution for my problem (even if others had the same one, it didn't work for me). 我知道有很多人已经问过这个问题,但是在我读过的所有主题中,我都找不到一种解决方案(即使其他人有相同的解决方案,也对我不起作用)。

As the Title says, I'm trying to connect from a Flash/SWF-Application to a small Java server I wrote via Sockets. 如标题所述,我正在尝试从Flash / SWF应用程序连接到我通过套接字编写的小型Java服务器。 It works fine offline (on the same machine), but as soon as I put the .swf on a Webspace and open it from there, Flash requests the Policy file from the server. 它可以在脱机(在同一台计算机上)上正常运行,但是只要将.swf放到Webspace上并从那里打开它,Flash就会从服务器请求Policy文件。 There's nothing bad with that, but my problem is that Flash disconnects after (hopefully) getting the policy-file but doesn't reconnect again. 这样做没有什么不好,但是我的问题是(希望)获取了策略文件后,Flash断开了连接,但不再重新连接。

My server always opens a new Thread when a client connects, but that's not where the trouble is made, as I already tried it without opening a new Thread. 当客户端连接时,我的服务器总是打开一个新线程,但这并不是麻烦所在,因为我已经尝试过不打开新线程。

Here's my code: 这是我的代码:

while (true) {
    connection = providerSocket.accept();
    System.out.println("Incoming connection from " + 
        connection.getInetAddress().getHostName());
    BufferedReader in = new BufferedReader(
        new InputStreamReader(connection.getInputStream()));
    String request = in.readLine();

    if (request != null && request.contains("<policy-file-request/>")) {
        System.out.println("Authorization request.");
        PrintStream out = new PrintStream(connection.getOutputStream(), true);
        out.println("<?xml version=\"1.0\"?><cross-domain-policy><!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\"><allow-access-from domain=\"*\" to-ports=\"3002\" /></cross-domain-policy>\u0000");
        out.flush();
        System.out.println("AuthData sent.");
        connection.close();
        System.out.println("Authorization complete.");
        connection = providerSocket.accept();
        System.out.println("TEST");
        RequestProcessor c = new RequestProcessor(connection, connectionCounter++);
        Thread t = new Thread(c);
        t.start();

    } else {
        RequestProcessor c = new RequestProcessor(connection, connectionCounter++);
        Thread t = new Thread(c);
        t.start();
    }
}

You will surely notice that I am using "\" at the end instead of "\\0", but don't worry, I also tested that case, didn't change anything. 您肯定会注意到,我在结尾处使用的是“ \\ u0000”而不是“ \\ 0”,但是请放心,我也测试了这种情况,没有进行任何更改。 :/ :/

I dont even reach the "TEST"-Ouput, because I don't get a new connection. 我什至没有到达“ TEST” -Ouput,因为我没有建立新的连接。 And if I don't close the connection myself, flash automatically disconnects me. 如果我自己没有关闭连接,闪光灯会自动断开连接。

The last thing I tried was just sending the xml without any request (right at the beginning, after the connection is established). 我尝试的最后一件事就是不发送任何请求就发送xml(在建立连接后,刚开始时就这样)。 I got a "recv failed" - Error for that. 我收到“ recv失败”的消息-错误。

PS: RequestProcessor is my new Thread, in which I would process the Strings/Commands sent from my.swf-File... PS:RequestProcessor是我的新线程,在其中我将处理从my.swf-File发送的字符串/命令...

Thanks for helping me! 感谢您的帮助! :) :)

I had this problem before, you can not just use in.readLine() to get the policy file request string, because there're zero character. 之前我遇到过这个问题,您不能只使用in.readLine()来获取策略文件请求字符串,因为其中的字符为零。 To make sure you read the whole policy file request: 为确保您阅读了整个策略文件请求,请执行以下操作:

private String read(BufferedReader in) throws IOException {
    StringBuilder builder = new StringBuilder();
    int codePoint;
    boolean zeroByteRead = false;

    System.out.println("Reading...");
    do {
        codePoint = in.read();
        if (codePoint == -1) {
            return null;
        }
        if (codePoint == 0) {
            zeroByteRead = true;
        } else {
            builder.appendCodePoint(codePoint);
        }
    } while (!zeroByteRead);
    return builder.toString();
}

In the calling method: 在调用方法中:

BufferedReader in = new BufferedReader(new InputStreamReader(
                    clientSocket.getInputStream()));

String inputLine;

while ((inputLine = read(in)) != null) {
    System.out.println("Receive from client: " + inputLine);
    if ("<policy-file-request/>".equals(inputLine)) {
        // Serve policy file, like the one in your question.
        out.println(buildPolicy() +"\u0000");
    } else {
        // Do your job.
    }
}

You can find the policy file project in java which can be downloaded. 您可以在java中找到可以下载的策略文件项目 I myself thank to the guys over there. 我自己感谢那边的家伙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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