Socket与其他的通信技术最大的不同是Socket连接在数据传输完成后不会自动关闭,因此服务器可以在任何时间主动发送数据给客户端.?
Flex下Socket连接的方式有Socket和XMLSocket两种.?
读取数据使用的是异步的方式,需要监听对应的事件?
socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );?
xmlSocket.addEventListener( DataEvent.DATA, onData );?
写入数据的方式?
socket下比较常用的是writeUTFBytes(String),writeMultiByte()可以自定义编码,?
使用flush()发送数据?
xmlScoket下 .send(String)?
连接Socket的方式?
socket.connect( "localhost", 2900 ); //代表地址和端口?
如果要跨域连接服务器,需要加载策略文件?
Security.loadPolicyFile("url/crossdomain.xml");?
例子使用Java建立Socket服务器,然后使用Flex连接,并且进行数据交互?
- package?socket;??
- ??
- import?java.io.*;??
- import?java.net.*;??
- import?java.util.Scanner;??
- ??
- public?class?SocketService?{??
- ????public?static?void?main(String[]?args)?{??
- ????????try?{??
- ????????????System.out.println("启动ServerSocket..");??
- ????????????int?i=1;??
- ????????????ServerSocket?s=new?ServerSocket(8189);??
- ????????????while?(true)?{??
- ????????????????Socket?incoming=s.accept();??
- ????????????????System.out.println("新用户"+i);??
- ????????????????Runnable?r=new?ThreadEcho(incoming);??
- ????????????????Thread?t=new?Thread(r);??
- ????????????????t.start();??
- ????????????????i++;??
- ????????????}??
- ????????}?catch?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ????????}??
- ????}??
- }??
- class?ThreadEcho?implements?Runnable??
- {??
- ????private?Socket?incoming;??
- ????public?ThreadEcho(Socket?s)??
- ????{??
- ????????incoming=s;??
- ????}??
- ????public?void?run()?{??
- ????????try?{??
- ????????????InputStream?i=incoming.getInputStream();??
- ????????????OutputStream?o=incoming.getOutputStream();??
- ????????????Scanner?s=new?Scanner(i);??
- ????????????PrintWriter?p=new?PrintWriter(o);??
- ????????????boolean?done=false;??
- ????????????while(!done?&&?s.hasNext())??
- ????????????{??
- ????????????????String?line=s.nextLine();??
- ????????????????p.println("Echo"+line+"\n");??
- ????????????????p.flush();??
- ????????????????System.out.println("MSG"+line);??
- ????????????????if(line.trim().equals("bye"))??
- ????????????????{??
- ????????????????????done=true;??
- ????????????????}??
- ????????????}??
- ????????????System.out.println("用户退出");??
- ????????}?catch?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ????????}finally??
- ????????{??
- ????????????try?{??
- ????????????????incoming.close();??
- ????????????}?catch?(IOException?e)?{??
- ????????????????e.printStackTrace();??
- ????????????}??
- ????????}??
- ????}??
- ??????
- }??
- <?xml?version="1.0"?encoding="utf-8"?>??
- <mx:Application?xmlns:mx="http://www.adobe.com/2006/mxml"?layout="absolute"?>??
- <mx:Script>??
- ????<![CDATA[??
- ????private?var?sock:Socket;??
- ????????private?function?connSocket():void??
- ????????{??
- ????????????sock=new?Socket();??
- ????????????sock.addEventListener(Event.CONNECT,onConnect)??
- ????????????sock.addEventListener(ProgressEvent.SOCKET_DATA,MSGHandler)??
- ????????????sock.connect(adds.text,int(post.text));??
- ????????}??
- ????????private?function?onConnect(evt:Event):void??
- ????????{??
- ????????????trace("Connection?sucess");??
- ????????}??
- ????????private?function?sendMSG():void??
- ????????{??
- ????????????sock.writeUTFBytes(mess.text+"\n");??
- ????????????sock.flush();??
- ????????????trace('send?msg'+mess.text);??
- ????????}??
- ????????/**消息获取函数*/??
- ????????private?function?MSGHandler(event:ProgressEvent):void??
- ????????{??
- ????????????trace("GetMSG"+sock.readUTFBytes(sock.bytesAvailable));??
- ????????}??
- ????]]>??
- </mx:Script>??
- <mx:Button?label="连接服务器"?click="connSocket()"/>??
- <mx:TextInput?text="127.0.0.1"?x="100"?width="200"?id="adds"/>??
- <mx:TextInput?text="8189"?x="300"?id="post"/>??
- <mx:Button?label="发送数据"?click="sendMSG()"?y="40"/>??
- <mx:TextInput?id="mess"?x="100"?y="40"/>??
- </mx:Application>??
注意:需要使用手动的\n结尾发送数据