mina 简单示例

public class MyServer {

	public static void main(String[] args){
		//IoAcceptor acceptor = new NioSocketAcceptor();
		SocketAcceptor acceptor = new NioSocketAcceptor();   
		acceptor.getSessionConfig().setReadBufferSize(2048*2);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		/*acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(
			new TextLineCodecFactory(Charset.forName("utf-8"),LineDelimiter.WINDOWS.getValue(),
			LineDelimiter.WINDOWS.getValue())
		));*/
		//acceptor.setHandler(new MyIoHandle());

		acceptor.setHandler(new Flash843());
		try {
			acceptor.bind(new InetSocketAddress(8899));
			//acceptor.bind( new InetSocketAddress(843)); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}



public class Flash843 extends IoHandlerAdapter {
	List list = new ArrayList();
	  /**
	  * 对消息进行处理
	  **/
	public void messageReceived(IoSession session, Object message) {
		 
			 
	
		try {
			IoBuffer get = (IoBuffer)message;
        System.out.println("Rec:" + get.getHexDump());
            String s = new String(get.array());
            //System.out.println("s=="+s);
            if (s.indexOf("<policy-file-request/>") != -1)
            {
            	System.out.println("========1111======="+session.getId());
                    String res = "<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\"/></cross-domain-policy>";
                   
                                 byte[] reps = res.getBytes("UTF-8");   
                                    IoBuffer wb = IoBuffer.allocate(reps.length+1);   
                                     wb.put(reps);   
                                     wb.put((byte)0x0);   
                                    wb.flip();   
                                    session.write(wb);   
                                   
                
            } else{
            	String res="分布式数据库系统的优点";
            	System.out.println("========222======="+ message.toString());
            	//for(int i=0;i<5;i++){
            	System.out.println("session=="+session);
            	
            	 byte[] reps = res.getBytes();   
                 IoBuffer wb = IoBuffer.allocate(reps.length+1);  
                 wb.put(reps);   
                 wb.put((byte)0x0);   
                 wb.flip();  
                 wb.clear();
                session.write(wb); 
                
                
                
                System.out.println("===afert====");
                list.add(session);
                //test();
                new T().start();
            	//}
            }
		}
		catch(Exception e){
			e.printStackTrace();
		}
	} 
	class T extends Thread{
		public  void run(){
			   System.out.println("list==="+list.size());
			
				   try {
					   Thread.sleep(5000);
			
				   System.out.println("list333==="+list.size());
				   
					//Thread.sleep(5000);
					if(list.size()>0){
						System.out.println("list(0)=="+list.get(0));
					IoSession session=(IoSession)list.get(0);
					System.out.println("session==="+session.getId());
					String res="Hi,sdfafsdf?";

			    	 byte[] reps = res.getBytes("UTF-8");   
			         IoBuffer wb = IoBuffer.allocate(reps.length+1);  
			         wb.put(reps);   
			         wb.put((byte)0x0);   
			         wb.flip();  
			      
			        session.write(wb);
			    
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			   }
			   
			
	} 


你可能感兴趣的:(thread,windows,xml,Access,Mina)