`
ggsonic
  • 浏览: 259673 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

flex socket python

阅读更多
#coding=utf-8
from socket import *
from time import ctime
 
HOST='localhost'
PORT=5000
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()

<?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", 5000);
				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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics