Flex - XMLSocket 截断 XML 的最终结束标记 - 为什么?

发布于 2024-08-24 06:31:43 字数 3382 浏览 6 评论 0原文

我有一个项目尝试使用 XMLSocket 将 XML 发送到在另一端监听它的服务器。

应用程序文件是:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
        <mx:Script>
                <![CDATA[
                        import MyConnection;

                        [Bindable]
                        public var conn:MyConnection = new MyConnection(33333);
                ]]>
        </mx:Script>
        <mx:VBox>
                <mx:Button label="Click me" buttonDown="conn.sendXml()" />
        </mx:VBox>
</mx:Application>

MyConnection.as 是:

package
{
 import flash.errors.*;
 import flash.events.*;
 import flash.net.XMLSocket;

 public class MyConnection  {

  private var hostName:String = "localhost";
        private var port:uint = 33333;
        private var socket:XMLSocket;
        private var xmlData:XML;


  public function MyConnection(port:int) {
   super();
   this.port = port;
   socket = new XMLSocket();
   configureListeners(socket);
  }  


  /**
   * @throws IOError 
   */
  public function sendXml():void {
   xmlData =
   <body>
    <action>Hello</action>
    <name>Kittie</name>
   </body>

   socket.connect(hostName, port);
  }

  /**
   * @param dispatcher IEventDispatcher
   */
  private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.CLOSE, closeHandler);
            dispatcher.addEventListener(Event.CONNECT, connectHandler);
            dispatcher.addEventListener(DataEvent.DATA, dataHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        }

        private function closeHandler(event:Event):void {
            trace("closeHandler: " + event);
        }

        private function connectHandler(event:Event):void {
            trace("connectHandler: " + event);
            socket.send(xmlData);
   socket.close();
   xmlData = null;
        }

        private function dataHandler(event:DataEvent):void {
            trace("dataHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }
 }
}

正如您可能看到的,这与语言参考中的 XMLSocket 示例非常相似。

但是,嗅探服务器接收到的数据时,我得到了一个没有结束标记的截断 XML

Got connection from 127.0.0.1
<body>
  <action>Hello</action>
  <name>Kittie</name>
127.0.0.1 disconnected

,并且结束标记将出现在下一次数据发送中,即

Got connection from 127.0.0.1
</body><body>
  <action>Hello</action>
  <name>Kittie</name>
127.0.0.1 disconnected

您知道为什么会发生这种情况吗?有什么建议吗?

我必须在每个请求上打开和关闭套接字,但即使为了测试而尝试不这样做也无济于事,

谢谢!

卡纳夫

I have a project which tries to send an XML using XMLSocket to a server listening to it on the other side.

The application file is:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
        <mx:Script>
                <![CDATA[
                        import MyConnection;

                        [Bindable]
                        public var conn:MyConnection = new MyConnection(33333);
                ]]>
        </mx:Script>
        <mx:VBox>
                <mx:Button label="Click me" buttonDown="conn.sendXml()" />
        </mx:VBox>
</mx:Application>

And MyConnection.as is:

package
{
 import flash.errors.*;
 import flash.events.*;
 import flash.net.XMLSocket;

 public class MyConnection  {

  private var hostName:String = "localhost";
        private var port:uint = 33333;
        private var socket:XMLSocket;
        private var xmlData:XML;


  public function MyConnection(port:int) {
   super();
   this.port = port;
   socket = new XMLSocket();
   configureListeners(socket);
  }  


  /**
   * @throws IOError 
   */
  public function sendXml():void {
   xmlData =
   <body>
    <action>Hello</action>
    <name>Kittie</name>
   </body>

   socket.connect(hostName, port);
  }

  /**
   * @param dispatcher IEventDispatcher
   */
  private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.CLOSE, closeHandler);
            dispatcher.addEventListener(Event.CONNECT, connectHandler);
            dispatcher.addEventListener(DataEvent.DATA, dataHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        }

        private function closeHandler(event:Event):void {
            trace("closeHandler: " + event);
        }

        private function connectHandler(event:Event):void {
            trace("connectHandler: " + event);
            socket.send(xmlData);
   socket.close();
   xmlData = null;
        }

        private function dataHandler(event:DataEvent):void {
            trace("dataHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }
 }
}

As you can probably see, this is very similar to the XMLSocket example in the language reference.

However, sniffing on the data received by the server, I get a truncated XML without the closing tag

Got connection from 127.0.0.1
<body>
  <action>Hello</action>
  <name>Kittie</name>
127.0.0.1 disconnected

And the closing tag will appear on the next data sending, i.e.

Got connection from 127.0.0.1
</body><body>
  <action>Hello</action>
  <name>Kittie</name>
127.0.0.1 disconnected

Any ideas why this is happening? Any suggestions?

I have to open and close the socket on each request, but even trying not to do that for the sake of testing didn't help

Thanks!

karnaf

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

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

发布评论

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

评论(1

寻梦旅人 2024-08-31 06:31:43

在您的具体情况下,我可能是错的,但一般来说,使用套接字连接时,您的服务器应该设置为接收可能被拆分的数据包。例如,如果您的数据比数据包可以容纳的长度长,那么下一个数据包将是上一个交换的延续。

在 Wireshark 中查看原始套接字时,这一点最为明显,您可以看到 TCP 连续数据包。

我还注意到您正在使用 E4X 来定义 XML,您可能希望正确结束定义:

xmlData = <body>
 <action>Hello</action>
 <name>Kittie</name>
</body>;

并使用 toXMLString 函数调用它:

socket.send(xmlData.toXMLString());

I may be wrong in your individual case, but working with Socket connections in general, your server should be set up to recieve packets that may be split. For example if your data is longer than the packet can hold, then the next packet will be a continuation of the previous exchange.

This is most obviously seen when viewing raw sockets in Wireshark, you are able to see TCP continuation packets.

I also notice you're using E4X to define your XML, you may want to properly end your definition:

xmlData = <body>
 <action>Hello</action>
 <name>Kittie</name>
</body>;

And call it using the toXMLString function:

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