Apache Mina简单实例客户端篇

Java代码  收藏代码
    1. package com.heming.apache.mina.samplesclient;  
    2.   
    3. import org.apache.mina.core.service.IoHandlerAdapter;  
    4. import org.apache.mina.core.session.IdleStatus;  
    5. import org.apache.mina.core.session.IoSession;  
    6.   
    7. /** 
    8.  * 消息处理类 
    9.  * @author 何明 
    10.  * 
    11.  */  
    12. public class SamplMinaClientHander extends IoHandlerAdapter {  
    13.   
    14.     @Override  
    15.     public void exceptionCaught(IoSession arg0, Throwable arg1)  
    16.             throws Exception {  
    17.         // TODO Auto-generated method stub  
    18.   
    19.     }  
    20.   
    21.     /** 
    22.      * 当客户端接受到消息时 
    23.      */  
    24.     @Override  
    25.     public void messageReceived(IoSession session, Object message) throws Exception {  
    26.   
    27.         //我们已设定了服务器的消息规则是一行一行读取,这里就可以转为String:  
    28.         String s = (String)message;  
    29.           
    30.         //Writer the received data back to remote peer  
    31.         System.out.println("服务器发来的收到消息: " + s);  
    32.           
    33.         //测试将消息回送给客户端  
    34.         session.write(s);  
    35.   
    36.     }  
    37.   
    38.     @Override  
    39.     public void messageSent(IoSession arg0, Object arg1) throws Exception {  
    40.         // TODO Auto-generated method stub  
    41.   
    42.     }  
    43.   
    44.     /** 
    45.      * 当一个客户端被关闭时 
    46.      */  
    47.     @Override  
    48.     public void sessionClosed(IoSession session) throws Exception {  
    49.         System.out.println("one client Disconnect");  
    50.   
    51.     }  
    52.   
    53.     @Override  
    54.     public void sessionCreated(IoSession arg0) throws Exception {  
    55.         // TODO Auto-generated method stub  
    56.   
    57.     }  
    58.   
    59.     @Override  
    60.     public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {  
    61.         // TODO Auto-generated method stub  
    62.   
    63.     }  
    64.   
    65.     /** 
    66.      * 当一个客户端连接进入时 
    67.      */  
    68.     @Override  
    69.     public void sessionOpened(IoSession session) throws Exception {  
    70.   
    71.         System.out.println("incomming client:" + session.getRemoteAddress());  
    72.         session.write("我来啦");  
    73.   
    74.     }  
    75.   
    76. }  
    77.   
    78.   
    79. package com.heming.apache.mina.samplesclient;  
    80.   
    81. import java.net.InetSocketAddress;  
    82.   
    83. import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;  
    84. import org.apache.mina.core.future.ConnectFuture;  
    85. import org.apache.mina.filter.codec.ProtocolCodecFilter;  
    86. import org.apache.mina.filter.codec.textline.TextLineCodecFactory;  
    87. import org.apache.mina.transport.socket.nio.NioSocketConnector;  
    88.   
    89. /** 
    90.  * 启动客户端 
    91.  * @author 何明 
    92.  * 
    93.  */  
    94. public class MainClient {  
    95.   
    96.     public static void main(String []args)throws Exception{  
    97.           
    98.         //Create TCP/IP connection  
    99.         NioSocketConnector connector = new NioSocketConnector();  
    100.           
    101.         //创建接受数据的过滤器  
    102.         DefaultIoFilterChainBuilder chain = connector.getFilterChain();  
    103.           
    104.         //设定这个过滤器将一行一行(/r/n)的读取数据  
    105.         chain.addLast("myChin"new ProtocolCodecFilter(new TextLineCodecFactory()));  
    106.           
    107.         //服务器的消息处理器:一个SamplMinaServerHander对象  
    108.         connector.setHandler(new SamplMinaClientHander());  
    109.           
    110.         //set connect timeout  
    111.         connector.setConnectTimeout(30);  
    112.           
    113.         //连接到服务器:  
    114.         ConnectFuture cf = connector.connect(new InetSocketAddress("localhost",8899));  
    115.           
    116.         //Wait for the connection attempt to be finished.  
    117.         cf.awaitUninterruptibly();  
    118.           
    119.         cf.getSession().getCloseFuture().awaitUninterruptibly();  
    120.           
    121.         connector.dispose();  
    122.     }  
    123.       

原文地址:https://www.cnblogs.com/orochihuang/p/2525793.html