Socket网络编程-Mina 实例
public class MinaServer {
public static final int PORT = 1989;
private static final Logger logger = Logger.getLogger(MinaServer.class);
public static void main(String[] args) {
IoAcceptor ioAcceptor = null;
try {
//创建一个非阻塞 的Server端Socket
ioAcceptor = new NioSocketAcceptor();
//
ioAcceptor.getFilterChain().addLast(
“codec”,
new ProtocolCodecFilter(new TextLineCodecFactory(Charset
.forName(“UTF-8”),LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));
//设置读取数据的缓冲区大小
ioAcceptor.getSessionConfig().setReadBufferSize(4096);
//设置10秒没有任何操作进入空闲状态
ioAcceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
//设置业务逻辑处理程序
ioAcceptor.setHandler(new DefaultServerHander());
ioAcceptor.bind(new InetSocketAddress(PORT));
logger.info(“在端口” + PORT + “启动Mina监听程序成功”);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第二部: 创建Mina服务端处理程序
public class MinaServer {
public static final int PORT = 1989;
private static final Logger logger = Logger.getLogger(MinaServer.class);
public static void main(String[] args) {
IoAcceptor ioAcceptor = null;
try {
//创建一个非阻塞 的Server端Socket
ioAcceptor = new NioSocketAcceptor();
//
ioAcceptor.getFilterChain().addLast(
“codec”,
new ProtocolCodecFilter(new TextLineCodecFactory(Charset
.forName(“UTF-8”),LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));
//设置读取数据的缓冲区大小
ioAcceptor.getSessionConfig().setReadBufferSize(4096);
//设置10秒没有任何操作进入空闲状态
ioAcceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
//设置业务逻辑处理程序
ioAcceptor.setHandler(new DefaultServerHander());
ioAcceptor.bind(new InetSocketAddress(PORT));
logger.info(“在端口” + PORT + “启动Mina监听程序成功”);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第三部:客户端连接程序代码
public class MinaClient {
private static final int PORT = 1989;
private static final String Host = “localhost”;
public static void main(String[] args) {
// 创建一个非阻塞的客户端连接
IoConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast(
“codec”,
new ProtocolCodecFilter(new TextLineCodecFactory(Charset
.forName(“UTF-8”),LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));
connector.setHandler(new MinaClientHander());
ConnectFuture connectFuture = connector.connect(new InetSocketAddress(Host, PORT));
connectFuture.awaitUninterruptibly(); //等待连接创建完成
IoSession session = connectFuture.getSession();
session.write(“高江涛”);
session.getCloseFuture().awaitUninterruptibly(); //等待连接断开
connector.dispose();
}
}
第四步: 客户端处理程序
public class MinaClientHander extends IoHandlerAdapter {
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
// TODO Auto-generated method stub
super.exceptionCaught(session, cause);
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
// TODO Auto-generated method stub
super.messageReceived(session, message);
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
// TODO Auto-generated method stub
super.messageSent(session, message);
}
@Override
public void sessionClosed(IoSession session) throws Exception {
// TODO Auto-generated method stub
super.sessionClosed(session);
}
@Override
public void sessionCreated(IoSession session) throws Exception {
// TODO Auto-generated method stub
super.sessionCreated(session);
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
// TODO Auto-generated method stub
super.sessionIdle(session, status);
}
@Override