dis_oo

flash\flex\AS\Unity3d\JS\HTML5\CSS3\AJAX
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Socket通信(客户端)

Posted on 2011-08-16 14:32  FLASH春哥  阅读(283)  评论(0)    收藏  举报

最近在找一些关于服务器上的东西,目的就是想和FLEX通信:当然就目前而言,有很多方式,下面用sokcet方式

客户端:

<code-->

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx=" http://ns.adobe.com/mxml/2009"
					   xmlns:s="library://ns.adobe.com/flex/spark"
					   xmlns:mx="library://ns.adobe.com/flex/mx"
					   width="575" height="403">
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Script>
		
		<![CDATA[
			import flash.events.ProgressEvent;
			import flash.net.Socket;
			
			import mx.events.FlexEvent;
			private var C:Socket;
			private var C2:Socket;
			private function init():void
			{
				C=new Socket();
				C.connect("192.168.1.101",int(portInput.text));
				C.addEventListener(Event.CONNECT,onCon);
				C.addEventListener(ProgressEvent.SOCKET_DATA,onPro);
			}
			private function onCon(e:Event):void
			{
				C2=e.currentTarget as Socket;
				//trace(e.currentTarget);
				//C.writeUTF("hello");
				//C.flush()
			}
			private function onPro(e:Event):void
			{
				var C3:Socket=e.currentTarget as Socket;	
				//trace (C2.readUTF());
				var S:String=C3.readUTF();
				MessageText.appendText("FormSever:"+S+"\n");
				
			}
			
			private function onsendMessage():void
			{
				C2.writeUTF(MessageInput.text);
				C2.flush();
				MessageText.appendText("FormClient:"+MessageInput.text+"\n");
			}
			
			
		]]>
	</fx:Script>
	<s:Panel x="14" y="10" width="547" height="365" title="ClientC">
		<s:Label x="10" y="10" width="126" height="16" fontWeight="bold" text="Message form server:"/>
		<s:TextArea id="MessageText" x="10" y="30" width="525"/>
		<s:TextInput id="MessageInput" x="10" y="209" width="525" height="58"/>
		<s:Label x="10" y="189" fontWeight="bold" text="SendMessage:"/>
		<s:Button id="sendbtn" x="461" y="275" label="send" click="onsendMessage()"/>
		<s:Label x="10" y="285" width="43" height="21" fontWeight="bold" text="Port:"/>
		<s:TextInput id="portInput" x="61" y="284"/>
		<s:Button id="portbtn" x="197" y="285" label="confirm" click="init()"/>
	</s:Panel>
</s:WindowedApplication>