加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex 与Python之间Socket通讯

发布时间:2020-12-15 00:59:31 所属栏目:百科 来源:网络整理
导读:?查阅Adobe提供的API文档可以了解到,XMLSocket提供了四个公开方法:? ??? 1、XMLSocket(host:String=null,port:int=0)--创建一个新的XMLSocket对象。? ??? 2、close():void--关闭一个XMLSocket。? ??? 3、connect(host:String,port:int):void--连接到指定的T

?查阅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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读