2

Possible Duplicate:
how to do ssl socket programming

I am doing communication from android as client and laptop as server through Socket using wireless router.But how if i want to do communication securely. here is my android code in eclipse

public class TCPClient implements Runnable {

    public void run() {

     try {

         InetAddress serverAddr = InetAddress.getByName("192.168.1.2");

             Log.d("TCP", "C: Connecting...");

             Socket socket = new Socket(serverAddr,12345);

             String message = "Hello from Client android emulator";
              try {

                     Log.d("TCP", "C: Sending: '" + message + "'");

                     PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);

                     out.println(message);

                     Log.d("TCP", "C: Sent.");

                 Log.d("TCP", "C: Done.");



         } catch(Exception e) {

             Log.e("TCP", "S: Error", e);
                 } finally {

                    socket.close();

                  }
     } catch (Exception e) {

          Log.e("TCP", "C: Error", e);

     }

}

}

here is server code in Netbean that is working and communicating with android. what changes i have to do ?

public class TCPDesktopServer implements Runnable{



public static final String SERVERIP = "10.0.2.15";

public static final int SERVERPORT = 12345;



public void run() {

     try {

        System.out.println("S: Connecting...");

        ServerSocket serverSocket = new ServerSocket(SERVERPORT);

         while (true) {

             Socket client = serverSocket.accept();

             System.out.println("S: Receiving...");

             try {
                  BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

                  String str = in.readLine();

                  System.out.println("S: Received: '" + str + "'");
             } catch(Exception e) {

                    System.out.println("S: Error");

                    e.printStackTrace();

             } finally {
                    client.close();
                    System.out.println("S: Done.");
                }
         }

     } catch (Exception e) {

         System.out.println("S: Error");

         e.printStackTrace();
     }
}

public static void main (String a[]) {

    Thread desktopServerThread = new Thread(new TCPDesktopServer());

    desktopServerThread.start();

}

}

Community
  • 1
  • 1
Qaiser Mehmood
  • 975
  • 6
  • 21
  • 33

2 Answers2

0

Seems like you need to use javax.crypto.CipherOutputStream/CipherInputStream, or if you want a more standardized approach you could try Apache commons (http://commons.apache.org/) where there is a basic implementation of SSL.

Simone Gianni
  • 11,426
  • 40
  • 49
0

You could very easily use the SSLSocket class which is part of the base java distribution. If you are having problems getting certificates to validate, you can user this tutorial to show you how to allow self-signed certs and non-matching host names.

Deven Phillips
  • 1,129
  • 14
  • 39