`

flex_flush_Socket 安全沙箱解决

    博客分类:
  • flex
阅读更多

1.写了个聊天,放到web 老报安全沙箱,改了一上午

 解决办法:使用另个端口843接听,flex中连接,他会发送一个<policy-file-request/>,然后你给他返回一个

crossdomain.xml,下边链接都有说明 ,发送crossdomain.xml时,必须追加\0,否则没效果

 

http://www.riachina.com/showtopic-13786.aspx

http://www.riacn.net/showtopic-12813.aspx

http://hi.baidu.com/fsnhf/blog/item/410c5a456e914f3787947356.html

 

package Control;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

public class Server {
	private ServerSocketChannel server=null;
	private Selector selector=null;
	InetSocketAddress isa=new InetSocketAddress("192.168.200.14",30000);
	private Charset charset=Charset.forName("UTF-8");
	public static void main(String args[]) throws IOException{
		new Server().init();
	}
	public void init() throws IOException{
		selector=Selector.open();
		server=ServerSocketChannel.open();
		server.socket().bind(isa);
		server.configureBlocking(false);
		server.register(selector,SelectionKey.OP_ACCEPT);
		while(selector.select()>0){
			//依次处理selector上的每个已选择的selectorKey
			for(SelectionKey sk:selector.selectedKeys()){
				selector.selectedKeys().remove(sk);
				//1如果有客户端连接
				if(sk.isAcceptable()){
					SocketChannel sc=server.accept();
					sc.configureBlocking(false);
					sc.register(selector, SelectionKey.OP_READ);
					sk.interestOps(SelectionKey.OP_ACCEPT);
				}
				//sk对应的通道有数据需要读取
				String content="";

				if(sk.isReadable()){
					SocketChannel sc=(SocketChannel)sk.channel();
					ByteBuffer buff=ByteBuffer.allocate(1024);
										try{
						while(sc.read(buff)>0){
							buff.flip();
							content+=charset.decode(buff);
						}
						System.out.println(content);
						sk.interestOps(SelectionKey.OP_READ);//设置成准备下次读取
					}catch(IOException e){
						e.printStackTrace();
						sk.cancel();
						if(sk.channel()!=null){
							sk.channel().close();
						}
					}
				}
				if(content.length()>0){
					for(SelectionKey key:selector.keys()){
						Channel targetChannel=key.channel();
						if(targetChannel instanceof SocketChannel){
							SocketChannel dest=(SocketChannel) targetChannel;
							dest.write(charset.encode(content));//写入buffer
						}
					}
				}
			}
		}
		
	}
	
}

 

package security;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SecurityServerSocket {

	private ServerSocket server;
	private String xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
	"<!DOCTYPE cross-domain-policy SYSTEM \"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=\"*\"/>"+
"</cross-domain-policy>";
	public SecurityServerSocket() throws IOException{
		server=new ServerSocket(843);
		while(true){
		  Socket s=server.accept();
		  sendToCrossXml(s);
		  System.out.println("already");
		}
	}
	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("lianjie ");
	}
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		new SecurityServerSocket();

	}

}

   把编译后的swf放到 webapps下的某个工程中测试,注意端口和ip

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			var s:Socket=null;
			public function init():void{
//				Alert.show("sdafklsdafk")
				s=new Socket("192.168.200.14",30000);
				s.addEventListener(IOErrorEvent.IO_ERROR,error);
				s.addEventListener(Event.CONNECT,onConn);
				s.addEventListener(ProgressEvent.SOCKET_DATA,onrecieve);
				
			}
			private function error(event:IOErrorEvent){
				Alert.show(event.text);
			}
			private function onConn(event:Event):void{
				Alert.show("conn");
			}
			private function onrecieve(event:Event):void{
				ta.text+=s.readUTF();
				
			}
			private function send(){
				if(s.connected){
					s.writeUTF(input.text);
					s.flush();
				}
			}
		]]>
	</mx:Script>
	<mx:Panel x="229" y="57" width="333" height="248" layout="absolute" title="测试发送">
		<mx:TextArea x="48" y="24" width="231" height="95" id="ta"/>
		<mx:TextInput x="34" y="165" id="input"/>
		<mx:Button x="224" y="165" label="发送" click="send()"/>
	</mx:Panel>
</mx:Application>
 
分享到:
评论
2 楼 fykyx521 2013-12-03  
两个同一个功能
1 楼 ustb 2013-11-05  
你上下两个代码类之间没关系

相关推荐

Global site tag (gtag.js) - Google Analytics