问题描述
我有尝试使用的XMLSocket到服务器听着它的对方发送XML的项目。
I have a project which tries to send an XML using XMLSocket to a server listening to it on the other side.
应用程序文件是:
<?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是:
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);
}
}
}
正如你可能会看到,这是非常相似的语言参考的XMLSocket例子。
As you can probably see, this is very similar to the XMLSocket example in the language reference.
然而,嗅探服务器接收到的数据,我得到一个截断XML没有结束标记
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
谢谢!
karnaf
推荐答案
我可能是错在你的个人情况,但与插座连接的一般营运,您的服务器应设置才能收到数据包可能会被分割。例如,如果你的数据超过了数据包能够成立,那么下一个数据包将延续previous交流。
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.
这是在Wireshark中查看原始套接字时,最明显的看到,你能看到的TCP数据包的延续。
This is most obviously seen when viewing raw sockets in Wireshark, you are able to see TCP continuation packets.
我还你使用E4X来定义XML注意到,您可能需要适当地结束你的定义:
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>;
和使用作用与toXMLString函数调用它:
And call it using the toXMLString function:
socket.send(xmlData.toXMLString());
这篇关于Flex的 - 的XMLSocket截断XML的最终结束标记 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!