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

Flex 与Python之间Socket通讯

发布时间:2020-12-15 05:16:02 所属栏目:百科 来源:网络整理
导读:?查阅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代码:

[plain] view plain copy print ?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. ??<mx:Application?xmlns:mx="http://www.adobe.com/2006/mxml"?layout="vertical"??
  3. ?????creationComplete="init()">??
  4. ??<mx:Script>??
  5. ??????<![CDATA[??
  6. ??????????private?var?custSocket:Socket;??
  7. ??????????[Bindable]?private?var?response:String?=?"";??
  8. ????????????private?function?init():void???
  9. ???????????{??
  10. ?????????????custSocket?=?new?Socket("localhost",?21567);??
  11. ?????????????configureListeners();??
  12. ???????????}??
  13. ??
  14. ???????????private?function?onClick(evt:Event):void???
  15. ???????????{??
  16. ?????????????sendRequest();??
  17. ???????????}??
  18. ??
  19. ???????????private?function?configureListeners():void??
  20. ??????????{??
  21. ????????????custSocket.addEventListener(Event.CLOSE,?closeHandler);??
  22. ?????????????custSocket.addEventListener(Event.CONNECT,?connectHandler);??
  23. ?????????????custSocket.addEventListener(IOErrorEvent.IO_ERROR,?ioErrorHandler);??
  24. ?????????????custSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,?securityErrorHandler);??
  25. ????????????custSocket.addEventListener(ProgressEvent.SOCKET_DATA,?socketDataHandler);??
  26. ???????????}??
  27. ??
  28. ???????????private?function?writeln(str:String):void???
  29. ???????????{??
  30. ?????????????str?+=?"n";??
  31. ?????????????try?{??
  32. ?????????????????custSocket.writeUTFBytes(str);??
  33. ?????????????}??
  34. ?????????????catch(e:IOError)?{??
  35. ?????????????????trace(e);??
  36. ????????????}??
  37. ???????????}??
  38. ??
  39. ???????????private?function?sendRequest():void???
  40. ???????????{??
  41. ????????????trace("sendRequest");??
  42. ?????????????writeln(inTxt.text);??
  43. ????????????custSocket.flush();??
  44. ???????????}??
  45. ??
  46. ???????????private?function?readResponse():void???
  47. ???????????{??
  48. ?????????????var?str:String?=?custSocket.readUTFBytes(custSocket.bytesAvailable);??
  49. ?????????????response?+=?str;??
  50. ???????????}??
  51. ??
  52. ???????????private?function?closeHandler(event:Event):void???
  53. ???????????{??
  54. ?????????????trace("closeHandler:?"?+?event);??
  55. ?????????????trace(response.toString());??
  56. ???????????}??
  57. ??
  58. ??????????private?function?connectHandler(event:Event):void???
  59. ??????????{??
  60. ?????????????trace("connectHandler:?"?+?event);??
  61. ??????????}??
  62. ??
  63. ??????????private?function?ioErrorHandler(event:IOErrorEvent):void???
  64. ??????????{??
  65. ?????????????trace("ioErrorHandler:?"?+?event);??
  66. ??????????}??
  67. ??
  68. ???????????private?function?securityErrorHandler(event:SecurityErrorEvent):void???
  69. ???????????{??
  70. ?????????????trace("securityErrorHandler:?"?+?event);??
  71. ???????????}??
  72. ??
  73. ???????????private?function?socketDataHandler(event:ProgressEvent):void??
  74. ??????????{??
  75. ?????????????trace("socketDataHandler:?"?+?event);??
  76. ?????????????readResponse();??
  77. ???????????}??
  78. ??
  79. ???????]]>??
  80. ?</mx:Script>??
  81. ?????<mx:TextArea?text="{response}"?id="outTxt"??
  82. ?????????height="126"?width="283"?fontSize="12"/>??
  83. ?????<mx:HBox?verticalAlign="bottom"?width="282"?height="40">??
  84. ?????????<mx:TextArea?id="inTxt"?width="100%"?height="100%"?fontSize="12"/>??
  85. ?????????<mx:Button?label="发送"??fontSize="12"?click="onClick(event)"/>??
  86. ?????</mx:HBox>??
  87. ??</mx:Application>??


?

服务器端Python代码:

[python] view plain copy print ?
  1. #!/usr/bin/env?python ??
  2. #coding=utf-8 ??
  3. from?socket?import?*??
  4. from?time?import?ctime??
  5. ??HOST=‘localhost’??
  6. ???PORT=21567??
  7. ??BUFSIZ=4096??
  8. ??ADDR=(HOST,?PORT)??
  9. ??tcpSerSock?=?socket(AF_INET,?SOCK_STREAM)??
  10. tcpSerSock.bind(ADDR)??
  11. tcpSerSock.listen(5)??
  12. ?while?True:??
  13. ???print?‘waiting?for?connection…’??
  14. ????tcpCliSock,?addr?=?tcpSerSock.accept()??
  15. ???print?‘…connected?from:’,?addr??
  16. ??????while?True:??
  17. ????????data?=?tcpCliSock.recv(BUFSIZ)??
  18. ????????if?not?data:??
  19. ????????????break??
  20. ????????tcpCliSock.send(‘[%s]?%s’?%?(ctime(),?data))??
  21. ???????????tcpCliSock.close()??
  22. cpSerSock.close()??

(编辑:李大同)

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

    推荐文章
      热点阅读