Flex 与Python之间Socket通讯
2011-12-20 11:45
337 查看
查阅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代码:
服务器端Python代码:
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()
相关文章推荐
- flex与java之间socket通信的好教程推荐.
- python实现socket通讯
- Python采用socket模拟TCP通讯的实现方法
- 基于Socket编程实现平行主机之间网络通讯
- python实现socket通讯(UDP)
- android socket通讯,PC和模拟器,两个模拟器之间的通信
- flex的Socket通讯的安全沙箱的问题全解析
- unity3D中Socket链接与服务之间的通讯测试程序
- python实现socket通讯(UDP)
- C#.NET通过Socket实现平行主机之间网络通讯(含图片传输的Demo演示)
- python网络编程-使用socket实现S/C之间的tcp通信
- C++与Flex之间socket通信policy-file-request解决方案
- Flex 与 as3.0 SWF 之间的通讯
- python实现socket通讯(UDP)
- [读书笔记]python3.5实现socket通讯(UDP)
- java与C++之间进行SOCKET通讯要点简要解析
- android socket通讯,PC和模拟器,两个模拟器之间的通信
- 2.5 Linux下的Socket编程-多个客户端之间的消息通讯(实验三)
- Webfocus Flex 之间通讯
- 模拟器之间的Socket通讯