serverSocket与socket的使用

1、使用serverSocket建立服务端:

   首先创建一个 serverSocket(一定要是个全局变量)

      private var server:ServerSocket;

   绑定到指定的本地址和端口

      server.bind( 6666, "119.138.64.241" );

   侦听绑定的IP地址和端口上的TCP连接 

      server.listen();

    当有客户端连接时(当客户端尝试连接到服务器套接字时,ServerSocket 对象调度 ServerSocketConnectEvent 对象)

      server.addEventListener(ServerSocketConnectEvent.CONNECT, onConnect);

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 3     
 4     <mx:Script>
 5         <![CDATA[
 6 
 7             private var server:ServerSocket;
 8             
 9             [Bindable]
10             private var isRunning:Boolean = false;
11 
12             protected function start(event:MouseEvent):void
13             {
14                 if( !server )
15                 {
16                     server = new ServerSocket();
17                 }
18                 if( isRunning )
19                 {
20                     server.close();
21                     server = null;
22                 }
23                 else
24                 {
25                     try
26                     {
27                         //绑定到指定的本地地址和端口
28                         server.bind( 6666, "119.138.64.241" );
29                         //侦听绑定的 IP 地址和端口上的 TCP 连接
30                         server.listen();
31                         server.addEventListener(ServerSocketConnectEvent.CONNECT, onConnect);
32                     }
33                     catch(e:*)
34                     {
35                         trace( e );
36                     }
37                 }
38                 isRunning = !isRunning;
39             }
40             
41             protected function onConnect(event:ServerSocketConnectEvent):void
42             {
43                 //trace( "connect..." );
44                 var socket:Socket = event.socket;
45                 socket.addEventListener(ProgressEvent.SOCKET_DATA, onReceive);
46                 
47                 outputTxt.text += "connect..." +"\n";
48             }
49             
50             protected function onReceive(event:ProgressEvent):void
51             {
52                 var socket:Socket = event.target as Socket;
53                 //socket.readUTF();
54                 var str:String = socket.readUTF();
55                 
56                 outputTxt.text += str + "\n";
57                 
58                 //回发已收到的消息
59                 socket.writeUTF( "Message received: " + str );
60                 socket.flush();
61             }
62             
63         ]]>
64     </mx:Script>
65     <mx:ApplicationControlBar left="10" right="10" top="10" bottom="415" horizontalAlign="right">
66         <mx:Button id="startBT" label="{isRunning?'Stop':'Start'}" click="start(event)"/>
67     </mx:ApplicationControlBar>
68     <mx:TextArea id="outputTxt" left="10" right="10" top="59" bottom="10"/>
69     
70 </mx:WindowedApplication>

2、使用socket建立客户端
    创建 scoket 对象

    private var socket:Socket;

    socket = new Socket();

   连接服务端(地址和端口要和服务端一到致)

   socket.connect( "119.138.64.241", 6666 );

   侦听socket的连接

   socket.addEventListener(Event.CONNECT, onConnect );   

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 3     
 4     <mx:Script>
 5         <![CDATA[
 6             import mx.events.FlexEvent;
 7 
 8             private var socket:Socket;
 9             protected function connectServer(event:MouseEvent):void
10             {
11                 socket = new Socket();
12                 socket.connect( "119.138.64.241", 6666 );
13                 socket.addEventListener(Event.CONNECT, onConnect );    
14                 socket.addEventListener(ProgressEvent.SOCKET_DATA, receiveServerMsg);
15             }
16             
17             protected function onConnect(event:Event):void
18             {
19                 outputTxt.text += "connect..." + "\n";
20             }
21             
22             protected function sendMsg(event:FlexEvent):void
23             {
24                 if( socket.connected )
25                 {
26                     socket.writeUTF( inputTxt.text );
27                     socket.flush();
28                 }
29             }
30             
31             /**
32              * 接收服务器回发的消息
33              * 
34              **/
35             protected function receiveServerMsg(event:ProgressEvent):void
36             {
37                 var socket:Socket = event.target as Socket;
38                 var str:String = socket.readUTF();
39                 outputTxt.text += str + "\n";
40             }
41             
42         ]]>
43     </mx:Script>
44     
45     <mx:ApplicationControlBar left="10" right="10" top="10" bottom="415" horizontalAlign="right">
46         <mx:Label text="ID"/>
47         <mx:TextInput id="idInput" text="kft"/>
48         <mx:Button id="connectBT" label="connect" click="connectServer(event)"/>
49     </mx:ApplicationControlBar>
50     <mx:TextArea id="outputTxt" left="11" right="9" top="67" bottom="40"/>
51     <mx:TextInput id="inputTxt" left="10" right="10" bottom="10" height="22" enter="sendMsg(event)"/>
52 </mx:Application>

 

posted @ 2013-12-03 14:33  actionkong  阅读(526)  评论(0编辑  收藏  举报