1

What could be the problem with Apache MINA if it won't call messageSent() method after writing to active session? My code works perfectly in simple Java client but doesn't work in Android application.

TCPClient's code:

if (session != null && session.isConnected()) {
            throw new IllegalStateException("Already connected. Disconnect first.");
        }

        connector = new NioSocketConnector();
        connector.getSessionConfig().setUseReadOperation(true);

        try {
            SslFilter sslFilter = new SslFilter(ClientSslContextFactory.getInstance());
            sslFilter.setUseClientMode(true);
            connector.getFilterChain().addFirst("sslFilter", sslFilter);

            handler = new TCPHandler();
            connector.setHandler(handler);
            connector.getSessionConfig().setReadBufferSize(4096);

            // try to connect to server
            Log.d(TAG, "Connecting...");
            ConnectFuture future = connector.connect(new InetSocketAddress(Const.hostIP, Const.sessionPort));
            future.addListener(new IoFutureListener<IoFuture>() {

                public void operationComplete(IoFuture future) {
                    ConnectFuture connFuture = (ConnectFuture) future;
                    if(connFuture.isConnected()){
                        Log.d(TAG, "Assigning session...");
                        session = future.getSession();

                    } else {
                        Log.e(TAG, "Not connected...exiting");
                    }
                }
            });
            future.awaitUninterruptibly();
        } catch (RuntimeIoException e) {
            Log.e(TAG, "Failed to connect.");
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "Failed to connect. Illegal Argument! Terminating program!");
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            Log.e(TAG, "Failed to set SSL filter!");
            e.printStackTrace();
        }

TCPHandler's sending code:

@Override
    public void sessionOpened(IoSession session) throws Exception {
        super.sessionOpened(session);
        Log.d(TAG, "Session opened...");

        byte[] ba = ("stringasdfasdf\n").getBytes();
        IoBuffer buffer = IoBuffer.allocate(ba.length);
        buffer.put(ba);
        buffer.flip();
        session.write(buffer);
        Log.d(TAG, "TCP writing executed.");
    }

2 Answers 2

1

I have the same issue and notice that it is important:

@Override
public void sessionCreated(IoSession session) throws Exception {
    Log.d(TAG, "sessionCreated");
    session.setAttribute(SslFilter.USE_NOTIFICATION);
}

Once session.setAttribute(SslFilter.USE_NOTIFICATION) is added. It works.

Sign up to request clarification or add additional context in comments.

Comments

0

connector.connect can not run in the main thread in Android. Are you sure the session is open?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.