我是靠谱客的博主 简单发箍,最近开发中收集的这篇文章主要介绍简单的mina应用简单的mina应用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

MINA入门

  • 简单的mina应用
    • 简单的mina服务端
    • 服务端代码
    • 简单的mina客户端
    • 客户端代码

简单的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");
}
}

最后

以上就是简单发箍为你收集整理的简单的mina应用简单的mina应用的全部内容,希望文章能够帮你解决简单的mina应用简单的mina应用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部