Flex 与Python之间Socket通讯

 查阅Adobe提供的API文档可以了解到,XMLSocket提供了四个公开方法: 
    1、XMLSocket(host:String=null,port:int=0)--创建一个新的XMLSocket对象。 
    2、close():void--关闭一个XMLSocket。 
    3、connect(host:String,port:int):void--连接到指定的TCP端口。 
    4、send(object:*):void--将数据发送到连接服务端。

客户端Flex代码:

  <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
       creationComplete="init()">
    <mx:Script>
        <![CDATA[
            private var custSocket:Socket;
            [Bindable] private var response:String = "";
              private function init():void 
             {
               custSocket = new Socket("localhost", 21567);
               configureListeners();
             }

             private function onClick(evt:Event):void 
             {
               sendRequest();
             }

             private function configureListeners():void
            {
              custSocket.addEventListener(Event.CLOSE, closeHandler);
               custSocket.addEventListener(Event.CONNECT, connectHandler);
               custSocket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
               custSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
              custSocket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
             }

             private function writeln(str:String):void 
             {
               str += "\n";
               try {
                   custSocket.writeUTFBytes(str);
               }
               catch(e:IOError) {
                   trace(e);
              }
             }

             private function sendRequest():void 
             {
              trace("sendRequest");
               writeln(inTxt.text);
              custSocket.flush();
             }

             private function readResponse():void 
             {
               var str:String = custSocket.readUTFBytes(custSocket.bytesAvailable);
               response += str;
             }

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

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

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

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

             private function socketDataHandler(event:ProgressEvent):void
            {
               trace("socketDataHandler: " + event);
               readResponse();
             }

         ]]>
   </mx:Script>
       <mx:TextArea text="{response}" id="outTxt"
           height="126" width="283" fontSize="12"/>
       <mx:HBox verticalAlign="bottom" width="282" height="40">
           <mx:TextArea id="inTxt" width="100%" height="100%" fontSize="12"/>
           <mx:Button label="发送"  fontSize="12" click="onClick(event)"/>
       </mx:HBox>
    </mx:Application>


 

服务器端Python代码:

    #!/usr/bin/env python
    #coding=utf-8
    from socket import *
    from time import ctime
      HOST=‘localhost’
       PORT=21567
      BUFSIZ=4096
      ADDR=(HOST, PORT)
      tcpSerSock = socket(AF_INET, SOCK_STREAM)
    tcpSerSock.bind(ADDR)
    tcpSerSock.listen(5)
     while True:
       print ‘waiting for connection…’
        tcpCliSock, addr = tcpSerSock.accept()
       print ‘…connected from:’, addr
          while True:
            data = tcpCliSock.recv(BUFSIZ)
            if not data:
                break
            tcpCliSock.send(‘[%s] %s’ % (ctime(), data))
               tcpCliSock.close()
   tcpSerSock.close()


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值