Apache Mina:一个简单的tcp通信demo

Posted 你是小KS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache Mina:一个简单的tcp通信demo相关的知识,希望对你有一定的参考价值。

当前版本:jdk1.8

1. 声明

当前内容主要为本人学习apache mina,主要为记录学习的demo,当前内容主要借鉴官方tcp的demo

基本依赖:

<!-- https://mvnrepository.com/artifact/org.apache.mina/mina-core -->
<dependency>
	<groupId>org.apache.mina</groupId>
	<artifactId>mina-core</artifactId>
	<version>2.1.5</version>
</dependency>

2. 服务器端

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.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NiosocketAcceptor;

/**
 * 
 * @author hy
 * @createTime 2021-12-26 09:34:27
 * @description 这个为时间服务器(专门提供时间的)
 *
 */
public class TimeServer 
	private static final int PORT = 8086;

	public static void main(String[] args) throws IOException 
		IoAcceptor acceptor = new NioSocketAcceptor();
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
		acceptor.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
		acceptor.setHandler(new TimeServerHandler());
		acceptor.getSessionConfig().setReadBufferSize(2048);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		acceptor.bind(new InetSocketAddress(PORT));
		System.out.println("启动服务在端口:"+PORT);
	

服务器端的处理Handler

import java.util.Date;

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

public class TimeServerHandler extends IoHandlerAdapter 
	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception 
		System.out.println("服务器出现错误:"+cause.getMessage());
	

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception 
		String str = message.toString();
		System.out.println("Server Received Message :"+str);
		if (str.trim().equalsIgnoreCase("quit")) 
			session.close();
			return;
		

		Date date = new Date();
		session.write(date.toString());
		System.out.println("Message written...");
	

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



主要实现功能:客户端发送消息后自动回复当前的时间给客户端

3. 客户端

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import org.apache.mina.core.future.ConnectFuture;
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.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

/**
 * 
 * @author hy
 * @createTime 2021-12-26 09:53:44
 * @description 时间的客户端,用于和当前的TimeServer进行通信用的
 *
 */
public class TimeClient 

	public static void main(String[] args) 
		IoConnector connector = new NioSocketConnector();
		connector.getFilterChain().addLast("logger", new LoggingFilter());
		connector.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
		connector.setHandler(new TimeClientHandler());
		connector.getSessionConfig().setReadBufferSize(2048);
		connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		connector.setConnectTimeoutMillis(60000L);
		InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 8086);
		// connector.setDefaultLocalAddress(inetSocketAddress);
		connector.setDefaultRemoteAddress(inetSocketAddress);
		ConnectFuture connect = connector.connect(inetSocketAddress);
		
		try 
			// 等待连接
			connect.await(60, TimeUnit.SECONDS);
			System.out.println("client connected to server...");
			IoSession session = connect.getSession();
			Scanner scanner = new Scanner(System.in);
			while (true) 
				System.out.println("请输入:");
				String line = scanner.nextLine();
				if (line == null) 
					continue;
				
				session.write(line);
				if ("quit".equals(line)) 
					break;
				
				
			
			scanner.close();
			session.closeNow();
		 catch (InterruptedException e) 
			// TODO Auto-generated catch block
			e.printStackTrace();
		
		connector.dispose();
		System.out.println("client shutdown");
	

当前内容中必须使用connect.await,否则得到的session就是null,导致后面报错

客户端的Handler

public class TimeClientHandler extends IoHandlerAdapter 

	@Override
	public void sessionCreated(IoSession session) throws Exception 
		// TODO Auto-generated method stub
		super.sessionCreated(session);
	

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception 
		// TODO Auto-generated method stub
		super.exceptionCaught(session, cause);
		session.closeNow();
	

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception 
		// TODO Auto-generated method stub
		super.messageReceived(session, message);
		System.out.println("client received message :" + message);
	


4. 测试

1.启动服务器

2.启动客户端

3.开始发送消息

测试成功

5. 总结

1. 在Apache mina中有IoConnector(客户端)和IoAcceptor(服务器)
2.服务器和客户端都遵循一定的解码规则(类似Netty),只使用一个ProtocolCodecFilter进行进站和出站的规则(Netty中需要定义进出站规则两个)

以上是关于Apache Mina:一个简单的tcp通信demo的主要内容,如果未能解决你的问题,请参考以下文章

使用mina 基于tcp协议实现客户端和服务端进行通信

大并发量socket 通信框架MINA介绍

Mina简单的入门示例

Apache Mina

Apache Mina

mina框架详解