Java Reference
In-Depth Information
Solution
Make use of the Server Name Indication (SNI) extension to determine what server
name a client is attempting to connect to during a handshake. The SNI extension is
used in the handshake process to specify which host a client is attempting to connect
to. In the following example, a client is created that indicates a server host for a valid
connection in its SSL parameter list.
public class SocketClient {
public static Socket socket = null;
public static PrintWriter out;
public static BufferedReader in;
public static void main(String[] args) {
createConnection("gmail.com",443);
}
public static void createConnection(String host, int
port) {
try {
SSLSocketFactory factory
= HttpsURLConnection.getDefaultSSLSocketFactory();
SSLSocket sslSocket = (SSLSocket)
factory.createSocket(host, port);
SNIHostName serverName = new
SNIHostName(host);
List<SNIServerName> serverNames = new
ArrayList<>(1);
serverNames.add(serverName);
SSLParameters params
= sslSocket.getSSLParameters();
params.setServerNames(serverNames);
sslSocket.setSSLParameters(params);
//Create socket connection
Search WWH ::




Custom Search