如何从flex向java套接字服务器发送AMF格式的数据?

发布于 2024-11-08 13:16:00 字数 1393 浏览 0 评论 0原文

我想知道如何使用 AMF 格式将数据从我的 Flex AIR 项目发送到用 Java 编写的套接字。使用 writeUTFBytes() 方法发送数据时,我收到 CorruptedStreamException 。有人遇到过类似的问题吗?另外,如果我仅使用 LCDS,是否可以使用 AMF?

private SimpleServer(int port)
{
    System.out.println(">> Starting SimpleServer on port " + port);
    try
    {
        socket      = new ServerSocket(port);
        incoming    = socket.accept();
        objectInputStream   = new ObjectInputStream(incoming.getInputStream());
        objectOutputStream = new ObjectOutputStream(incoming.getOutputStream());
        boolean done = false;
        while (!done)
        {
            Object obj = objectInputStream.readObject();
            System.out.println( obj.toString() );
            if(obj == null)
            {
                done = true;
                incoming.close();
            }
        }
    }
    catch (Exception e)
    {
        System.out.println(e);
    }
}

我的 as3 向服务器发送数据的函数是

        private function onSendClick():void
        {
            var host:String = "10.87.118.8";
            var port:int = 9090;

            var socket:Socket = new Socket();

            trace("Connect");
            socket.connect(host, port);

            trace("write");
            socket.writeUTFBytes("HelloSocket");

            trace("flush");
            socket.flush();
        }

i want to know how to send data using the AMF format from my flex AIR project to a socket written in Java. I am getting CorruptedStreamException when sending data using writeUTFBytes() methods. Has anyone experienced similar problems? Also can AMF be used only if i am using LCDS only?

private SimpleServer(int port)
{
    System.out.println(">> Starting SimpleServer on port " + port);
    try
    {
        socket      = new ServerSocket(port);
        incoming    = socket.accept();
        objectInputStream   = new ObjectInputStream(incoming.getInputStream());
        objectOutputStream = new ObjectOutputStream(incoming.getOutputStream());
        boolean done = false;
        while (!done)
        {
            Object obj = objectInputStream.readObject();
            System.out.println( obj.toString() );
            if(obj == null)
            {
                done = true;
                incoming.close();
            }
        }
    }
    catch (Exception e)
    {
        System.out.println(e);
    }
}

And my as3 function to send data to the server is

        private function onSendClick():void
        {
            var host:String = "10.87.118.8";
            var port:int = 9090;

            var socket:Socket = new Socket();

            trace("Connect");
            socket.connect(host, port);

            trace("write");
            socket.writeUTFBytes("HelloSocket");

            trace("flush");
            socket.flush();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

银河中√捞星星 2024-11-15 13:16:00

AMF 代表操作消息格式。

它是定义如何在 ActionScript 客户端和外部系统之间传输数据的规范。

因此,许多服务器端技术将 AMF 纳入其软件包中。
例如 BlazeDS、GraniteDS、pyAMF、amfphp...

因此,为了回答您的问题,AMF 也不能在 LCDS 之外使用。
它只是一个“信封”,您可以用它来发送消息(=数据)。

它甚至应该与套接字一起使用。
我相信有一个名为 merapi 的开源库就使用了这一原理。

干杯

AMF stands for Action Message Format.

It is a specification which defines how to transfer data between an ActionScript client and external system.

Therefore, many server side technologies incorporate AMF into their packages.
For example BlazeDS, GraniteDS, pyAMF, amfphp, ...

Hence, to answer your question, no AMF can also be used outside of LCDS.
It is merely an "envelope" you can use to send your message (=data) in.

It should even work with sockets.
I believe there is an open source library called merapi that uses this principle.

Cheers

短暂陪伴 2024-11-15 13:16:00

现在这是一个老问题了,但是由于在 ActionScript 端您正在使用

socket.writeUTFBytes("HelloSocket");

Java 端,因此将其更改为这样,它将在没有 AMF 的情况下工作:

BufferedReader in = new BufferedReader (new InputStreamReader((clientSocket.getInputStream())));
String line = "";               
while( (line = in.readLine()) != null) {
    processMessage(line);
}

This is an old question now, but since, on the ActionScript side you are using

socket.writeUTFBytes("HelloSocket");

On the Java side, change it to this and it will work without AMF :

BufferedReader in = new BufferedReader (new InputStreamReader((clientSocket.getInputStream())));
String line = "";               
while( (line = in.readLine()) != null) {
    processMessage(line);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文