Class KSocketClient
java.lang.Object
ch.k43.util.KSocketClient
- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionKSocketClient(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 TypeMethodDescriptionvoidclose()Close the socket connection.booleanflush()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 errorReturn used TCP connection protocol (Example: "TLSv1.3").booleanReturn connection statebooleanCheck if data is availableintread(char[] argData) Read socket into character arrayreadLine()Read line from sockettoString()String representation of object.booleanwrite(byte[] argData) Write byte array to socketbooleanwrite(char[] argData) Write character array to socketbooleanWrite string to socket
-
Constructor Details
-
KSocketClient
Class constructor to open TLS-secured socket to remote host.- Parameters:
argHostName- Remote host nameargHostPort- Remote host port
-
KSocketClient
Class constructor to open TLS-secured or non-secured socket to remote host.- Parameters:
argHostName- Remote host nameargHostPort- Remote host portargTLS- 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 nameargHostPort- Remote host portargTLS- True for TLS-secured socket, false for non-secured socketargKeyStoreFileName- Key store file name to be loaded or nullargKeyStorePassword- 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 nameargHostPort- Remote host portargKeyFileName- Key store file name to be loaded or nullargKeyFilePassword- Key store file password or null
-
-
Method Details
-
close
-
flush
public boolean flush()Flush the output data.- Returns:
- boolean True if successful, false otherwise
-
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
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
Return used TCP cipher suite (Example: "TLS_AES_256_GCM_SHA384").- Returns:
- TLS cipher suite or null
-
getLastError
-
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
-
toString
-
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
Write string to socket- Parameters:
argLine- String to be written- Returns:
- boolean True if successful, false otherwise
-