MINA框架

  • 一个简单易用的基于TCP/IP通信的JAVA框架

  • 下载地址

  • 开发一个Mina应用,简单来说,就是创建连接、设定过滤规则、编写自己的消息处理器

使用

  • 引入项目并添加到库中
    在这里插入图片描述
  • 代码示例
    • 结构目录
      在这里插入图片描述
    • 代码
public class Client {
    public static void main(String[] args) {

        //创建连接
        NioSocketConnector connector = new NioSocketConnector();
        DefaultIoFilterChainBuilder chain = connector.getFilterChain();

        chain.addLast("objectFilter",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));

        connector.setHandler(new MinaClientHandler());
        connector.setConnectTimeoutMillis(10000);

        //连接服务器
        ConnectFuture cf = connector.connect(new InetSocketAddress("localhost",9866));
        cf.awaitUninterruptibly();  //等待连接成功
        Scanner input = new Scanner(System.in);

        while(true){
            //以对象的方式传输数据
            Message msg = new Message();
            System.out.println("from:");
            msg.setFrom(input.nextLine());
            System.out.println("to:");
            msg.setTo(input.nextLine());
            System.out.println("info:");
            msg.setInfo(input.nextLine());
            msg.setType("send");

            cf.getSession().write(msg);
        }

        //等待服务器连接关闭,结束长连接
        //cf.getSession().getCloseFuture().awaitUninterruptibly();
        //connector.dispose();   //关闭连接

    }
}
public class MinaClientHandler extends IoHandlerAdapter {
    @Override
    public void sessionOpened(IoSession session) throws Exception {
        super.sessionOpened(session);
        System.out.println("sessionOpened");
    }

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

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        super.messageReceived(session, message);
        Message msg  = (Message) message;
        System.out.println(msg);
    }
}
public class Message implements Serializable {

    private String from;
    private String to;
    private String type;
    private String info;

    public Message() {
    }

    public Message(String from, String to, String type, String info) {
        this.from = from;
        this.to = to;
        this.type = type;
        this.info = info;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Message{" +
                "from='" + from + '\'' +
                ", to='" + to + '\'' +
                ", type='" + type + '\'' +
                ", info='" + info + '\'' +
                '}';
    }
}

public class Server {
    public static void main(String[] args) {

        //创建一个非阻塞的Server端Socket NIO
        SocketAcceptor acceptor = new NioSocketAcceptor();
        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();

        //设定一个过滤器,一行一行的读取数据
        //chain.addLast("myChin",new ProtocolCodecFilter(new TextLineCodecFactory()));
        //设定过滤器以对象为单位读取数据
        chain.addLast("objectFilter",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
        //设置服务器端的消息处理器
        acceptor.setHandler(new MinaServerHandler());
        int port = 9866;  //服务器的端口号

        try {
            //绑定端口号,启动服务器(不会阻塞,立即返回)
            acceptor.bind(new InetSocketAddress(port));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("Mina Server running,listener on:"+port);
    }
}

public class MinaServerHandler  extends IoHandlerAdapter {
    @Override
    public void sessionOpened(IoSession session) throws Exception {
        super.sessionOpened(session);
        System.out.println("welcome client" + session.getRemoteAddress());
    }

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

    //接收消息
    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        super.messageReceived(session, message);

        //String msg = (String) message;
        Message msg = (Message) message;

        System.out.println("收到客户端发来的消息:"+msg);
        msg.setInfo("吃好吃的");
        session.write(msg);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值