0

hi I'm new on Android platform. I need to write a TCP/SSL client class in my app, which download text file with some c# server. I have created .cer file and bks file for ssl communication . Indeed I need to use socket class communication instead of HTTP or HTTPS communication. My android code is:

public class MainActivity extends Activity {
static String filename = null;
static Socket socket = null;
static Boolean flag = true;
Button GetServerData, Upload;
TextView textPort;
EditText et;
Context context;static final int SocketServerPORT = 8889;
static final String SocketServerIP = "169.254.80.80";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button GetServerData = (Button) findViewById(R.id.GetServerData);

    GetServerData.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Intent i = new Intent(MainActivity.this, MyHttpClient.class);
            // startActivity(i);
            ClientRxThread clientRxThread = new ClientRxThread(
                    SocketServerIP, SocketServerPORT);

            clientRxThread.start();}
    });

}private class ClientRxThread extends Thread {
    String dstAddress;
    int dstPort;

    ClientRxThread(String address, int port) {
        dstAddress = address;
        dstPort = port;
    }

    @Override
    public void run() {
        // Socket socket = null;
        InputStream in;
        int bufferSize = 0;

        try {

            socket = new Socket(dstAddress, dstPort);

            File file = new File(Environment.getExternalStorageDirectory(),
                    "test.txt");
            bufferSize = socket.getReceiveBufferSize();
            in = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream output = new BufferedOutputStream(fos);
            DataInputStream clientData = new DataInputStream(in);
            byte[] buffer = new byte[bufferSize];
            int read;
            while ((read = clientData.read(buffer)) > 0) {// != -1) {
                output.write(buffer, 0, read);
            }
            output.flush();
            output.close();
            socket.close();

            MainActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, "Finished",
                            Toast.LENGTH_LONG).show();
                }});
        } catch (IOException e) {

            e.printStackTrace();

            final String eMsg = "Something wrong: " + e.getMessage();
            MainActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, eMsg,
                            Toast.LENGTH_LONG).show();
                }
            });

        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}}
Hadi
  • 91
  • 1
  • 9
  • looks like you forgot to post your code. – EJK Jul 08 '15 at 21:04
  • What is your question? Which file transfert protocol are you using? SCP? SFTP? – BladeCoder Jul 08 '15 at 21:35
  • my c# server send text file to my android client. my server programming by socket and secure by ssl, but my android app need to be ssl secure by socket instead of http – Hadi Jul 08 '15 at 22:33

1 Answers1

0

Have you tried the SSLSocket, the usage of it is just like the ordinary Socket.

private void initSslSocketFactory() {

    try {
        /*
         * SETUP TRUSTSTORE 
         */  
        KeyStore trustStore = KeyStore.getInstance("BKS");  
        TrustManagerFactory trustManagerFactory = TrustManagerFactory  
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());  
        InputStream trustStoreStream = context.getResources()  
            .openRawResource(R.raw.ca);  
        trustStore.load(trustStoreStream, "000000".toCharArray());  
        trustManagerFactory.init(trustStore);  

        /* 
         * SETUP KEYSTORE 
         */  
        KeyStore keyStore = KeyStore.getInstance("PKCS12");  
        KeyManagerFactory keyManagerFactory = KeyManagerFactory  
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());  
        InputStream keyStoreStream = context.getResources()  
            .openRawResource(R.raw.client);  
        keyStore.load(keyStoreStream, "000000".toCharArray());  
        keyManagerFactory.init(keyStore, "000000".toCharArray());  

        /* 
         * SETUP the SSL context to use the truststore and keystore 
         */   
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
        mSslSocketFactory = sslContext.getSocketFactory();
    } catch (NoSuchAlgorithmException e) {
        Log.e(Tag, "" + e.getMessage());
        e.printStackTrace();
    } catch (KeyManagementException e) {
        Log.e(Tag, "" + e.getMessage());
        e.printStackTrace();
    }
}

public void connect() {
    mSslSock = (SSLSocket) mSslSocketFactory.createSocket();
    mSslSock.connect(new InetSocketAddress(host, port));
    mSslSock.setSoTimeout(SOCKET_TIMEOUT);
    mSslSock.setUseClientMode(true);
    mSslSock.setEnabledCipherSuites(mSslSock.getEnabledCipherSuites());
    mSslSock.setEnabledProtocols(mSslSock.getSupportedProtocols());
    mSslSock.connect(SocketAddress);
    ......
}
wshztlg
  • 181
  • 1
  • 7
  • thank you, but where add my bks file? and is there simple tutorial show me how to do this? – Hadi Jul 09 '15 at 11:57
  • This may help you too, http://just-another-blog.net/programming/android-create-ssl-socket-over-ssl-socket-with-client-auth/, http://stackoverflow.com/questions/6755180/java-ssl-connect-add-server-cert-to-keystore-programatically – wshztlg Jul 10 '15 at 02:44