Skip to content

Commit 3817678

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

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.io.*;
2+
import java.net.*;
3+
4+
public class Client {
5+
public static void main(String[] args) throws Exception {
6+
// Server se connect karna (localhost:5000)
7+
Socket s = new Socket("localhost", 5000);
8+
9+
// Output stream ke through server ko message bhejna
10+
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
11+
pw.println("Hello Server!");
12+
13+
// Input stream se server ka reply read karna
14+
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
15+
String reply = br.readLine();
16+
System.out.println("Server replied: " + reply);
17+
18+
pw.close();
19+
br.close();
20+
s.close();
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.io.*;
2+
import java.net.*;
3+
4+
public class Server {
5+
public static void main(String[] args) throws Exception {
6+
// ServerSocket banaya (port 5000 pe listen karega)
7+
ServerSocket ss = new ServerSocket(5000);
8+
System.out.println("Server started. Waiting for client...");
9+
10+
// Client ka connection accept kiya
11+
Socket s = ss.accept();
12+
System.out.println("Client connected!");
13+
14+
// Input stream se client ka message read karna
15+
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
16+
String msg = br.readLine();
17+
System.out.println("Client says: " + msg);
18+
19+
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
20+
pw.println("Hello Client, I got your message!");
21+
22+
br.close();
23+
pw.close();
24+
s.close();
25+
ss.close();
26+
}
27+
}

0 commit comments

Comments
 (0)