blocking-server is the blocking socket server module for the Blocking Echo example.
It accepts TCP clients, processes text-line messages, and responds with a simple echo protocol.
Two server variants are provided:
PlatformServer- per-client handler runs on platform threads.VirtualServer- per-client handler runs on virtual threads.
Both are built on the same common server core.
common.dev.nklip.javacraft.echo.blocking.server.MultithreadedServer- Accept loop and shared server lifecycle.
- Guard against duplicate
run()calls viarunningflag. - Graceful shutdown via
close()(implementsAutoCloseable):- flips running flag
- closes current
ServerSocket - unblocks
accept()
- Wraps
startUpClient(...)in baseRuntimeExceptionhandling and closes failed client sockets.
common.dev.nklip.javacraft.echo.blocking.server.ServerThread- Per-client protocol processing.
- UTF-8 input/output.
- Per-client read timeout (
setSoTimeout(2000)). - Per-server-instance connection counting through shared
AtomicInteger.
platform.dev.nklip.javacraft.echo.blocking.server.PlatformServer- Starts each
ServerThreadon a platform daemon thread.
- Starts each
virtual.dev.nklip.javacraft.echo.blocking.server.VirtualServer- Starts each
ServerThreadon a virtual thread.
- Starts each
common.dev.nklip.javacraft.echo.blocking.server.PortValidator- Validates CLI port (
1..65535) with default fallback (8075).
- Validates CLI port (
PlatformServerApplication/VirtualServerApplication- Entry points for running server manually.
For each received line:
""->Please type something.stats(case-insensitive) ->Simultaneously connected clients: Nbye(case-insensitive) ->Have a good day!and connection closes- Any other text ->
Did you say '...'?
All responses end with CRLF on the wire.
- Constructor only stores config; runtime state is initialized in
run(). - Connection counters are incremented/decremented safely in
ServerThread. - Unexpected startup failures in client handler creation are handled centrally in
MultithreadedServer. close()is idempotent; calling it when server is not running is a no-op.
Run tests only for this module:
mvn -pl echo/blocking/blocking-server testRun from your IDE using one of these main classes:
platform.dev.nklip.javacraft.echo.blocking.server.PlatformServerApplicationvirtual.dev.nklip.javacraft.echo.blocking.server.VirtualServerApplication
Optional CLI argument: port number (default 8075).