In Java NIO (New I/O), SocketChannel and ServerSocketChannel are powerful classes used for non-blocking network communication. These classes are part of the java.nio.channels package and allow developers to build efficient client-server applications without relying on the traditional blocking I/O of java.net.Socket and java.net.ServerSocket.
SocketChannel – The Client-Side Channel
Definition
SocketChannel is a channel-based version of the traditional Socket class. It allows a Java program to connect to a remote server over TCP, send and receive data using ByteBuffer, and optionally operate in non-blocking mode.
Key Features
- Works like a client socket.
- Supports blocking and non-blocking modes.
- Allows asynchronous reads/writes using
ByteBuffer. - Can connect to a remote host using
SocketChannel.open()orconnect()method.
Typical Usage
- Open a socket channel.
- Connect to a remote server.
- Write data to the server using
write(ByteBuffer). - Read the response using
read(ByteBuffer). - Close the channel.
ServerSocketChannel – The Server-Side Channel
Definition
ServerSocketChannel is a channel-based equivalent of the ServerSocket class. It is used to listen for incoming TCP client connections. Once a connection is accepted, it returns a SocketChannel that can be used to communicate with the client.
Key Features
- Works like a server socket.
- Supports blocking and non-blocking modes.
- Can accept multiple connections without blocking the main thread.
- Each accepted connection is represented as a new
SocketChannel.
Typical Usage
- Open a server socket channel.
- Bind it to a port using
bind(). - Listen for incoming connections using
accept(). - For each accepted connection, get a
SocketChannel. - Communicate with the client using
read()andwrite()methods. - Close the server channel.
Non-Blocking Mode
Both SocketChannel and ServerSocketChannel can be set to non-blocking mode using:
channel.configureBlocking(false);Code language: JavaScript (javascript)
In non-blocking mode:
read()andwrite()return immediately without blocking the thread.- You typically use a
Selectorto monitor multiple channels and react to readiness events (like OP_READ, OP_WRITE, OP_CONNECT).
Opening Channels

Commonly Used Methods
SocketChannel

ServerSocketChannel

Simple Program: Echo Server and Client using Channels
Server: Echoes what the client sends
// File: SimpleEchoServer.java
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
public class SimpleEchoServer {
public static void main(String[] args) throws Exception {
ServerSocketChannel server = ServerSocketChannel.open();
server.bind(new InetSocketAddress(5000));
System.out.println("Server started. Waiting for client...");
SocketChannel client = server.accept();
System.out.println("Client connected!");
ByteBuffer buffer = ByteBuffer.allocate(256);
client.read(buffer);
buffer.flip();
client.write(buffer); // Echo back
buffer.clear();
client.close();
server.close();
}
}Client: Sends message to server
// File: SimpleEchoClient.java
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class SimpleEchoClient {
public static void main(String[] args) throws Exception {
SocketChannel client = SocketChannel.open(new InetSocketAddress("localhost", 5000));
String msg = "Hello from Client!";
ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
client.write(buffer);
buffer.clear();
client.read(buffer);
buffer.flip();
System.out.print("Received from server: ");
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
client.close();
}
}Problem Statement
Mahesh wants to build a secure announcement system.LotusJavaPrince should create:
- A ServerSocketChannel that listens for announcements.
- Multiple clients using SocketChannel that send announcements.
- The server will prefix each announcement with “Mahesh Received: “ and send it back.
Server Program: Mahesh Receives Messages
// File: MaheshServer.java
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class MaheshServer {
public static void main(String[] args) throws Exception {
ServerSocketChannel server = ServerSocketChannel.open();
server.bind(new InetSocketAddress(6000));
System.out.println("Mahesh's Server is listening on port 6000...");
while (true) {
SocketChannel client = server.accept(); // Blocking
ByteBuffer buffer = ByteBuffer.allocate(256);
client.read(buffer);
buffer.flip();
String message = new String(buffer.array()).trim();
String reply = "Mahesh Received: " + message;
ByteBuffer outBuffer = ByteBuffer.wrap(reply.getBytes());
client.write(outBuffer);
client.close();
}
}
}Client Program: LotusJavaPrince Sends Announcements
// File: LotusClient.java
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
public class LotusClient {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your announcement: ");
String announcement = scanner.nextLine();
SocketChannel client = SocketChannel.open(new InetSocketAddress("localhost", 6000));
ByteBuffer buffer = ByteBuffer.wrap(announcement.getBytes());
client.write(buffer);
buffer.clear();
client.read(buffer);
buffer.flip();
System.out.print("Server replied: ");
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
client.close();
scanner.close();
}
}Use Cases
- Building scalable chat servers, game servers, or file transfer systems.
- Creating non-blocking clients that can interact with multiple services simultaneously.
- Implementing event-driven servers using
SelectorwithServerSocketChannel.
The SocketChannel and ServerSocketChannel classes in Java NIO provide a modern, non-blocking approach to building network applications. Unlike traditional socket APIs, these NIO channels offer asynchronous and scalable communication over TCP, making them ideal for high-performance networking.
ServerSocketChannelis used to listen for incoming client connections. It acts like a server-side socket but supports non-blocking mode, enabling it to handle multiple connection requests without blocking the thread.SocketChannelrepresents a client-side connection or a channel obtained from a server. It supports read/write operations with buffers, and can work in both blocking and non-blocking modes.
When used with Selectors, these channels enable a single thread to manage multiple concurrent connections efficiently, which is crucial for building scalable servers like chat apps, web servers, and real-time systems.
Key Advantages:
- Support for non-blocking I/O and multiplexing,
- Better scalability and performance than classic sockets,
- Integration with
ByteBufferfor efficient data handling, - Ideal for event-driven and asynchronous network applications.
In summary, SocketChannel and ServerSocketChannel:
- Are essential for high-performance, non-blocking network communication,
- Offer precise control over connections and data flow,
- Form the backbone of scalable, modern server-client architectures in Java NIO.
