Java Reference
In-Depth Information
import java.net.*;
import javax.net.ssl.*;
import java.io.*;
public class SSLClientDemo
{
public static void main(String [] args)
{
String host = args[0];
int port = Integer.parseInt(args[1]);
try
{
System.out.println(“Locating socket factory for SSL...”);
SSLSocketFactory factory =
(SSLSocketFactory) SSLSocketFactory.getDefault();
System.out.println(“Creating secure socket to “
+ host + “:” + port);
SSLSocket socket =
(SSLSocket) factory.createSocket(host, port);
System.out.println(“Enabling all available cipher
suites...”);
String [] suites = socket.getSupportedCipherSuites();
socket.setEnabledCipherSuites(suites);
System.out.println(“Registering a handshake
listener...”);
socket.addHandshakeCompletedListener(
new MyHandshakeListener());
System.out.println(“Starting handshaking...”);
socket.startHandshake();
System.out.println(“Just connected to “
+ socket.getRemoteSocketAddress());
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Let me make a few comments about the SSLClientDemo program:
The getDefault() static method of SSLSocketFactory is used to obtain a
reference to the default secure socket factory.
■■
The socket factory is used to create an SSLSocket on the localhost at
port 5002. In this example, the server program is the SSLServerDemo
program discussed earlier.
■■
The client and server need a common cipher suite, so the client needs to
enable at least one cipher suite that the server understands. I just
enabled all of them in this example and let the underlying implementa-
tion choose a cipher suite.
■■
Search WWH ::




Custom Search