Creating an SSL Client Program

It's even easier to convert a client program to SSL because you don't need to worry about the key store. You just create an SSLSocketFactory and ask the factory to create an SSLSocket object.

Listing 35.5 shows an excerpt from the ChatClient class from Chapter 27.

Code Listing 35.5. Excerpt from ChatClient.java
        try
        {
// Connect to the server
            Socket sock = new Socket(host, portNumber);

Listing 35.6 shows the SSL version of the same section of code.

Code Listing 35.6. Exception from SSL Version of ChatClient.java
        try
        {
// Connect to the server
            SSLSocketFactory sslFactory =
                (SSLSocketFactory) SSLSocketFactory.getDefault();

            SSLSocket sock = (SSLSocket) sslFactory.
                createSocket(host, portNumber);

Once again, the ...

Get Special Edition Using Java™ 2 Enterprise Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.