mina框架简单使用
mina框架是广泛实用的socket框架,使用nio代码开发。
服务端代码:
import java.net.InetSocketAddress;
import org.apache.mina.core.service.IoHandler;
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;
public class MyServerBin {
public static void main(String[] args) {
MyServerBin myServerBin=new MyServerBin();
myServerBin.startAction();
}
void startAction(){
NioSocketAcceptor nioSocketAcceptor=new NioSocketAcceptor();
try {
nioSocketAcceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
nioSocketAcceptor.getSessionConfig().setReadBufferSize(1024*1024);
nioSocketAcceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
nioSocketAcceptor.setHandler(new nioHandler());
nioSocketAcceptor.bind(new InetSocketAddress(7878));
} catch (Exception e) {
e.printStackTrace();
}
}
class nioHandler implements IoHandler{
@Override
public void exceptionCaught(IoSession arg0, Throwable arg1)
throws Exception {
System.out.println("exceptionCaught");
}
@Override
public void messageReceived(IoSession arg0, Object arg1)
throws Exception {
String clientString=(String)arg1;
System.out.println("客户端信息:"+clientString);
arg0.write("服务端返回:"+clientString+"\n");
}
@Override
public void messageSent(IoSession arg0, Object arg1) throws Exception {
System.err.println("messageSent");
}
@Override
public void sessionClosed(IoSession arg0) throws Exception {
System.out.println("sessionClosed");
}
@Override
public void sessionCreated(IoSession arg0) throws Exception {
System.out.println("sessionCreated");
}
@Override
public void sessionIdle(IoSession arg0, IdleStatus arg1)
throws Exception {
System.out.println("心跳设置");
}
@Override
public void sessionOpened(IoSession arg0) throws Exception {
System.out.println("sessionOpened");
}
}
}
客户端代码:
import java.net.InetSocketAddress;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoHandler;
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.NioSocketConnector;
public class MyClickBin {
public static void main(String[] args) {
MyClickBin myClickBin=new MyClickBin();
myClickBin.startAction();
}
void startAction(){
NioSocketConnector nioSocketConnector=new NioSocketConnector();
try {
nioSocketConnector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
nioSocketConnector.getSessionConfig().setReadBufferSize(1024*1024);
nioSocketConnector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
nioSocketConnector.setHandler(new MyHander());
ConnectFuture con = nioSocketConnector.connect(new InetSocketAddress("127.0.0.1", 7878));
con.awaitUninterruptibly();
IoSession mSession = con.getSession();
mSession.write("你好吗\n");
} catch (Exception e) {
e.printStackTrace();
}
}
class MyHander implements IoHandler{
@Override
public void exceptionCaught(IoSession arg0, Throwable arg1)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void messageReceived(IoSession arg0, Object arg1)
throws Exception {
// TODO Auto-generated method stub
System.out.println(arg1);
}
@Override
public void messageSent(IoSession arg0, Object arg1) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void sessionClosed(IoSession arg0) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void sessionCreated(IoSession arg0) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void sessionIdle(IoSession arg0, IdleStatus arg1)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void sessionOpened(IoSession arg0) throws Exception {
// TODO Auto-generated method stub
}
}
}