Commit 3817678
committed
feat(tcp): add basic single-client Server and Client communication over port 5000
What
- Implemented a simple TCP server (`Server.java`) that:
- Binds to port 5000 using `ServerSocket`.
- Accepts one client connection.
- Reads a line of text from client via `BufferedReader`.
- Prints client’s message to server console.
- Sends back a fixed acknowledgment message via `PrintWriter`.
- Closes all streams and sockets after exchange.
- Implemented a simple TCP client (`Client.java`) that:
- Connects to `localhost:5000`.
- Sends `"Hello Server!"` message to server.
- Reads the server’s reply and prints to console.
- Closes all resources after communication.
Why
- Demonstrates the foundational client-server communication model in Java using TCP sockets.
- Provides a minimal working example of:
- ServerSocket (listening for incoming connections).
- Socket (creating client connection).
- Stream-based I/O (`BufferedReader` for reading, `PrintWriter` for writing).
- Helps beginners understand how request-response style communication works over TCP.
How
- **Server flow**:
- `ServerSocket ss = new ServerSocket(5000)` → bind server on port 5000.
- `Socket s = ss.accept()` → block until client connects.
- Wrap input stream in `BufferedReader` → `br.readLine()` to get client’s message.
- Wrap output stream in `PrintWriter` with auto-flush → send acknowledgment string.
- Close all resources in order (`br`, `pw`, `s`, `ss`).
- **Client flow**:
- `Socket s = new Socket("localhost", 5000)` → connect to server.
- Use `PrintWriter` to send `"Hello Server!"`.
- Use `BufferedReader` to read server’s acknowledgment.
- Print server response and close resources.
Key considerations / notes
- Blocking I/O: `accept()` and `readLine()` block until a connection/message is received.
- Protocol: Both sides assume newline-terminated strings because of `readLine()`/`println()`.
- Single client only: Server handles only one client, then shuts down.
- Charset: Defaults to platform encoding; for production use, specify UTF-8 explicitly.
- Lifecycle: After exchange, both server and client close sockets, ending session.
How to run / test
1. Start the server:
- Run `Server.main()`.
- Console prints “Server started. Waiting for client...”.
2. Start the client:
- Run `Client.main()`.
- Client sends `"Hello Server!"`.
- Server prints `"Client says: Hello Server!"`.
- Client prints `"Server replied: Hello Client, I got your message!"`.
3. Both programs close their sockets after message exchange.
Real-life applications
- Educational examples for socket programming.
- Base for building request-response systems (chat servers, messaging apps).
- Useful foundation before scaling to multi-client or multi-threaded servers.
Future improvements
- Add server loop to handle multiple clients sequentially.
- Introduce multi-threading for concurrent client handling.
- Improve protocol (instead of hardcoded strings, exchange structured messages).
- Implement proper error handling (socket timeouts, IOException).
- Replace `localhost` with configurable host/IP for remote testing.
Files added
- `Server.java` — minimal TCP server (single client, one-message exchange).
- `Client.java` — minimal TCP client connecting to server and exchanging one message.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9ed5c29 commit 3817678
2 files changed
+49
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
0 commit comments