Java <-> Flash Socket - Policy File Issue

痴心易碎 提交于 2019-12-11 15:58:54

问题


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. 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. There's nothing bad with that, but my problem is that Flash disconnects after (hopefully) getting the policy-file but doesn't reconnect again.

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 "\u0000" at the end instead of "\0", but don't worry, I also tested that case, didn't change anything. :/

I dont even reach the "TEST"-Ouput, because I don't get a new connection. 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). I got a "recv failed" - Error for that.

PS: RequestProcessor is my new Thread, in which I would process the Strings/Commands sent from my.swf-File...

Thanks for helping me! :)


回答1:


I had this problem before, you can not just use in.readLine() to get the policy file request string, because there're zero character. 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. I myself thank to the guys over there.



来源:https://stackoverflow.com/questions/8604671/java-flash-socket-policy-file-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!