Commit 9ed5c29
committed
feat(tcp): implement simple single-client ReverseEcho TCP server and client
What
- Added a TCP server (ReverseEcho) bound to port 2000 that accepts a single client connection.
- Reads lines of text from client, reverses them with StringBuilder.reverse(), and sends the reversed string back.
- Terminates when the reversed message equals "dne".
- Added a standalone Client class that:
- Connects to the server at given IP and port 2000.
- Reads user input from stdin.
- Sends each line to the server and prints back reversed response until "dne" is echoed.
Why
- Demonstrates the fundamentals of socket programming in Java:
- Creating a ServerSocket and accepting a connection.
- Using blocking I/O (BufferedReader / PrintStream) for text-based line protocol.
- Applying simple transformation logic on server (reverse string).
- Useful for beginners to learn how client-server communication works before scaling to multi-threaded or multi-client setups.
- Builds a foundation for understanding protocols, request/response handling, and TCP’s connection-oriented nature.
How
- Server:
- Create `ServerSocket ss = new ServerSocket(2000)` and block on `ss.accept()` for client.
- Wrap input with `BufferedReader` for line-based reading, output with `PrintStream` for convenience.
- Loop: readLine() from client, reverse via `StringBuilder`, send back via println().
- Exit loop when reversed string equals "dne".
- Client:
- Connect to server IP and port using `new Socket("device-ip", 2000)`.
- Wrap keyboard input (System.in) in BufferedReader.
- Wrap socket I/O in BufferedReader/PrintStream for line protocol.
- Loop: read line from keyboard, send with ps.println(), read server’s reversed reply, print to stdout.
- Exit loop when reply equals "dne".
Key considerations / notes
- Host IP:
- Replace `"Add Your Device IP Address"` with `"localhost"` when testing locally.
- Protocol:
- Both client and server must use newline-terminated strings because of readLine/println design.
- Termination:
- Triggered when the reversed message equals "dne".
- This means client must type "end" to get "dne" after reversing, or directly type "dne".
- Limitations:
- Server supports only one client at a time and blocks until connection closes.
- No error handling for client disconnects (readLine() may return null).
- No multi-threading; new client cannot connect until server restarts.
- Encoding:
- Uses platform default charset in InputStreamReader/PrintStream; for robustness specify UTF-8 explicitly.
How to run / test
1. Start server:
- Run `ReverseEcho.main()`.
- Console shows no logs, but server is bound on port 2000.
2. Start client:
- Run `Client.main()` with correct host IP (or localhost).
- Client will block waiting for keyboard input.
3. Interaction:
- Type a line, press Enter.
- Server reverses and sends it back; client prints reversed line.
- Enter "dne" (or any line whose reverse equals "dne") to terminate session.
Real-life applications
- Educational exercises for networking fundamentals.
- Simple text transformers (palindrome checkers, encoders, mock chat bots).
- Introductory labs for teaching TCP/IP and socket programming concepts.
Future improvements
- Add logging statements in server for better visibility.
- Support multiple clients with multi-threaded handler model (one thread per client).
- Gracefully handle client disconnects (check null from readLine).
- Close sockets and streams in finally block or try-with-resources.
- Add configuration for host/port instead of hardcoding.
- Specify UTF-8 charset explicitly for consistent behavior across platforms.
- Enhance protocol with structured commands (not just reversed text).
Files added / modified
- ReverseEcho.java — server implementation (main class).
- Client.java — simple TCP client for testing ReverseEcho server.
Notes
- This is a minimal blocking TCP example: best suited for learning.
- For production use, prefer java.nio (non-blocking) or multi-threaded server with proper resource handling.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 135456c commit 9ed5c29
File tree
1 file changed
+19
-22
lines changed- Section27NetworkProgramming/ReverseEchoServerSingleClient/src
1 file changed
+19
-22
lines changedLines changed: 19 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
| 5 | + | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | | - | |
| 9 | + | |
11 | 10 | | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
21 | 20 | | |
22 | | - | |
| 21 | + | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
28 | | - | |
| 27 | + | |
29 | 28 | | |
30 | | - | |
31 | | - | |
| 29 | + | |
| 30 | + | |
32 | 31 | | |
33 | 32 | | |
34 | 33 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 34 | + | |
| 35 | + | |
39 | 36 | | |
40 | 37 | | |
41 | 38 | | |
| |||
47 | 44 | | |
48 | 45 | | |
49 | 46 | | |
50 | | - | |
51 | | - | |
52 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
53 | 50 | | |
54 | | - | |
| 51 | + | |
55 | 52 | | |
56 | 53 | | |
57 | 54 | | |
58 | 55 | | |
59 | | - | |
| 56 | + | |
60 | 57 | | |
61 | | - | |
| 58 | + | |
0 commit comments