13 Aralık 2022 Salı

SSLServerSocket Sınıfı - Sunucu Tarafında Kullanılır

Giriş
Açıklaması şöyle
This class is similar to the SSLSocket class. The main difference is that SSLServerSocket is used to create sockets at server side, while SSLSocket is used to create sockets at client side.
Kullanım
Örnek
Şöyle yaparız
String[] protocols = new String[]{"TLSv1.3"};
String[] cipher_suites = new String[]{"TLS_AES_128_GCM_SHA256"};


SSLServerSocket serverSocket = null;
try {
  // Step : 1
  SSLServerSocketFactory factory =
    (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
  // Step : 2
  serverSocket = (SSLServerSocket) factory.createServerSocket(8980);
  // Step : 3
  serverSocket.setEnabledProtocols(protocols);
  serverSocket.setEnabledCipherSuites(cipher_suites);
  // Step : 4
  SSLSocket sslSocket = (SSLSocket) serverSocket.accept();
  // Step : 5
  InputStream inputStream = sslSocket.getInputStream();
  InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
  BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
  String request = null;
  while((request = bufferedReader.readLine()) != null) {
    ...
  }
} catch (Exception e) {
  ...
} finally {
  if (serverSocket != null) {
    serverSocket.close();
  }
}

Hiç yorum yok:

Yorum Gönder