gpt4 book ai didi

flash - 柔性 socket 连接

转载 作者:行者123 更新时间:2023-12-03 12:01:26 24 4
gpt4 key购买 nike

我有一个Groovy服务器和Flex客户端。当我尝试从Flex连接到Groovy应用程序时,我显示<policy-file-request/>消息。然后,我发送一些策略XML响应,并尝试将某些内容发送到服务器。但是我没有得到Groovy方面的帮助,反而一无所获。几秒钟后,我在Flex应用程序端收到了SecurityException

Groovy服务器代码:

import java.util.Scanner

class server
{
public static handleConnection(client)
{
PrintWriter socketwriter = new PrintWriter(client.getOutputStream())

Scanner socketreader = new Scanner(client.getInputStream())

while (socketreader.hasNext())
{
String s = socketreader.nextLine()

println s

if (s =~ /<policy-file-request\s*\/>/)
{

socketwriter.print("<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><site-control permitted-cross-domain-policies=\"master-only\" /><allow-access-from domain=\"*\" to-ports=\"2900-2910\" /></cross-domain-policy>\n\0\n")

socketwriter.flush()
socketwriter.close()
client.close()

println "Policy File sent."

return
}
}

socketwriter.print("Connection: Done.\n")
socketwriter.flush()

socketwriter.close()
socketreader.close()
}

static main(args)
{
ServerSocket serversocket

try
{
serversocket = new ServerSocket(2900)

println "Server is up and running"

while (true)
{
Socket client = serversocket.accept()

Thread.start {
server.handleConnection(client)
}
}
} catch (e)
{
e.printStackTrace()
} finally
{
serversocket.close()
}
}
}

Flex客户端代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import flash.net.Socket;
import flash.events.*;
import mx.controls.Alert;

private var socket : Socket;

private function connect() : void
{
socket = new Socket();

socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, ServerSecurityError);

socket.connect("localhost", 2900);

Alert.show("yay!");
}

private function ServerSecurityError(evt : Event) : void
{
Alert.show("security error")
}

private function onConnect(evt : Event) : void
{
Alert.show("Connected to server!");
}

private function onSocketData(evt : ProgressEvent) : void
{
var data : String = socket.readUTFBytes(socket.bytesAvailable);
Alert.show("Read: " + data);
}

private function send() : void
{
var msg : String = "Hello, World!";
socket.writeUTFBytes(msg);
socket.flush();
Alert.show("Sent: " + msg);
}
]]>
</mx:Script>

<mx:Button label="connect" click="connect()" x="10" y="10" />
<mx:Button label="send something" click="send()" x="10" y="40" />
</mx:Application>

因此,问题是:我应该如何连接才能在服务器和客户端之间获得稳定的通信?

最佳答案

出于以下原因(从文档中)出现SecurityError:

  • 本地不受信任的SWF文件无法与Internet通信。你
    可以通过将文件重新分类为
    本地联网或受信任。
  • 您无法指定套接字端口
    高于65535。
  • 在包含SWF内容的HTML页面中,
    对象和embed标签的allowNetworking参数设置为
    “没有”。

  • 我们正在运行类似的设置(使用纯AS3前端),并且在调用 connect()之前,我调用了:
    Security.loadPolicyFile( "xmlsocket://" + this.m_host + ":" + this.m_policyFilePort );
    其中 m_host可以是“localhost”之类的,而 m_policyFilePort是您要为其提供服务的端口。您可以在 hereat the Socket files上找到更多信息

    关于flash - 柔性 socket 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8637536/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com