Package ch.k43.util

Klasse KSocketServer

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

public class KSocketServer extends Object implements AutoCloseable
This class provides support for a TLS-secured or non-secured socket server.

Notes:
- The constructor will load and start the KSocketServerListener thread which process all incoming client connects.
- The KSocketServerListener will start a separate KSocketServerThread for each accepted client connection.

Siehe auch:
  • Konstruktorübersicht

    Konstruktoren
    Konstruktor
    Beschreibung
    KSocketServer(int argLocalPort, Class<?> argClass)
    Start a non-TLS socket server.
    KSocketServer(int argLocalPort, Class<?> argClass, String argKeyStoreFileName, char[] argKeyStorePassword)
    Start a TLS-secured socket server.
    KSocketServer(int argLocalPort, Class<?> argClass, String argKeyStoreFileName, char[] argKeyStorePassword, String argTrustStoreFileName, char[] argTrustStorePassword)
    Start a TLS-secured socket server.
    KSocketServer(int argLocalPort, String argClassName, boolean argTLS, String argKeyStoreFileName, char[] argKeyStorePassword, String argTrustStoreFileName, char[] argTrustStorePassword)
    Start a TLS-secured or non-secured socket server.
  • Methodenübersicht

    Modifizierer und Typ
    Methode
    Beschreibung
    void
    Stop the socket server by terminating the KSocketServerListener thread.
    Return last error.
    boolean
    Return current status of the socket server.
    String representation of object.

    Von Klasse geerbte Methoden java.lang.Object

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

    • KSocketServer

      public KSocketServer(int argLocalPort, Class<?> argClass)
      Start a non-TLS socket server.

      This constructor will start the KSocketServerListener thread to wait for all incoming client connections. For each successful client connect, the passed user class will be instantiated as a separate thread to handle the client connection.

      Example:

       KSocketServer server = new KSocketServer(9999, KSocketServerThreadSample.class);
       
       KLog.abort(!server.isActive(), "Server could not be started - " + server.getLastError());
       
       ...
       
       server.close();
       
      Parameter:
      argLocalPort - Local host port
      argClass - User class handling the client connections (subclass of KSocketServerThread)
      Siehe auch:
    • KSocketServer

      public KSocketServer(int argLocalPort, Class<?> argClass, String argKeyStoreFileName, char[] argKeyStorePassword)
      Start a TLS-secured socket server.

      This constructor will start the KSocketServerListener thread to wait for all incoming client connections. For each successful client connect, the passed user class will be instantiated as a separate thread to handle the client connection.

      Example:

       KSocketServer server = new KSocketServer(9999, KSocketServerThreadSample.class, "keyfile.jks", "Pa$$w0rd".toBytes());
       KLog.abort(!server.isActive(), "Server could not be started - " + server.getLastError());
       
       ...
       
       server.close();
       
      Parameter:
      argLocalPort - Local host port
      argClass - User class handling the client socket request (must be subclass of KSocketServerThread)
      argKeyStoreFileName - Key store file name to be loaded
      argKeyStorePassword - Key store password
      Siehe auch:
    • KSocketServer

      public KSocketServer(int argLocalPort, Class<?> argClass, String argKeyStoreFileName, char[] argKeyStorePassword, String argTrustStoreFileName, char[] argTrustStorePassword)
      Start a TLS-secured socket server.

      This constructor will start the KSocketServerListener thread to wait for all incoming client connections. For each successful client connect, the passed user class will be instantiated as a separate thread to handle the client connection.

      Example:

       KSocketServer server = new KSocketServer(9999, KSocketServerThreadSample.class, "keystore.jks", "Pa$$w0rd".toBytes(), "truststore.jks", "Pa$$w0rd".toBytes());
       
       if (!server.isActive()) {
          KLog.error("Server could not be started - " + server.getLastError());
          ...
       }
       ...
       server.close();
       
       
      Parameter:
      argLocalPort - Local host port
      argClass - User class handling the client socket request (must be subclass of KSocketServerThread)
      argKeyStoreFileName - Key store file name to be loaded
      argKeyStorePassword - Key store password
      argTrustStoreFileName - Trust store file name to be loaded or null for non-TLS
      argTrustStorePassword - Trust store password or null
      Siehe auch:
    • KSocketServer

      public KSocketServer(int argLocalPort, String argClassName, boolean argTLS, String argKeyStoreFileName, char[] argKeyStorePassword, String argTrustStoreFileName, char[] argTrustStorePassword)
      Start a TLS-secured or non-secured socket server.

      This constructor will start the KSocketServerListener thread to wait for all incoming client connections. For each successful client connect, the passed user class will be instantiated as a separate thread to handle the client connection.

      Example:

       KSocketServer server = new KSocketServer(9999, "ch.k43.util.KSocketServerThreadSample", true, "keystore.jks", "Pa$$w0rd".toBytes(), "truststore.jks", "Pa$$w0rd".toBytes());
       
       if (!server.isActive()) {
          KLog.error("Server could not be started - " + server.getLastError());
          ...
       }
       ...
       server.close();
       
       
      Parameter:
      argLocalPort - Local host port
      argClassName - User class name handling the client socket requests (must be subclass of KSocketServerThread)
      argTLS - True for TLS-secured socket, false for non-secured socket
      argKeyStoreFileName - Key store file name to be loaded (TLS requirement) or null for non-TLS
      argKeyStorePassword - Key store password or null
      argTrustStoreFileName - Trust store file name to be loaded or null for non-TLS
      argTrustStorePassword - Trust store password or null
      Siehe auch:
  • Methodendetails

    • close

      public void close()
      Stop the socket server by terminating the KSocketServerListener thread. During shutdown, KSocketServerListener will itself terminate all client connections.
      Angegeben von:
      close in Schnittstelle AutoCloseable
      Siehe auch:
    • getLastError

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

      public boolean isActive()
      Return current status of the socket server.
      Gibt zurück:
      True if server is active, false otherwise
    • toString

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