Skip to content

Commit 9ed5c29

Browse files
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

1 file changed

+19
-22
lines changed

Section27NetworkProgramming/ReverseEchoServerSingleClient/src/ReverseEcho.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,37 @@
22
import java.io.*;
33

44
public class ReverseEcho {
5-
public static void main(String[] args) throws Exception
6-
{
5+
public static void main(String[] args) throws Exception {
76
ServerSocket ss = new ServerSocket(2000);
87
Socket stk = ss.accept();
98

10-
//Reading complete line from client.
9+
//Reading complete line from a client.
1110
BufferedReader br = new BufferedReader(new InputStreamReader(stk.getInputStream()));
11+
1212
//Print output to the client.
1313
PrintStream ps = new PrintStream(stk.getOutputStream());
1414

1515
String msg;
16-
//Reading a msg from a client and reverse it back and send to client.
17-
StringBuilder sb;
18-
//References deceleration.
19-
do
20-
{
16+
//Reading a msg from a client and reverse it back and send to a client.
17+
18+
StringBuilder sb; //References deceleration.
19+
do {
2120
msg = br.readLine();
22-
//reversing a msg from client using method present inside the StringBuilder.
21+
//reversing a msg from a client using method present inside the StringBuilder.
2322
/*StringBuilder sb = new StringBuilder(msg);*/
2423
sb=new StringBuilder(msg);
2524
sb.reverse();
2625
msg=sb.toString();
2726

28-
//write back to the output stream of socket.
27+
//write back to the output stream of the socket.
2928
ps.println(msg);
30-
}while(!msg.equals("dne"));
31-
//Server part is ready.
29+
} while(!msg.equals("dne"));
30+
//The server part is ready.
3231
}
3332
}
3433

35-
class Client
36-
{
37-
public static void main(String[] args)throws Exception
38-
{
34+
class Client {
35+
public static void main(String[] args)throws Exception {
3936
Socket stk = new Socket("Add Your Device IP Address",2000);
4037

4138
//Read from the keyboard.
@@ -47,15 +44,15 @@ public static void main(String[] args)throws Exception
4744
PrintStream ps = new PrintStream(stk.getOutputStream());
4845

4946
String msg;
50-
//StringBuilder sb; no need here because we are not reading it from client side.
51-
do
52-
{
47+
//StringBuilder sb; no need here because we are not reading it from the client side.
48+
49+
do {
5350
msg= keyb.readLine();
54-
//After reading from the keyboard it send it to the server.
51+
//After reading from the keyboard it sends it to the server.
5552

5653
ps.println(msg);
5754
msg=br.readLine();
5855
System.out.println("From Server: "+msg);
59-
}while (!msg.equals("dne"));
56+
} while (!msg.equals("dne"));
6057
}
61-
}
58+
}

0 commit comments

Comments
 (0)