Change your Java code to use socket factories

To use secure sockets layer (SSL) with your existing code, you must first change your code to use socket factories.

To change your code to use socket factories, perform the following steps:

  1. Add this line to your program to import the SocketFactory class:
    import javax.net.*;
  2. Add a line that declares an instance of a SocketFactory object. For example:
    SocketFactory socketFactory
  3. Initialize the SocketFactory instance by setting it equal to the method SocketFactory.getDefault(). For example:
    socketFactory = SocketFactory.getDefault();
    The whole declaration of the SocketFactory should look like this:
    SocketFactory socketFactory = SocketFactory.getDefault();
  4. Initialize your existing sockets. Call the SocketFactory method createSocket(host,port) on your socket factory for each socket you declare.
    Your socket declarations should now look like this:
    Socket s = socketFactory.createSocket(host,port);
    Where:
    • s is the socket that is being created.
    • socketFactory is the SocketFactory that was created in step 2.
    • host is a string variable that represents the name of a host server.
    • port is an integer variable that represents the port number of the socket connection.

When you have completed all of these steps, your code uses socket factories. You do not need to make any other changes to your code. All of the methods that you call and all the syntax with your sockets still work.

See Examples: Change your Java™ code to use server socket factories for an example of a client program being converted to use socket factories.

See Example: Change your Java code to use client socket factories for an example of a client program being converted to use socket factories.

Related concepts
Select a digital certificate to use
Use the digital certificate when you run your Java application
Related tasks
Prepare iSeries server for secure sockets layer support
Change your Java code to use secure sockets layer