Java
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Java
-
Section 4: Collections Framework
-
Section 5: Exception Handling
-
Section 6: Generics
-
Section 7: Functional Java
-
Section 8: Concurrency
-
Section 9: I/O and NIO
-
Section 10: JVM Internals
-
Section 11: Modern Java Features
-
Section 12: Build Tools and Project Structure
-
Section 13: Testing
-
Section 14: Databases and Persistence
-
Section 15: Networking
-
Section 16: Design and Best Practices
-
Section 17: Reflection and Annotations
-
Section 18: Logging and Diagnostics
-
Section 19: Date, Time, and Internationalization
-
Section 20: Java Platform Module System
-
Section 21: Security in Java
-
Section 22: Advanced Collections and Data Structures
-
Section 23: More Concurrency Patterns
-
Section 24: Compression, Files, and System Integration
-
Section 25: GUI Programming
-
Section 26: Practical Projects
-
Section 27: Data Structures and Algorithms
-
Section 28: Interview and Algorithm Practice
-
Section 29: JSON and Data Interchange
-
Section 30: More Concurrency Utilities
-
Section 31: More Collections and Streams Practice
-
Section 32: More File and System Programming
-
Section 33: Standard Library Deep Dive
-
Section 34: More Practice and Drills
-
Section 35: More Testing and Quality
-
Section 36: More Design Patterns and Architecture
-
Section 37: Career and Ecosystem
-
Section 38: More OOP and Architecture Practice
-
Section 39: More Enterprise Concepts
-
Section 40: Advanced JavaFX
-
Section 41: More Interview Practice
141: Building a Simple TCP Client and Server
Think of a TCP connection like a traditional phone call. For a conversation to happen, one person has to be sitting by the phone, waiting for it to ring, and the other person has to know the specific phone number to dial. If the person waiting isn't there, or if the caller dials the wrong number, the connection fails. Once the call is picked up, though, you have a dedicated line where both people can talk and listen simultaneously until someone hangs up.
In Java, we map this exactly: the ServerSocket is the person waiting by the phone. The Socket is the actual connection—the "call" itself. The IP address is the phone number, and the Port is like an extension number that ensures you're talking to the right department (or in our case, the right application) on that machine.
Setting up the Listening Post
The server's job is to sit in a loop and wait. I've seen a lot of developers forget that serverSocket.accept() is a blocking call. This means your program literally stops and waits right there until a client attempts to connect. It doesn't burn CPU cycles spinning in a circle; it just sleeps until the OS wakes it up with a connection request.
import java.io.*;
import java.net.*;
public class TempServer {
public static void main(String[] args) throws IOException {
// We open the "phone line" on port 5000
try (ServerSocket serverSocket = new ServerSocket(5000)) {
System.out.println("Server is listening on port 5000...");
// This blocks until a client connects
try (Socket clientSocket = serverSocket.accept()) {
System.out.println("Client connected!");
// We need a way to read what the client says and a way to respond
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
String city = in.readLine();
System.out.println("Client asked for temperature in: " + city);
// For this simple example, we'll just fake the data
if ("London".equalsIgnoreCase(city)) {
out.println("15°C and rainy, as usual.");
} else {
out.println("22°C and sunny!");
}
}
}
}
}
Making the Call
The client is much more straightforward. You just need to know where the server is and which port it's listening on. When you instantiate a Socket, Java immediately attempts to establish the three-way handshake that TCP is famous for. If the server isn't running, you'll get a ConnectException immediately.
I always recommend using PrintWriter with the autoFlush parameter set to true. If you don't, your data might just sit in a local buffer, and your server will be left waiting for a message that's technically already been "sent" but hasn't actually left the client's memory.
import java.io.*;
import java.net.*;
public class TempClient {
public static void main(String[] args) throws IOException {
// Dial the server at "localhost" (this machine) on port 5000
try (Socket socket = new Socket("localhost", 5000)) {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Send the request
out.println("London");
// Read the response
String response = in.readLine();
System.out.println("Server says: " + response);
}
}
}
The Flow of Data
Notice how both sides use InputStream and OutputStream. This is the core of Java I/O. The server's getInputStream() is actually linked to the client's getOutputStream(). It's a bidirectional pipe. When the client calls out.println(), the bytes travel across the network and land in the server's input buffer.
One thing to keep in mind: readLine() expects a newline character (\n or \r\n) to know when a message ends. If you send a string without a newline, the server will just hang there forever, thinking the client is still typing. That's why we use println instead of print.
📋 Practical Task
Build a Digital Vending Machine
Your task is to create a client-server application that simulates a vending machine.
- The Server: Should listen on port 6000. It should wait for a "Product Code" (e.g., "A1", "B2", "C3") from the client. Based on the code, it should return the name of the snack (e.g., "A1" returns "Cool Ranch Doritos", "B2" returns "Snickers Bar"). If the code isn't recognized, return "Invalid Code".
- The Client: Should allow the user to type a product code into the console, send that code to the server, and then print the snack name returned by the server.
Make sure to handle the potential IOException and ensure that your streams are closed properly using try-with-resources.
There are no comments for now.