Commit bbd1873
committed
feat(reverse-echo): add multithreaded TCP reverse-echo server and client example with I/O details
What
- Added ReverseEcho: a multithreaded TCP server that accepts multiple client connections on port 6000.
- Each client is handled by a dedicated Thread subclass (ReverseEcho) which:
- uses BufferedReader(InputStreamReader(socket.getInputStream())) to read text lines from client,
- uses PrintStream(socket.getOutputStream()) to send text responses,
- reverses each incoming line using StringBuilder.reverse() and returns it back to the client,
- uses a simple sentinel "dne" to terminate per-connection loop and close the socket.
- Included an inner Client example showing how to connect to the server, send keyboard input, and receive reversed responses.
- Documented why BufferedReader is used, how the server accepts connections in an infinite loop, and where to change the address/port.
Why
- Demonstrates core TCP server patterns:
- ServerSocket.accept() to accept incoming connections,
- per-connection thread model to enable concurrent clients,
- use of character streams (BufferedReader) for line-oriented protocols,
- use of PrintStream for simple line output.
- Educational example for learning blocking I/O, thread-per-connection model, and simple request/response protocols.
- Provides a minimal, clear pattern to build or test line-based services (chat bots, small REPL servers, command servers).
How to use
1. Start the server:
- Run ReverseEcho.main(). Server listens on port 6000 and prints connection counts.
2. Start a client:
- Replace the placeholder IP in Client.main() with server IP (for local tests use "localhost").
- Run Client.main(). Type lines at the keyboard; the server will echo each line reversed.
3. Protocol details:
- Client sends newline-terminated text lines.
- Server responds with newline-terminated reversed text lines.
- Send the exact string "dne" (without quotes) to terminate the session from either side (server closes socket on "dne").
4. Example session:
- Client: Hello
- Server: olleH
- Client: world
- Server: dlrow
- Client: dne
- Connection closed.
Real-life applications
- Teaching and testing TCP basics, socket programming and stream I/O.
- Lightweight text-processing backends (e.g., simple command processors, protocol simulators).
- Local development tools for debugging and learning about concurrency, timeouts and network flows.
Notes & recommendations
- BufferedReader is wrapped around InputStreamReader to efficiently read lines and handle character decoding.
- PrintStream is convenient for println() but consider using BufferedWriter/PrintWriter with explicit flushing for production code.
- Current server uses one thread per client. For many simultaneous clients, prefer a thread pool (ExecutorService) or NIO (java.nio) for scalability.
- The code currently swallows exceptions silently in run(); add proper logging and resource cleanup.
- The sentinel "dne" is a simple protocol choice for demo purposes—use formal protocol framing and validation for production systems.
- Always validate input and consider timeouts (socket read timeout) to prevent blocked threads.
- Consider graceful shutdown handling for ServerSocket (e.g., listening flag + close) instead of infinite loop.
- Security: do not trust client input; avoid exposing sensitive operations over plain TCP. Use TLS (SSLSocket) for encrypted communication in real deployments.
Possible improvements (next steps)
- Replace per-connection Thread with ExecutorService to limit max threads.
- Add socket timeouts and per-connection exception handling/logging.
- Switch to non-blocking NIO for high-concurrency use cases.
- Add graceful server shutdown hook and connection tracking.
- Replace sentinel-based termination with a formal request/response code or protocol.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 90fe260 commit bbd1873
File tree
1 file changed
+26
-16
lines changed- Section27NetworkProgramming/ReverseEchoServerMultipleClient/src/MultiClient
1 file changed
+26
-16
lines changedLines changed: 26 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
| 24 | + | |
24 | 25 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
30 | 36 | | |
| 37 | + | |
31 | 38 | | |
32 | 39 | | |
33 | 40 | | |
| |||
49 | 56 | | |
50 | 57 | | |
51 | 58 | | |
52 | | - | |
| 59 | + | |
| 60 | + | |
53 | 61 | | |
54 | 62 | | |
55 | 63 | | |
56 | 64 | | |
57 | | - | |
| 65 | + | |
58 | 66 | | |
59 | | - | |
60 | | - | |
| 67 | + | |
| 68 | + | |
61 | 69 | | |
62 | 70 | | |
63 | | - | |
| 71 | + | |
| 72 | + | |
64 | 73 | | |
65 | 74 | | |
66 | 75 | | |
| |||
74 | 83 | | |
75 | 84 | | |
76 | 85 | | |
77 | | - | |
| 86 | + | |
| 87 | + | |
78 | 88 | | |
79 | 89 | | |
80 | | - | |
| 90 | + | |
81 | 91 | | |
82 | 92 | | |
83 | 93 | | |
84 | 94 | | |
85 | 95 | | |
86 | 96 | | |
87 | 97 | | |
88 | | - | |
| 98 | + | |
89 | 99 | | |
90 | 100 | | |
91 | 101 | | |
| |||
118 | 128 | | |
119 | 129 | | |
120 | 130 | | |
121 | | - | |
| 131 | + | |
122 | 132 | | |
123 | | - | |
| 133 | + | |
0 commit comments