android mina ssl can not send data but can receive data

231 Views Asked by At

android using mina ssl handshake success, but can not send data, but can receive data! The following are the key code: Server:

    public static void main(String args[]){
    try {
        SSLContext sslContext = SSLContext.getInstance("tlsv1");
        sslContext.init(getKeyManagers("server.jks", "123456"), new TrustManager[]{x509}, null);
        SocketAcceptor acceptor = new NioSocketAcceptor();
        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
        SslFilter sslFilter = new SslFilter(sslContext);
        sslFilter.setNeedClientAuth(true);
        chain.addLast("ssl", sslFilter);
        LoggingFilter loggerFilter = new LoggingFilter();
        chain.addLast("logger", loggerFilter);// log过滤器
        /**
         *I suspect that most start ProtocolCodecFilter problem, but then I tried to use TextLineCodecFactory then can not send data!
         */
        chain.addLast("codec", new ProtocolCodecFilter(new VargoProtocolCodecFactory()));
        //chain.addLast("protocal", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

        acceptor.setHandler(new TempTLSServerHandler());
        final InetSocketAddress address;
        if(args!=null && args.length>0 && args[0]!=null){
            address = new InetSocketAddress(Integer.parseInt(args[0]));
        }else {
            address = new InetSocketAddress(8012);
        }
        acceptor.bind(address);
        System.out.println("Server run as :" + address.toString());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Client:

        if (mConnector == null) {
            mConnector = new NioSocketConnector();
            mConnector.setDefaultRemoteAddress(new InetSocketAddress("192.168.1.78", 8012));
            IoHandlerListener listener = new IoHandlerListener(bean[0]);
            mConnector.setHandler(listener);
        try {
            SSLContext sslContext = SSLContext.getInstance("ssl");
            sslContext.init(getKeyManagers(mContext,"123456"),new TrustManager[] { new X509TrustManager() }, new SecureRandom());
            SslFilter sslFiler = new SslFilter(sslContext);
            sslFiler.setNeedClientAuth(false);
            sslFiler.setUseClientMode(true);
            mConnector.getFilterChain().addLast("SslFilter",sslFiler);
            //mConnector.getFilterChain().addLast("protocal", new ProtocolCodecFilter(new VargoProtocolCodecFactory()));// vargo协议Filter
            mConnector.getFilterChain().addLast("protocal", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
            Log.i(LOG_TAG, "connect server error.....");
        }  catch (IOException e) {
            e.printStackTrace();
            Log.i(LOG_TAG, "connect server error.....");
        }
    }
    ConnectFuture connectFuture = mConnector.connect();
    connectFuture.awaitUninterruptibly();
    IoSession session = connectFuture.getSession();
    if (session != null && session.isConnected()) {
        Log.i(LOG_TAG, "connect server success.....");
    } else {
        Log.i(LOG_TAG, "connect server error.....");
    }

Server called public void sessionOpened (IoSession session) method session.write android client can send data received normally. At that time when I was in the android public void sessionOpened (IoSession session) method to send data to the server could not be sent. Client messageSent methods have not been called! I have tried, if I get rid of any certificate validation end. You will be prompted handshake failed!

0

There are 0 best solutions below