Commit 135456c
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 dbc71e5 commit 135456c
File tree
1 file changed
+30
-19
lines changed- Section27NetworkProgramming/ReverseEchoServerMultipleClient/src
1 file changed
+30
-19
lines changedLines changed: 30 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
14 | 13 | | |
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | | - | |
| 19 | + | |
| 20 | + | |
21 | 21 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
26 | 33 | | |
27 | 34 | | |
28 | 35 | | |
29 | 36 | | |
| 37 | + | |
30 | 38 | | |
31 | 39 | | |
32 | 40 | | |
33 | | - | |
| 41 | + | |
34 | 42 | | |
35 | 43 | | |
36 | 44 | | |
| |||
47 | 55 | | |
48 | 56 | | |
49 | 57 | | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
58 | 68 | | |
59 | 69 | | |
60 | | - | |
| 70 | + | |
61 | 71 | | |
62 | 72 | | |
63 | 73 | | |
64 | 74 | | |
65 | 75 | | |
66 | 76 | | |
67 | 77 | | |
68 | | - | |
| 78 | + | |
| 79 | + | |
69 | 80 | | |
70 | 81 | | |
71 | 82 | | |
| |||
111 | 122 | | |
112 | 123 | | |
113 | 124 | | |
114 | | - | |
| 125 | + | |
115 | 126 | | |
116 | | - | |
| 127 | + | |
0 commit comments