Apache MINA客户端服务器

时间:2018-09-22 17:15:57

标签: java apache client-server apache-mina

我正在使用apache NIO创建聊天服务器。如果我向客户端->服务器或服务器->客户端发送一条消息,则可以正常工作,但是每当我向服务器发送多条消息时,服务器只会收到第一条消息。...如何创建该消息,以便客户端可以发送更多消息服务器和服务器接收到所有这些信息...

    package com.mina;

import java.util.Scanner;

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

public class ClientHandler extends IoHandlerAdapter {

    @Override
    public void exceptionCaught(IoSession arg0, Throwable arg1) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("In Exception Caught");

    }

    @Override
    public void inputClosed(IoSession arg0) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        System.out.println("In received" + message);
        if (session != null && message != null) {
            String retmsg = Utility.readMessageToUtility(session, message);
            System.out.println(retmsg);

            Scanner sc = new Scanner(System.in);
            String msg = sc.nextLine();
            Utility.sendMessageToUtility(session, msg);

        }

    }

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

    }

    @Override
    public void sessionClosed(IoSession arg0) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void sessionCreated(IoSession arg0) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void sessionOpened(IoSession arg0) throws Exception {
        // TODO Auto-generated method stub

    }

}

服务器:

package com.mina;

import java.net.InetSocketAddress;
import java.`util`.HashMap;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class Server implements IoHandler {
    private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(this.getClass().getName());
    HashMap<IoSession, Integer> map = new HashMap<>();

    public static void main(String[] r) throws Exception {
        Server server = new Server();
        server.connect();

    }

    public void connect() throws Exception {
        IoAcceptor acceptor = new NioSocketAcceptor();
        System.out.println(acceptor + "    acceptor");
        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
        acceptor.setHandler(this);
        acceptor.getSessionConfig().setReadBufferSize(2048);
        acceptor.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, 2);

        acceptor.bind(new InetSocketAddress("localhost", 9081));

    }

    @Override
    public void exceptionCaught(IoSession arg0, Throwable arg1) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void inputClosed(IoSession arg0) throws Exception {
        // TODO Auto-generated method stub\

    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        if (session != null && message != null) {

            ServerHandler serverhandler = new ServerHandler();

            map.put(session, serverhandler.messageHandler(session, message, map.get(session)));
        }

    }

    @Override
    public void messageSent(IoSession arg0, Object arg1) throws Exception {
        System.out.println("In messageSent    ");

    }

    @Override
    public void sessionClosed(IoSession arg0) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void sessionCreated(IoSession session) throws Exception {
        map.put(session, 1);

    }

    @Override
    public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void sessionOpened(IoSession session) throws Exception {

        String message = "Welcome and give your userName";
        Utility.sendMessageToUtility(session, message);

    }

}

0 个答案:

没有答案