2

android使用mina ssl握手成功,但是不能发送数据,但是可以接收数据!以下是关键代码: 服务器:

    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();
    }
}

客户:

        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.....");
    }

服务器调用public void sessionOpened(IoSession session)方法session.write android客户端可以正常发送接收到的数据。当时我在android public void sessionOpened(IoSession session)方法发送数据到服务器时无法发送。尚未调用客户端 messageSent 方法!我已经尝试过,如果我摆脱任何证书验证结束。会提示你握手失败!

4

0 回答 0