Flex中XMLSocket与Java中Socket通信,遇到的问题是,Java中的socket能够接受到XMLSocket传输的数据,但是Flex并不能接受到从Java服务器传输的数据,从网上搜了好多资料,终于找到了原因,原来XMLSocket 接收到服务端下发的数据时,将连续放于接收缓冲区,直到接收到 "\0" 字节(字节内容为 ASCII 值 0),才认为接收完成,并调用相应的 onData 或 onXML 事件,因此,服务端若用 Java 编写,并使用标准的 String 类族,则在发送数据结尾应手动加上 "\0",同样,当XMLSocket发送数据时,也会在数据的结尾自动加上"\0"。
我现在将我做的一个简单的XMLSocket通信的例子贴出来跟大家共享一下,如果有什么不足的地方,还望大家批评指出,毕竟我也是flex初学者,呵呵!
Java应用程序代码:
view plaincopy to clipboardprint?
public class XMLServerSocket {
private ServerSocket serverSocket;
private Socket socketin;
private BufferedReader br;
private PrintStream ps;
public static void main(String[] args) {
new XMLServerSocket();
}
private XMLServerSocket() {
try {
serverSocket = new ServerSocket(8800);
socketin = serverSocket.accept();
br = new BufferedReader(new InputStreamReader(socketin
.getInputStream()));
ps = new PrintStream(socketin.getOutputStream());
ps.println("Input EXIT for end\0");
ps.flush();
out("Input EXIT for end");
boolean done = false;
while (!done) {
String str = br.readLine();
if (str == null || (str.length() == 1 && str.charAt(0) == '\0')) {
done = true;
} else {
char first = str.charAt(0);
char tail = str.charAt(str.length() - 1);
if (first == '\0') {
str = str.substring(1);
}
if (tail == '\0') {
str = str.substring(0, str.length() - 2);
}
out(str);
ps.println(str + "\0");
if (str.trim().equals("EXIT")) {
done = true;
br.close();
}
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private void out(String str) {
System.out.println(str);
}
}
public class XMLServerSocket {
private ServerSocket serverSocket;
private Socket socketin;
private BufferedReader br;
private PrintStream ps;
public static void main(String[] args) {
new XMLServerSocket();
}
private XMLServerSocket() {
try {
serverSocket = new ServerSocket(8800);
socketin = serverSocket.accept();
br = new BufferedReader(new InputStreamReader(socketin
.getInputStream()));
ps = new PrintStream(socketin.getOutputStream());
ps.println("Input EXIT for end\0");
ps.flush();
out("Input EXIT for end");
boolean done = false;
while (!done) {
String str = br.readLine();
if (str == null || (str.length() == 1 && str.charAt(0) == '\0')) {
done = true;
} else {
char first = str.charAt(0);
char tail = str.charAt(str.length() - 1);
if (first == '\0') {
str = str.substring(1);
}
if (tail == '\0') {
str = str.substring(0, str.length() - 2);
}
out(str);
ps.println(str + "\0");
if (str.trim().equals("EXIT")) {
done = true;
br.close();
}
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private void out(String str) {
System.out.println(str);
}
}
Flex程序代码:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<!--[CDATA[
private var xmlconnect:XMLSocket;
private function connectServer():void
{
try{
xmlconnect = new XMLSocket();
xmlconnect.connect("localhost",8800);
xmlconnect.addEventListener(DataEvent.DATA,onData);
}
catch(e:Error)
{
trace(e.message);
}
}
private function onData(event:DataEvent):void
{
content.text = event.text;
}
private function sendData():void
{
xmlconnect.send(datasend.text + "\n");//必须加上"\n",否则数据不能成功发送
}
]]-->
</mx:Script>
<mx:Text id="content" width="271" height="116" horizontalCenter="0" verticalCenter="0" />
<mx:TextInput id="datasend" horizontalCenter="0" verticalCenter="95"/>
<mx:Button label="发送" horizontalCenter="0" verticalCenter="121" click="sendData()"/>
<mx:Button label="连接" horizontalCenter="0" verticalCenter="-103" click="connectServer()"/>
</mx:Application>