关于简单MINA客户端和服务器的问题

发布于 2024-11-16 07:50:02 字数 3797 浏览 1 评论 0原文

我只是想创建一个简单的 MINA 服务器和客户端来评估。这是我的代码。

public class Server {

private static final int PORT = 8080;

static class ServerHandler extends IoHandlerAdapter {
    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        cause.printStackTrace();
    }

    @Override
    public void sessionCreated(IoSession session) {
        System.out.println("session is created");
        session.write("Thank you");
    }

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

    @Override
    public void messageReceived(IoSession session, Object message) {
        System.out.println("message=" + message);
        session.write("Reply="+message);
    }
}

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    SocketAcceptor acceptor = new NioSocketAcceptor();
    acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
    acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
    acceptor.setHandler(new Server.ServerHandler());
    acceptor.getSessionConfig().setReadBufferSize( 2048 );
    acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
    acceptor.bind(new InetSocketAddress(PORT));
    System.out.println("Listening on port " + PORT);
    for (;;) {
        Thread.sleep(3000);
    }
}

当我执行此代码

public class Client {
private static final int PORT = 8080;

private IoSession session;
private ClientHandler handler;
public Client() {
    super();
}

public void initialize() throws Exception {
    handler = new ClientHandler();
    NioSocketConnector connector = new NioSocketConnector();
    connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
    connector.getFilterChain().addLast("logger", new LoggingFilter());
    connector.setHandler(handler);
    for (;;) {
        try {
            ConnectFuture future = connector.connect(new InetSocketAddress(PORT));
            future.awaitUninterruptibly();
            session = future.getSession();
            break;
        } catch (RuntimeIoException e) {
            System.err.println("Failed to connect.");
            e.printStackTrace();
            Thread.sleep(5000);
        }
    }
    if (session == null) {
        throw new Exception("Unable to get session");
    }
    Sender sender = new Sender();
    sender.start();

    session.getCloseFuture().awaitUninterruptibly();
    connector.dispose();
    System.out.println("client is done.");
}

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    Client client = new Client();
    client.initialize();
}

class Sender extends Thread {
    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        handler.messageSent(session, "message");
    }
}

class ClientHandler extends IoHandlerAdapter {

    @Override
    public void sessionOpened(IoSession session) {

    }

    @Override
    public void messageSent(IoSession session, Object message) {
        System.out.println("message sending=" + message);
        session.write(message);
    }

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

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) {
        cause.printStackTrace();
    }
}

时,

客户端似乎一直在发送消息,而不是在发送后停止。在我看来,底层 MINA 代码中有一个递归调用。我知道我做错了。

有人可以告诉我如何解决这个问题吗?

谢谢。

I am just trying to create a simple MINA server and client to evaluate. Here is my code.

public class Server {

private static final int PORT = 8080;

static class ServerHandler extends IoHandlerAdapter {
    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        cause.printStackTrace();
    }

    @Override
    public void sessionCreated(IoSession session) {
        System.out.println("session is created");
        session.write("Thank you");
    }

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

    @Override
    public void messageReceived(IoSession session, Object message) {
        System.out.println("message=" + message);
        session.write("Reply="+message);
    }
}

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    SocketAcceptor acceptor = new NioSocketAcceptor();
    acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
    acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
    acceptor.setHandler(new Server.ServerHandler());
    acceptor.getSessionConfig().setReadBufferSize( 2048 );
    acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
    acceptor.bind(new InetSocketAddress(PORT));
    System.out.println("Listening on port " + PORT);
    for (;;) {
        Thread.sleep(3000);
    }
}

}

public class Client {
private static final int PORT = 8080;

private IoSession session;
private ClientHandler handler;
public Client() {
    super();
}

public void initialize() throws Exception {
    handler = new ClientHandler();
    NioSocketConnector connector = new NioSocketConnector();
    connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
    connector.getFilterChain().addLast("logger", new LoggingFilter());
    connector.setHandler(handler);
    for (;;) {
        try {
            ConnectFuture future = connector.connect(new InetSocketAddress(PORT));
            future.awaitUninterruptibly();
            session = future.getSession();
            break;
        } catch (RuntimeIoException e) {
            System.err.println("Failed to connect.");
            e.printStackTrace();
            Thread.sleep(5000);
        }
    }
    if (session == null) {
        throw new Exception("Unable to get session");
    }
    Sender sender = new Sender();
    sender.start();

    session.getCloseFuture().awaitUninterruptibly();
    connector.dispose();
    System.out.println("client is done.");
}

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    Client client = new Client();
    client.initialize();
}

class Sender extends Thread {
    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        handler.messageSent(session, "message");
    }
}

class ClientHandler extends IoHandlerAdapter {

    @Override
    public void sessionOpened(IoSession session) {

    }

    @Override
    public void messageSent(IoSession session, Object message) {
        System.out.println("message sending=" + message);
        session.write(message);
    }

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

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) {
        cause.printStackTrace();
    }
}

}

When I execute this code, the Client seems to keep sending a message instead of stopping after it sends. It looks to me that there is a recursive call in underlying MINA code. I know that I am doing something wrong.

Can somebody tell me how to fix this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下伊人醉 2024-11-23 07:50:02

尝试初始化并启动您的 sender 并在 sessionOpened (ClientHandler) 中使用 session

Try to initialize and start your sender and use the session within sessionOpened (ClientHandler)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文