Class KSocketClient

java.lang.Object
ch.k43.util.KSocketClient
All Implemented Interfaces:
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.

  • Constructor Summary

    Constructors
    Constructor
    Description
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    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

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • KSocketClient

      public KSocketClient(String argHostName, int argHostPort)
      Class constructor to open TLS-secured socket to remote host.
      Parameters:
      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.
      Parameters:
      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)

      Parameters:
      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.
      Parameters:
      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
  • Method Details

    • close

      public void close()
      Close the socket connection.
      Specified by:
      close in interface AutoCloseable
    • flush

      public boolean flush()
      Flush the output data.
      Returns:
      boolean True if successful, false otherwise
    • getAuthenticatedClient

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

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

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

      public String getLastError()
      Return last error
      Returns:
      String Error message or null
    • getProtocol

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

      public boolean isConnected()
      Return connection state
      Returns:
      boolean True if connected, false otherwise
    • isDataAvailable

      public boolean isDataAvailable()
      Check if data is available
      Returns:
      boolean True if data is available, false otherwise
    • read

      public int read(char[] argData)
      Read socket into character array
      Parameters:
      argData - Character buffer
      Returns:
      int Number of bytes read or -1
    • readLine

      public String readLine()
      Read line from socket
      Returns:
      String String read or null for end-of-data
    • write

      public boolean write(byte[] argData)
      Write byte array to socket
      Parameters:
      argData - Byte array to be written
      Returns:
      boolean True if successful, false otherwise
    • write

      public boolean write(char[] argData)
      Write character array to socket
      Parameters:
      argData - Character array to be written
      Returns:
      boolean True if successful, false otherwise
    • write

      public boolean write(String argLine)
      Write string to socket
      Parameters:
      argLine - String to be written
      Returns:
      boolean True if successful, false otherwise
    • toString

      public String toString()
      String representation of object.
      Overrides:
      toString in class Object
      Since:
      2024.08.23