SSL Server Socket
In this chapter you will learn:
Creating an SSL Server Socket
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*j av a 2 s.com*/
import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocketFactory;
public class Main {
public static void main(String[] argv) throws Exception {
int port = 443;
ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault();
ServerSocket ssocket = ssocketFactory.createServerSocket(port);
Socket socket = ssocket.accept();
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
// Read from in and write to out...
in.close();
out.close();
}
}
Get Supported Cipher Suites
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
// j av a 2 s . c om
public class Main {
public static void main(String[] argv) throws Exception {
SSLServerSocketFactory factory =
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket serverSocket =
(SSLServerSocket) factory.createServerSocket(8080);
String[] suites = serverSocket.getSupportedCipherSuites();
for (int i = 0; i < suites.length; i++) {
System.out.println(suites[i]);
}
serverSocket.setEnabledCipherSuites(suites);
String[] protocols = serverSocket.getSupportedProtocols();
for (int i = 0; i < protocols.length; i++) {
System.out.println(protocols[i]);
}
SSLSocket socket = (SSLSocket) serverSocket.accept();
socket.startHandshake();
System.out.println(socket.getRemoteSocketAddress());
}
}
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Socket