简单的mina应用

这篇博客介绍了如何创建一个简单的MINA应用,包括服务端和客户端的实现。服务端涉及IoAccepter配置、过滤器、缓冲区、空闲时间及业务处理类的设定,客户端则涵盖IoConnector、业务处理类以及连接管理和消息发送。
摘要由CSDN通过智能技术生成

简单的mina应用

一个简单的mina服务端客户端,只包含必要的元素,理解用mina搭建服务器的步骤。

简单的mina服务端

  1. 新建IoAccepter接收器
  2. 设置过滤器;
  3. 设置缓冲区大小;
  4. 设置空闲时间;
  5. 设置业务处理类;
  6. 绑定端口;

服务端代码

代码片.

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 MinaServer {
	private static int port = 8888;//端口

	public static void main(String[] args) {
		IoAcceptor accept = null;//接收器
		try {
			accept = new NioSocketAcceptor();//NioSocketAcceptor 接收器
			accept.getFilterChain().addLast("codec",
					new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("utf-8"),
							LineDelimiter.WINDOWS.getValue(), LineDelimiter.WINDOWS.getValue())));//设置过滤器
			accept.getSessionConfig().setReadBufferSize(1024);//设置缓冲区大小
			accept.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);//设置空闲时间
			accept.setHandler(new MyHandler());//设置业务处理类
			accept.bind(new InetSocketAddress(port));//绑定端口
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
package com.mina;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class MyHandler implements IoHandler {
	@Override
	public void sessionCreated(IoSession session) throws Exception {
		System.out.println("sessionCreated--》创建");
		
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		System.out.println("sessionOpened--》打开");

	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		System.out.println("sessionClosed--》关闭");

	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		System.out.println("sessionIdle--》空闲");
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		System.out.println("exceptionCaught--》出现异常");//强退时会出现异常
		
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		System.out.println("messageReceived--》接收消息");
		String msg = (String) message;
		System.out.println("接收到消息:"+msg);
		if(msg.equals("exit")) {
			session.close();
		}
		session.write(new Date());
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		System.out.println("messageSent--》发送消息");
	}

	@Override
	public void inputClosed(IoSession session) throws Exception {
		System.out.println("inputClosed");

	}

}

简单的mina客户端

  1. 新建IoConnector连接器
  2. 设置业务处理类;
  3. 建立连接
  4. 连接阻塞,监听连接成功
  5. 获得IoSession;
  6. 发送消息
  7. 监听关闭阻塞;

客户端代码

package com.mina;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoConnector;
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.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class MinaClient {
   private static String host  ="127.0.0.1";
   private static int port =8888;
   public static void main(String[] args) {
   	Properties properties = new Properties();
   	try {
   		properties.load(MinaClient.class.getClassLoader().getResourceAsStream("gameLog4j.properties"));
   	} catch (Exception e) {
   		e.printStackTrace();
   	}
   	PropertyConfigurator.configure(properties);
   	
   	IoConnector conn= new NioSocketConnector();
   	conn.setConnectTimeout(3000);
   	conn.getFilterChain().addLast("codec", new ProtocolCodecFilter(
   			new TextLineCodecFactory(Charset.forName("utf-8"), LineDelimiter.WINDOWS.getValue(), LineDelimiter.WINDOWS.getValue())));
   	conn.setHandler(new MyClientHandler());
   	ConnectFuture connectFuture = conn.connect(new InetSocketAddress(host, port));
   	connectFuture.awaitUninterruptibly();
   	IoSession session = connectFuture.getSession();
   	session.write("hello");
   	session.getCloseFuture().awaitUninterruptibly();
   	session.closeNow();
   	
   }
}
package com.mina;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class MyClientHandler implements IoHandler {
   @Override
   public void sessionCreated(IoSession session) throws Exception {
   	System.out.println("sessionCreated");
   }

   @Override
   public void sessionOpened(IoSession session) throws Exception {
   	System.out.println("sessionOpened");

   }

   @Override
   public void sessionClosed(IoSession session) throws Exception {
   	System.out.println("sessionClosed");

   }

   @Override
   public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
   	System.out.println("sessionIdle");
   }

   @Override
   public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
   	System.out.println("exceptionCaught");

   }

   @Override
   public void messageReceived(IoSession session, Object message) throws Exception {
   	System.out.println("messageReceived");
   	System.out.println("message:"+message);
   }

   @Override
   public void messageSent(IoSession session, Object message) throws Exception {
   	System.out.println("messageSent");
   }

   @Override
   public void inputClosed(IoSession session) throws Exception {
   	System.out.println("inputClosed");
   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值