标签:nas ext last object err actor net toc 时长
客户端代码不变,参照 http://www.cnblogs.com/Westfalen/p/6251473.html
服务端代码如下:
import java.io.IOException; import java.net.InetSocketAddress; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IdleStatus; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; /** * 使用mina作为服务器进行socket通讯 * * 需要jar包: mina-core-2.0.16.jar, slf4j-api-1.7.21.jar * jar包下载地址 : http://mina.apache.org/downloads-mina.html * */ public class MinaServer { public static void main(String[] args) { try { // 4步操作 // 1.新建 NioSocketAcceptor 事例对象 NioSocketAcceptor acceptor = new NioSocketAcceptor(); // 2.设置消息处理对象 acceptor.setHandler(new MyServerHandler()); // 3.设置消息解码器 acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory())); // acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 3); // //设置idle时长 // 4.绑定端口开启服务 acceptor.bind(new InetSocketAddress(9999)); } catch (IOException e) { e.printStackTrace(); } } } /** * 专门处理消息的类,实现网络连接和消息处理的解耦 */ class MyServerHandler extends IoHandlerAdapter { /** 异常时候的处理 */ @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { System.out.println("exceptionCaught"); } /** 收到消息 */ @Override public void messageReceived(IoSession session, Object message) throws Exception { System.out.println("messageReceived " + message); // session.write("reply " + message); //收到消息就马上把消息回送给客户端 } /** 发送消息 */ @Override public void messageSent(IoSession session, Object message) throws Exception { System.out.println("messageSent " + message); } /** 通讯闲置时候的处理 */ @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { System.out.println("sessionIdle"); } }
标签:nas ext last object err actor net toc 时长
原文地址:http://www.cnblogs.com/Westfalen/p/6252555.html