socket通信框架mina使用详解(一)
1.mina框架基于tcp/ip,udp/ip协议栈的通信框架。
2.mina框架的执行流程:
mina框架客户端与服务器端的执行流程一致,不同的是:Ioservice的client端实现是Ioconnector,server端是IoAcceptor.
客户端实现
package com.minaclient;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 客户端 iohandler
* @author liudd
*
*/
public class MyClinetIoHandler extends IoHandlerAdapter {
private final static Logger logger=LoggerFactory.getLogger(MyClinetIoHandler.class);
private final String values;
public MyClinetIoHandler(String values){
this.values=values;
}
@Override
public void sessionOpened(IoSession session) throws Exception {
// TODO Auto-generated method stub
session.write(values);
}
}
package com.minaclient;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
/**
* mina框架中客户端与服务器端的执行流程一致,
* 唯一的区别是:
* ioservice的client实现是ioconnetor,server端是ioacceptor
* @author liudd
*
*/
public class MyClient {
public static void main(String[] args) {
//1.创建ioservice
IoConnector connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(30000);
//2.注册过滤器
connector.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset
.forName("UTF-8"), LineDelimiter.WINDOWS.getValue(),
LineDelimiter.WINDOWS.getValue())));
//3注册iohandler,到ioserivce
connector.setHandler(new MyClinetIoHandler("你好!\r\n大家好!"));
//4.绑定套接字,建立连接
connector.connect(new InetSocketAddress("localhost",1000));
}
}
server端实现:
package com.minaserver;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* iohandler处理客户端的数据
* @author liudd
*
*/
public class MyServerIoHandler extends IoHandlerAdapter {
private final static Logger logs=LoggerFactory.getLogger(MyServerIoHandler.class);
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String str=message.toString();
logs.info("the message received is["+str+"]");
System.out.println("the message received is["+str+"]");
if(str.endsWith("quit"))
{
session.close(true);
return ;
}
}
}
package com.minaserver;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class MyServer {
public static void main(String[] args) throws IOException {
//1.编写ioservice
IoAcceptor acceptor=new NioSocketAcceptor();
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,10);//读写通道在10秒内无任何操作进入空闲状态
//2.编写过滤器,
//通过TextLineCodeFactory编解码工厂对字符串进行编解码处理
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"),LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));//以换行符为标识的数据
//3.注册iohandler到ioservice
acceptor.setHandler(new MyServerIoHandler());
//4绑定套接字
acceptor.bind(new InetSocketAddress(1000));
}
}