Flex XMLSocket

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> 

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值