Flex与Java Socket之间的通信

发布于 2024-11-08 06:54:33 字数 1445 浏览 0 评论 0原文

我有一个 Flex AIR 应用程序,我正在尝试从 Java 服务器获取数据。有人可以告诉我这种方法是否有效吗?

在Java中启动一个ServerSocket并等待连接。
使用 Flex 连接到同一端口。
从 Java 服务器向套接字写入一些数据。
在Flex端读取数据并进行处理。

-- 更新 : 我想我能够让它工作,但我在 Flex 端读回的数据似乎是空的。

我的服务器代码是:

        socket  = new ServerSocket(port);
        client  = socket.accept();

        InetAddress address = client.getInetAddress();           

        BufferedReader in = new BufferedReader( new InputStreamReader(
                client.getInputStream()));
        PrintWriter out = new PrintWriter(client.getOutputStream());            

        out.println("hi");
        out.flush();

我在 Flex 中的代码是

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

            var socket:Socket = new Socket();
            socket.endian = Endian.BIG_ENDIAN;
            socket.addEventListener(Event.CONNECT, onConnect);
            socket.connect(host, port);
        }

        private function onConnect(event:Event):void
        {
            trace(" Connected to server socket ");
            var socket:Socket = Socket(event.target);
            var obj:String = socket.readUTFBytes( socket.bytesAvailable);
            trace(obj);
        }

我当前的问题是 socket.bytesAvailable 变为 0,因此 obj 出现为“”。那么如何在Flex中使用Socket读取服务器发送的数据呢?

I have a Flex AIR app, and i am trying to get data from a Java Server. Can some one tell me if this approach will work?

Start a ServerSocket in Java and wait for a connection.
Use Flex to connect to the same port.
Write some data to the socket from Java Server.
Read the data at the Flex end and process it.

--
Update :
I think i am able to get this working, but the data that i read back at Flex end seems to be empty.

My server code is :

        socket  = new ServerSocket(port);
        client  = socket.accept();

        InetAddress address = client.getInetAddress();           

        BufferedReader in = new BufferedReader( new InputStreamReader(
                client.getInputStream()));
        PrintWriter out = new PrintWriter(client.getOutputStream());            

        out.println("hi");
        out.flush();

And my code in Flex is

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

            var socket:Socket = new Socket();
            socket.endian = Endian.BIG_ENDIAN;
            socket.addEventListener(Event.CONNECT, onConnect);
            socket.connect(host, port);
        }

        private function onConnect(event:Event):void
        {
            trace(" Connected to server socket ");
            var socket:Socket = Socket(event.target);
            var obj:String = socket.readUTFBytes( socket.bytesAvailable);
            trace(obj);
        }

My current problem is socket.bytesAvailable becoming 0 and therefore obj is coming up as "". So how to read data sent from the server using a Socket in Flex?

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

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

发布评论

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

评论(1

浮生面具三千个 2024-11-15 06:54:33

经过一些基本的谷歌搜索后,我发现一篇博客文章完全符合您所描述的内容:
http://arthurnn.com/ blog/2010/12/09/socket-connection-beteween-flex-air-and-java/

但是,该示例在本地主机上运行 java 应用程序,而不是像您描述的那样远程运行。
因此,在尝试连接到远程服务器套接字时,您可能会遇到一些沙箱违规。这可以通过将 crossdomain.xml 添加到 java 应用程序服务器的根目录来解决。

事实上,这篇博文是 merapi 底层工作的一个基本示例。

干杯

After some basic googling, I've found a blog post that does exactly what you are describing:
http://arthurnn.com/blog/2010/12/09/socket-connection-beteween-flex-air-and-java/

The example however runs the java app on the localhost, not remotely like you describe.
Therefore it might occur that you will encounter some sandbox violations when trying to connect to the remote server socket. This might be solved by adding a crossdomain.xml to the root of java app server.

In fact, this blog post is a rudimentary example of what merapi does under the hood.

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文