Java和flash的通讯代码(XMLSocket类)
今天研究了一下flash里的XMLSocket类的通信源码,给大家分享看看!
flash代码:
CODE:[Copy to clipboard]var mySocket:XMLSocket = new XMLSocket(); //本地地址,端口5000 mySocket.connect("localhost", 5000); //事件 mySocket.onConnect = function(myStatus) { if (myStatus) { ; trace("连接成功!"); ; //发送信息 ; mySocket.send("soda" + "n"); ; trace("发送成功!"); } else { ; trace("连接失败!"); } }; //接受信息事件 mySocket.onData = function (msg:String) { trace("接受到的内容:" + msg); } -------------------------------------------------- java代码:
CODE:[Copy to clipboard]import java.net.*; import java.io.*;
public class Server { public static void main(String[] args) throws Exception { ; //建立服务器(TCP) ; ServerSocket ss = new ServerSocket(5000); ; System.out.println("等待连接......."); ; Socket sk = ss.accept(); ; System.out.println("连接成功......"); ; //建立字符流 ; BufferedReader br = new BufferedReader(new InputStreamReader(sk.getInputStream())); ; System.out.println("获取BufferedReader......"); ; System.out.println("获取里面的内容......"); ; //等待接受信息 ; String name = br.readLine();
//发送信息 ; PrintWriter os = new PrintWriter(sk.getOutputStream()); ; os.println("soda,soda" + "n"); ; os.flush(); ; os.close(); ; System.out.println("内容:" + name); } }
相关文章: