blocking-client contains the socket clients used by the Blocking Echo example.
It provides two client implementations:
PlatformThreadClient- uses a platform daemon thread for the response listener.VirtualThreadClient- uses a virtual thread for the response listener.
Both clients share the same core behavior through UserClient.
common.dev.nklip.javacraft.echo.blocking.client.UserClient- Common connection lifecycle and protocol behavior.
- Connects with explicit timeout (
CONNECT_TIMEOUT_MILLIS). - Uses UTF-8 for input/output streams.
- Sends messages to server and reads responses via a bounded queue (
MAX_QUEUED_RESPONSES). - Tracks closure state with
closedByClientandclosedByServer. - Provides interactive console loop in
readUserMessages(...).
platform.dev.nklip.javacraft.echo.blocking.client.PlatformThreadClient- Starts response listener on a platform daemon thread.
virtual.dev.nklip.javacraft.echo.blocking.client.VirtualThreadClient- Starts response listener on a virtual thread.
common.dev.nklip.javacraft.echo.blocking.client.PortValidator- Validates CLI port (
1..65535) with default fallback (8075).
- Validates CLI port (
PlatformClientApplication/VirtualClientApplication- Runnable entry points for manual interactive usage.
The client sends one text line per message and expects one response line:
- Send any text -> receive
Did you say '...'? - Send
stats-> receiveSimultaneously connected clients: N - Send empty line -> receive
Please type something. - Send
bye-> receiveHave a good day!and then socket closes from server side.
Important details:
- User input is sent as entered (including meaningful surrounding spaces).
- Reads and writes are UTF-8.
sendMessage(...)fails fast when connection is unavailable.- Queue overflow closes the client to avoid unbounded memory growth.
Run tests only for this module:
mvn -pl echo/blocking/blocking-client test- Start a server (
blocking-server) on a port, for example8075. - Run one of these main classes from your IDE:
platform.dev.nklip.javacraft.echo.blocking.client.PlatformClientApplicationvirtual.dev.nklip.javacraft.echo.blocking.client.VirtualClientApplication
- Optionally pass the port as first argument (default is
8075). - Type messages in the console and finish with
bye(or EOF).