//服务端程序
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
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;
/**
*
* @author user
*/
public class MyServer {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
//IoAcceptor for socket transport (TCP/IP). This class handles incoming TCP/IP based socket connections.
IoAcceptor accpter=new NioSocketAcceptor();
accpter.getSessionConfig().setReadBufferSize(2048);//设置读取数据的缓冲区大小
accpter.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,10);//读写通道均在 10 秒内无任何操作就进入空闲状态
//设置编码 addLast(String name, IoFilter filter)
//name
- The filter's name
//filter
- The filter to add
accpter.getFilterChain().addLast("codec",new ProtocolCodecFilter(new TextLineCodecFactory(
Charset.forName("UTF-8")
//,LineDelimiter.WINDOWS.getValue(),
//LineDelimiter.WINDOWS.getValue()
) ));
accpter.setHandler(new MyIoHandler());//IoHandler 注册到 IoService
accpter.bind(new InetSocketAddress(9123));//绑定端口号
}
}
//编写IoHandler
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication1; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IoSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
/** * * @author user */ public class MyIoHandler extends IoHandlerAdapter{ // 这里我们使用的SLF4J作为日志门面,至于为什么在后面说明。 private final static Logger log = LoggerFactory.getLogger(MyIoHandler.class);
//收到客户端消息,当前方法就会执行 @Override public void messageReceived(IoSession session, Object message) throws Exception { System.out.println(message.toString()); String str = message.toString(); log.info("The message received is [" + str + "]"); if (str.endsWith("quit")) { session.close(true); return; } } }
//客户端程序
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication1;
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;
/** * * @author user */ public class MyClient { public static void main(String[] args){ IoConnector connector=new NioSocketConnector(); connector.setConnectTimeoutMillis(30000); connector.getFilterChain().addLast("codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName("UTF-8"), LineDelimiter.WINDOWS.getValue(), LineDelimiter.WINDOWS.getValue() ) ) ); connector.setHandler(new ClientHandler("你好\r\nHello World"));; connector.connect(new InetSocketAddress("127.0.0.1", 9123)); } }
//编写IoHandler
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author user
*/
public class ClientHandler extends IoHandlerAdapter{
private final static Logger LOGGER = LoggerFactory .getLogger(ClientHandler.class);
private final String values;
public ClientHandler(String values) {
this.values = values;
}
//当连接服务器是,向服务器发送消息
@Override
public void sessionOpened(IoSession session) {
session.write(values);
}
}