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>