🟩 TCP/IP Client Socket and Server Socket
🔹 Introduction
In web technology, TCP/IP sockets are used for communication between two programs —
usually between a client (like a browser or user application) and a server (a machine that
provides services like web pages, files, or data).
● Think of it like a telephone connection:
● One side (server) waits for a call.
● The other side (client) makes the call.
● When both connect, they can talk (send and receive data).
● This connection between client and server is established using a socket.
🔹 What is a Socket?
A socket is an endpoint (a door) for sending or receiving data across a network.
It connects the application layer and transport layer (TCP/IP) in the Internet model.
Each socket is identified by:
> IP Address + Port Number
Example:
192.168.1.10 : 8080
Here,
192.168.1.10 → IP address of the computer
8080 → Port number used by the application
🔹 Types of Sockets
1. TCP Socket (Transmission Control Protocol)
● Connection-oriented (like a phone call)
● Reliable: ensures data delivery in correct order
● Used in most web applications (HTTP, HTTPS, FTP)
2. UDP Socket (User Datagram Protocol)
● Connectionless (like sending a message without reply)
● Faster but less reliable
● Used in video streaming, online games, etc.
● In Web Technology, we mainly study TCP Sockets because the web uses TCP/IP for
communication.
🟩 TCP/IP Client-Server Communication
Let’s understand this with the two sides of communication:
🔹 1. Server Socket
The Server Socket is like a host that waits for clients to connect.
Steps performed by a Server Socket:
1. Create a socket: to start communication.
2. Bind it: to a specific IP address and port number.
3. Listen: for incoming connection requests from clients.
4. Accept: a connection when a client requests.
5. Send/Receive data: after the connection is established.
6. Close connection: when the communication ends.
Example:
import java.io.*;
import java.net.*;
public class ServerExample {
public static void main(String[] args) {
try {
// Step 1: Create ServerSocket and bind to port
ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Server is waiting for client connection...");
// Step 2: Accept the client connection
Socket socket = serverSocket.accept();
System.out.println("Client connected: " + socket.getInetAddress());
// Step 3: Create input and output streams
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// Step 4: Read message from client
String clientMessage = in.readLine();
System.out.println("Client says: " + clientMessage);
// Step 5: Send response to client
out.println("Hello from Server");
// Step 6: Close connections
in.close();
out.close();
socket.close();
serverSocket.close();
System.out.println("Server closed connection.");
} catch (IOException e) {
e.printStackTrace();
}
}
🔹 2. Client Socket
}
The Client Socket is like a caller that connects to the server and exchanges data.
Steps performed by a Client Socket:
1. Create a socket
2. Connect to the server using its IP and port number.
3. Send data to the server.
4. Receive response from the server.
5. Close connection
Example:
import java.io.*;
import java.net.*;
public class ClientExample {
public static void main(String[] args) {
try {
// Step 1: Create a socket and connect to server
Socket socket = new Socket("localhost", 12345);
System.out.println("Connected to server.");
// Step 2: Create input and output streams
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// Step 3: Send message to server
out.println("Hello Server");
// Step 4: Receive server response
String serverResponse = in.readLine();
System.out.println("Server says: " + serverResponse);
// Step 5: Close connections
in.close();
out.close();
socket.close();
System.out.println("Client closed connection.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
🔹 Diagram: TCP/IP Client-Server Communication
🔹 Advantages of TCP/IP Socket Communication
✅ Reliable and error-free data transfer
✅ Maintains a continuous connection
✅ Used in real-time web applications
✅ Efficient and supports large data transmission
🔹 Applications
● Web Servers (HTTP/HTTPS)
● Chat applications
● Online file transfer
● Multiplayer games
● Remote login systems
🔹 In Summary
> A Server Socket waits for a client request,
while a Client Socket initiates the connection.
Together, they use TCP/IP to exchange data reliably between two systems.
This client-server socket mechanism forms the foundation of how web browsers and web
servers communicate over the Internet.