Search in sources :

Example 6 with ObjectSerializationCodecFactory

use of org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory in project pancm_project by xuwujing.

the class MinaServer method main.

/**
 * The entry point of application.
 *
 * @param args the input arguments
 */
/* 启动此类 提示服务端运行成功后
	 * Windows 命令 输入  telnet 127.0.0.1 3305
	 * 然后输入消息  message
	 * 消息为bye的时候关闭连接
	 * */
public static void main(String[] args) {
    IoAcceptor ia = null;
    try {
        // 创建一个非堵塞的server端Socket
        ia = new NioSocketAcceptor();
        // 创建 协议编码解码过滤器ProtocolCodecFilter
        // ProtocolCodecFilter pf=new ProtocolCodecFilter(new TextLineCodecFactory(Charset
        // .forName("UTF-8"),
        // LineDelimiter.WINDOWS.getValue(),
        // LineDelimiter.WINDOWS.getValue()));
        // 设置端口
        InetSocketAddress pt = new InetSocketAddress(PORT);
        // 设置过滤器(使用Mina提供的文本换行符编解码器)
        ia.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
        // 设置读取数据的缓存区大小
        ia.getSessionConfig().setReadBufferSize(2048);
        // 读写通道10秒内无操作进入空闲状态
        ia.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
        // 绑定逻辑处理器
        ia.setHandler(new MinaServerHandler());
        // 绑定端口
        ia.bind(pt);
        logger.info("服务端启动成功...端口号为:" + PORT);
    } catch (Exception e) {
        logger.error("服务器的异常..." + e);
        e.printStackTrace();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ObjectSerializationCodecFactory(org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory) IoAcceptor(org.apache.mina.core.service.IoAcceptor) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) NioSocketAcceptor(org.apache.mina.transport.socket.nio.NioSocketAcceptor)

Aggregations

ObjectSerializationCodecFactory (org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory)6 Charset (java.nio.charset.Charset)3 ProtocolCodecFilter (org.apache.mina.filter.codec.ProtocolCodecFilter)3 LineDelimiter (org.apache.mina.filter.codec.textline.LineDelimiter)3 InetSocketAddress (java.net.InetSocketAddress)2 MinaClientHandler (com.pancm.test.minatest.demo.MinaClientHandler)1 ConnectFuture (org.apache.mina.core.future.ConnectFuture)1 IoAcceptor (org.apache.mina.core.service.IoAcceptor)1 IoConnector (org.apache.mina.core.service.IoConnector)1 NioSocketAcceptor (org.apache.mina.transport.socket.nio.NioSocketAcceptor)1 NioSocketConnector (org.apache.mina.transport.socket.nio.NioSocketConnector)1