Skip to main content
Advertisement

TCP Socket Programming

TCP (Transmission Control Protocol) Socket Programming is a fundamental methodology explicitly structured to implement highly reliable 1:1 connection-based communication precisely between a Client and a Server. This is architecturally analogous to conducting a direct phone conversation.

Underneath, communicating thoroughly over a network is essentially an extension of utilizing the Input/Output (I/O) Streams covered previously in Chapter 15. In this context, a Socket strictly acts as the definitive end-point (connection terminal) for these persistent I/O streams.

1. Core Structure of TCP Sockets

To accurately facilitate TCP communication, the functional roles of the Server and Client are explicitly segregated.

  1. Server (ServerSocket): Acts as the authoritative base station. It aggressively opens a designated Port and patiently waits (accepts). When a Client successfully attempts to connect, the Server instantly spawns and gracefully returns a brand new, dedicated standard Socket strictly for 1:1 communication with that exact Client.
  2. Client (Socket): Proactively requests a dedicated connection utilizing the targeted Server's precise IP Address and explicitly opened Port number.

2. Building a Simple Echo Server

Below is the rudimentary structural architecture for a unidirectional Echo Server and its matching Client—where the localized Server simply bounces any received messages cleanly back to the originating Client.

Server-Side Implementation

import java.io.*;
import java.net.*;

public class TcpServer {
public static void main(String[] args) {
try {
// 1. Generate a ServerSocket actively listening exclusively on port 7777
ServerSocket serverSocket = new ServerSocket(7777);
System.out.println("Server successfully initiated. Standing by for Clients.");

// 2. Halts execution implicitly and generously returns a dedicated Socket upon Client connection
Socket socket = serverSocket.accept();
System.out.println("A Client has successfully connected!");

// 3. Instantiate a reliable Input Stream tightly bound to the socket to read incoming data
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 4. Instantiate an Output Stream to aggressively push data outwardly through the socket
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

// 5. Patiently read the Client's message and actively echo it backwards
String clientMsg = in.readLine();
System.out.println("Received Message: " + clientMsg);

out.println("Server Echo: " + clientMsg);

// Resources MUST be strictly and safely closed universally after usage
in.close();
out.close();
socket.close();
serverSocket.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}

Client-Side Implementation

import java.io.*;
import java.net.*;

public class TcpClient {
public static void main(String[] args) {
try {
// 1. Proactively request a reliable connection to the Server's IP (Localhost) on port 7777
Socket socket = new Socket("127.0.0.1", 7777);
System.out.println("Successfully connected securely to the Server.");

// 2. Structurally configure the synchronized streams
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

// 3. Actively transmit a customized message cleanly to the remote Server
out.println("Hello! I am the TCP Client.");

// 4. Competently receive and display the echoed response returning from the Server
System.out.println("Reply strictly received from Server: " + in.readLine());

// Aggressively release utilized resources
in.close();
out.close();
socket.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}

While this cleanly validates real-time dynamic data exchange, practically, if numerous independent users attempt connections, blocking predictably occurs entirely during the standard accept() phase. Consequently, employing structural Multi-threading or Non-blocking I/O (NIO) becomes absolutely mandatory for realistically scalable enterprise services.

Advertisement