9

I am doing socket communication through follwing IP address it working but no i want to do communication in ssl mode but how can I change InetAddress serverAddr = InetAddress.getByName("192.168.1.2"); to SSL.

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

     }

}

}
2
  • https is for secure HTTPS traffic. You appear to be writing a raw socket server. Just call it SSL. Commented Jul 22, 2011 at 7:36
  • You can't 'change InetAddress serverAddr = InetAddress.getByName("192.168.1.2") to SSL.` It's an IP address lookup. The question doesn't make sense. What you are looking for is javax.net.ssl.SSLSocket and friends. Commented Feb 6, 2014 at 0:22

3 Answers 3

18

Create SSLSocket instead of Socket. Rest is the same.

SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("192.168.1.2", 12345);

You may want to add aditional SSL properties. You have to do it ealier:

To authenticate the server, the client's trust store must contain the server's certificate. Client SSL with server authentication is enabled by the URL attribute ssl or the property ssl set to peerAuthentication. In addition, the system properties javax.net.ssl.trustStore and javax.net.ssl.trustStorePassword need to be set.:

System.setProperty("javax.net.ssl.trustStore","clientTrustStore.key");
System.setProperty("javax.net.ssl.trustStorePassword","qwerty");

If the server does client authentication, the client will need a key pair and a client certificate:

System.setProperty("javax.net.ssl.keyStore","clientKeyStore.key");
System.setProperty("javax.net.ssl.keyStorePassword","qwerty");
Sign up to request clarification or add additional context in comments.

3 Comments

There is no 'if the client wants to authenticate the server' about it. That's compulsory in SSL.
@user207421 Actually, it isn't.
Have a look at gpotter2.github.io/tutos/en/sslsockets, there are ways to work around System.setProperty (which is super dirty)
2

Basically you need to use SSLSocket which is for SSL communication in Java.

When creating the SSLSocket, you first need to configure the trust store which is to verify the server certificate.

Then you need to get the SSLSocket and connect to the server and then start to do handshake with the server.

Once the handshake complete successfully, you can start to exchange application data with server normally like other plain socket connection.

A HTTPS client and HTTPS server demo in Java provides a demo on how to create SSL server and SSL client in Java. It's quite useful.

Comments

0

Java has the SSLSocket class.

http://download.oracle.com/javase/1.4.2/docs/api/javax/net/ssl/SSLSocket.html

Hope this helps, haven't used it myself (yet).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.