通过tcp / ip连接的ssl套接字无法发送/接收

时间:2014-05-28 09:13:49

标签: java sockets ssl tcp

我正在构建一个需要使用tcp连接到控制器的Android应用程序与ssl,一切正常,没有ssl尝试它,但一旦我实现SSLSocket连接似乎工作(握手工作,等等)但发送相同字节我得不到回应。我不知道我做错了什么,因为这是完全相同的代码并且套接字是连接的......

任何帮助将不胜感激。

我的getSocket函数:

public SSLSocket getSocket(String ip) throws IOException {
        TrustManagerFactory trustManagerFactory = null;
        try {
            KeyStore trustStore = KeyStore.getInstance("BKS");  
            trustManagerFactory = TrustManagerFactory  
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());  
            App app = new App();
            InputStream trustStoreStream = app.getContext().getResources()  
                .openRawResource(R.raw.truststore);  
            trustStore.load(trustStoreStream, "mypassword".toCharArray());  
            trustManagerFactory.init(trustStore);

            outerSSLContext = SSLContext.getInstance("TLS");  
            outerSSLContext.init(null, trustManagerFactory.getTrustManagers(), null); 

            SSLSocketFactory outerSSLSocketFactory = (SSLSocketFactory) outerSSLContext  
                    .getSocketFactory();  
                 SSLSocket outerSocket = (SSLSocket) outerSSLSocketFactory.createSocket();
                 outerSocket.setSoTimeout(Constants.SOCKET_READ_TIMEOUT);

                 InetAddress serverAddr = InetAddress.getByName(ip);
                InetSocketAddress address = new InetSocketAddress(serverAddr, Integer.parseInt(serverPort));
                outerSocket.connect(address, Constants.SOCKET_CONNECTION_TIMEOUT);

                 socket = outerSocket;
                 socket.startHandshake();
         } catch (KeyStoreException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (NoSuchAlgorithmException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (NotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (CertificateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (KeyManagementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        return socket;
    }

使用套接字的随机函数:

int length = 0;
        byte[] recievedBytes = null;

        if (socket != null){
            try {
                DataOutputStream os = new DataOutputStream(socket.getOutputStream());
                os.write(bytes);


                DataInputStream is = new DataInputStream(socket.getInputStream());
                int tries = 0;
                while (tries < 20 && length == 0) {
                    length = is.available();
                    Thread.sleep(Constants.RESPONSE_DELAY);
                    tries++;
                }

                recievedBytes = new byte[length];
                is.readFully(recievedBytes);

                is.close();
                os.close();
                socket.close();

                if (recievedBytes.length > 0) {
                    String recievedBString = "";
                    for (int i = 0; i < recievedBytes.length; i++) {
                        recievedBString += String.valueOf(recievedBytes[i]) + ", ";
                    }
                    System.out.println(recievedBString);
                    } else {
                        System.out.println("nothing came back");
return null;
                }

0 个答案:

没有答案