nio/mina(二)mina传字符串

服务端:

MainServer.java

package com.nafio.mina;  
      
    import java.net.InetSocketAddress;  
      
    import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;  
    import org.apache.mina.filter.codec.ProtocolCodecFilter;  
    import org.apache.mina.filter.codec.textline.TextLineCodecFactory;  
    import org.apache.mina.transport.socket.SocketAcceptor;  
    import org.apache.mina.transport.socket.nio.NioSocketAcceptor;  
      
    public class MainServer {  
      
        /** 
         * @param args 
         */  
        public static void main(String[] args) throws Exception{  
            //创建一个非阻塞的Server端Socket  
            SocketAcceptor acceptor = new NioSocketAcceptor();  
            //创建接收数据的过滤器  
            DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();  
            //设定这个过滤器将一行一行(/r/n)的读取数据  
            chain.addLast("myChin", new ProtocolCodecFilter(new  
            TextLineCodecFactory()));  
            //设定服务器端的消息处理器:一个SamplMinaServerHandler对象,  
            acceptor.setHandler(new MinaServerHandler());  
            // 服务器端绑定的端口  
            int bindPort=9988;  
            //绑定端口,启动服务器  
            acceptor.bind(new InetSocketAddress(bindPort));  
            System.out.println("Mina Server is Listing on:= " + bindPort);  
        }  
      
    }

2 MinaServerHandler.java

package com.nafio.mina;  
      
    import org.apache.mina.core.service.IoHandlerAdapter;  
    import org.apache.mina.core.session.IoSession;  
      
    public class MinaServerHandler extends IoHandlerAdapter{  
      
        /** 
         * 当客户端发送消息到达时 
         */  
      
        public void messageReceived(IoSession session, Object message)  
        throws Exception {  
            //super.messageReceived(session, message);  
            //我们己设定了服务器解析消息的规则是一行一行读取,这里就可转为String:  
              
            System.out.println("ServerMessageReceived");  
              
            String s=(String)message;  
            // Write the received data back to remote peer  
            System.out.println("服务端->收到客户机发来的消息: "+s);  
            //测试将消息回送给客户端  
            session.write(s+count);  
            count++;  
        }  
        private int count = 0;  
      
        public void messageSent(IoSession session, Object message) throws Exception {  
            System.out.println("服务端->messageSent");  
        }  
      
      
        public void sessionClosed(IoSession session) throws Exception {  
            System.out.println("服务端->sessionClosed");  
        }  
      
        @Override  
        public void sessionCreated(IoSession session) throws Exception {  
            System.out.println("服务端->sessionCreated");  
        }  
      
        /** 
         * 当新客户端连接时调用 
         */  
      
        public void sessionOpened(IoSession session) throws Exception {  
            System.out.println("服务端->Open");  
        }  
      
    }

客户端:

1 MainClient.java

package com.nafio.mina;  
      
    import java.net.InetSocketAddress;  
      
    import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;  
    import org.apache.mina.core.future.ConnectFuture;  
    import org.apache.mina.filter.codec.ProtocolCodecFilter;  
    import org.apache.mina.filter.codec.textline.TextLineCodecFactory;  
    import org.apache.mina.transport.socket.nio.NioSocketConnector;  
      
    public class MainClient {  
        public static void main(String[] args) throws Exception{  
            // 创建 TCP/IP connector.  
            NioSocketConnector connector = new NioSocketConnector();  
            // 创建接收数据的过滤器  
            DefaultIoFilterChainBuilder chain = connector.getFilterChain();  
            //设定这个过滤器将一行一行(/r/n)的读取数据  
            chain.addLast("myChin", new ProtocolCodecFilter(new  
            TextLineCodecFactory()));  
            //设定服务器端的消息处理器:一个SamplMinaServerHandler对象,  
            connector.setHandler(new MinaClientHandler());  
            // Set connect timeout.  
            connector.setConnectTimeout(30);  
            //连结到服务器:  
            ConnectFuture cf = connector.connect(new  
            InetSocketAddress("localhost", 9988));  
              
            cf.awaitUninterruptibly();  
            cf.getSession().getCloseFuture().awaitUninterruptibly();  
            connector.dispose();  
        }  
    }

2 MinaClientHandler.java

package com.nafio.mina;  
      
    import org.apache.mina.core.service.IoHandlerAdapter;  
    import org.apache.mina.core.session.IoSession;  
      
    public class MinaClientHandler extends IoHandlerAdapter{  
      
      
        /** 
         * 当客户端发送的消息到达时 
         */  
        @Override  
        public void messageReceived(IoSession session, Object message)  
        throws Exception {  
      
            String s=(String)message;  
            System.out.println("客户端->服务器发来的消息: "+s);  
        }  
      
      
        @Override  
        public void messageSent(IoSession session, Object message) throws Exception {  
            super.messageSent(session, message);  
            System.out.println("客户端->messageSent");  
        }  
      
        @Override  
        public void sessionClosed(IoSession session) throws Exception {  
            System.out.println("客户端->sessionClosed");  
        }  
      
        @Override  
        public void sessionCreated(IoSession session) throws Exception {  
            System.out.println("客户端->sessionCreated");  
        }  
      
        @Override  
        public void sessionOpened(IoSession session) throws Exception {  
            System.out.println("客户端->sessionOpened");  
      
            //发送信息  
            try  
            {  
                SendMsg(session);  
            }  
            catch(Exception e)  
            {  
                e.printStackTrace();  
            }  
        }  
      
        //by nafio 自己写的函数  
        public void SendMsg(IoSession session) throws Exception{  
            //      TransferClass tansferClass=new TransferClass();  
            //      tansferClass.setNum(1);  
            //      tansferClass.setString("字符串");  
            String tansferStr="我是数据";  
            session.write(tansferStr);    
        }  
      
      
    }

相关推荐