Package ch.k43.util

Klasse KSocketClient

java.lang.Object
ch.k43.util.KSocketClient
Alle implementierten Schnittstellen:
AutoCloseable

public class KSocketClient extends Object implements AutoCloseable
Supported TCP socket connections to non-TLS, TLS and TLS with client authentication.

Notes:
- The remote host certificate must be present in the JVM trusted store to be authenticated.
- If client authentication is required by the host, the JKS file with the client certificate must be accessible.

  • Konstruktorübersicht

    Konstruktoren
    Konstruktor
    Beschreibung
    KSocketClient(String argHostName, int argHostPort)
    Class constructor to open TLS-secured socket to remote host.
    KSocketClient(String argHostName, int argHostPort, boolean argTLS)
    Class constructor to open TLS-secured or non-secured socket to remote host.
    KSocketClient(String argHostName, int argHostPort, boolean argTLS, String argKeyStoreFileName, char[] argKeyStorePassword)
    Class constructor to open TLS-secured or non-secured socket to remote host with client certificate.
    KSocketClient(String argHostName, int argHostPort, String argKeyFileName, char[] argKeyFilePassword)
    Class constructor to open TLS-secured socket with client authentication to remote host.
  • Methodenübersicht

    Modifizierer und Typ
    Methode
    Beschreibung
    void
    Close the socket connection.
    boolean
    Flush the output data.
    Return name of authenticated client based on the used client certificate.
    Return common name (without CN=) of the DN (distinguished name) from the used client certificate.
    Return used TCP cipher suite (Example: "TLS_AES_256_GCM_SHA384").
    Return last error
    Return used TCP connection protocol (Example: "TLSv1.3").
    boolean
    Return connection state
    boolean
    Check if data is available
    int
    read(char[] argData)
    Read socket into character array
    Read line from socket
    String representation of object.
    boolean
    write(byte[] argData)
    Write byte array to socket
    boolean
    write(char[] argData)
    Write character array to socket
    boolean
    write(String argLine)
    Write string to socket

    Von Klasse geerbte Methoden java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Konstruktordetails

    • KSocketClient

      public KSocketClient(String argHostName, int argHostPort)
      Class constructor to open TLS-secured socket to remote host.
      Parameter:
      argHostName - Remote host name
      argHostPort - Remote host port
    • KSocketClient

      public KSocketClient(String argHostName, int argHostPort, boolean argTLS)
      Class constructor to open TLS-secured or non-secured socket to remote host.
      Parameter:
      argHostName - Remote host name
      argHostPort - Remote host port
      argTLS - True for TLS-secured socket, false for non-secured socket
    • KSocketClient

      public KSocketClient(String argHostName, int argHostPort, boolean argTLS, String argKeyStoreFileName, char[] argKeyStorePassword)
      Class constructor to open TLS-secured or non-secured socket to remote host with client certificate.

      Note:
      - Use KClientSocket.isConnected() to see if the connection was established
      - For TLS client authentication, use the key store file name and password (JKS file)

      Parameter:
      argHostName - Remote host name
      argHostPort - Remote host port
      argTLS - True for TLS-secured socket, false for non-secured socket
      argKeyStoreFileName - Key store file name to be loaded or null
      argKeyStorePassword - Key store file password or null
    • KSocketClient

      public KSocketClient(String argHostName, int argHostPort, String argKeyFileName, char[] argKeyFilePassword)
      Class constructor to open TLS-secured socket with client authentication to remote host.
      Parameter:
      argHostName - Remote host name
      argHostPort - Remote host port
      argKeyFileName - Key store file name to be loaded or null
      argKeyFilePassword - Key store file password or null
  • Methodendetails

    • close

      public void close()
      Close the socket connection.
      Angegeben von:
      close in Schnittstelle AutoCloseable
    • flush

      public boolean flush()
      Flush the output data.
      Gibt zurück:
      boolean True if successful, false otherwise
    • getAuthenticatedClient

      public String getAuthenticatedClient()
      Return name of authenticated client based on the used client certificate.
      Gibt zurück:
      DN name of peer principal or null if not TLS authenticated
      Seit:
      2024.05.17
    • getAuthenticatedClientCN

      public String getAuthenticatedClientCN()
      Return common name (without CN=) of the DN (distinguished name) from the used client certificate.
      Gibt zurück:
      Common name of peer principal or null if not TLS authenticated
      Seit:
      2024.05.17
    • getCiphers

      public String getCiphers()
      Return used TCP cipher suite (Example: "TLS_AES_256_GCM_SHA384").
      Gibt zurück:
      TLS cipher suite or null
    • getLastError

      public String getLastError()
      Return last error
      Gibt zurück:
      String Error message or null
    • getProtocol

      public String getProtocol()
      Return used TCP connection protocol (Example: "TLSv1.3").
      Gibt zurück:
      TLS protocol used or null
    • isConnected

      public boolean isConnected()
      Return connection state
      Gibt zurück:
      boolean True if connected, false otherwise
    • isDataAvailable

      public boolean isDataAvailable()
      Check if data is available
      Gibt zurück:
      boolean True if data is available, false otherwise
    • read

      public int read(char[] argData)
      Read socket into character array
      Parameter:
      argData - Character buffer
      Gibt zurück:
      int Number of bytes read or -1
    • readLine

      public String readLine()
      Read line from socket
      Gibt zurück:
      String String read or null for end-of-data
    • toString

      public String toString()
      String representation of object.
      Setzt außer Kraft:
      toString in Klasse Object
      Seit:
      2024.08.23
    • write

      public boolean write(byte[] argData)
      Write byte array to socket
      Parameter:
      argData - Byte array to be written
      Gibt zurück:
      boolean True if successful, false otherwise
    • write

      public boolean write(char[] argData)
      Write character array to socket
      Parameter:
      argData - Character array to be written
      Gibt zurück:
      boolean True if successful, false otherwise
    • write

      public boolean write(String argLine)
      Write string to socket
      Parameter:
      argLine - String to be written
      Gibt zurück:
      boolean True if successful, false otherwise