首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

flex+java+sokcet长连接的兑现,处理了沙漏

2012-11-06 
flex+java+sokcet长连接的实现,处理了沙漏首先开个843端口处理沙漏问题package com.medouimport java.io.

flex+java+sokcet长连接的实现,处理了沙漏
首先开个843端口处理沙漏问题package com.medou;import java.io.*;import java.net.*;public class SecurityServerSocket { private ServerSocket server; private String xml="<?xml version="1.0" encoding="utf-8"?>"+ ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">" +"<cross-domain-policy>"+ "<allow-access-from domain='"127.0.0.1"' to-ports='"8400"/'>" +"<allow-access-from domain='"localhost"' to-ports='"8400"/'>"+ "<allow-access-from domain='"*"' to-ports='"*"/'>"+"</allow-access-from></allow-access-from></allow-access-from></cross-domain-policy>"; public SecurityServerSocket() throws IOException {server=new ServerSocket(843);while(true) {Socket s=server.accept();sendToCrossXml(s);} } /** * 发送策略文件格式 * @param s * @throws IOException */ private void sendToCrossXml(Socket s) throws IOException { PrintWriter pw=new PrintWriter(s.getOutputStream()); pw.write(xml+"\0");//必须有\0,否则没效果 pw.flush(); s.getOutputStream().flush(); System.out.println("策略已发送,等待连接"); } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { new SecurityServerSocket(); }}发送安全策略后,会自动关闭其次开个8888端口作为和flex的长连接通讯package com.medou;import java.io.*;import java.net.*;import java.util.*;public class Server {private ServerSocket server;private Vector<socket> sManager=new Vector<socket>();//管理套接字的Vectorprivate Random rnd=new Random();//创建随机数的发生器 public Server() {createServerSocket(8888);}//启动服务器private void createServerSocket(int port) {try {server = new ServerSocket(port);System.out.println("服务监听端口:" + port);while(true) {Socket client = server.accept();System.out.println("长连接已建立:" + port);new Phone_Thread(client).start();sManager.add(client);System.out.println("当前客户机连结数:"+sManager.size());}} catch (IOException e) {e.printStackTrace();}}class Phone_Thread extends Thread {private BufferedReader reader;private BufferedWriter writer;private Socket socket;Phone_Thread(Socket socket) {//构造函数this.socket=socket;}public void run() {try {InputStreamReader input = new InputStreamReader(socket.getInputStream(), "UTF-8");reader = new BufferedReader(input);OutputStreamWriter output = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");writer = new BufferedWriter(output);while(true) { //读取客户端发送的数据StringBuilder data = new StringBuilder();char[] a = new char[50];int j = reader.read(a);for(int i=0;i<j if data else break string info="data.toString().trim();" system writer catch e finally try null reader socket smanager ie public static void main args new server version="1.0" encoding="utf-8"><application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationcomplete="initApp()"><script>0&&intSY-2){myText.text+="结果:你输了\n";}else if(intSY>=2){myText.text+="结果:你输了\n";}else if(intSY</script><textarea x="10" y="32" width="331" height="263" id="myText"></textarea><textinput x="10" y="303" width="245" id="myInput"></textinput><button x="273" y="304" label="发送" id="myBtn" click="sendMessage(myInput.text)"></button><label x="10" y="10" text="规则:0代表剪刀;1代表锤;2代表包袱" width="331" fontweight="bold"></label></application></j></socket></socket>

热点排行