In Java, secure network communication is provided by the Java Secure Socket Extension, which supports SSL/TLS-based sockets.
👉 Two main classes used:
These are used to build secure client-server applications over the network.
An SSL Socket is a client-side socket used to create a secure connection with a server using SSL/TLS encryption.
👉 It ensures:
javax.net.ssl.SSLSocket
📊 Flow: Client → SSLSocket → Secure connection → SSL Server
| Method | Purpose |
|---|---|
startHandshake() |
Starts SSL handshake |
getInputStream() |
Receives data |
getOutputStream() |
Sends data |
close() |
Closes connection |
import javax.net.ssl.*;
public class SSLClient {
public static void main(String[] args) throws Exception {
SSLSocketFactory factory =
(SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket =
(SSLSocket) factory.createSocket("localhost", 8443);
socket.startHandshake();
System.out.println("Secure connection established");
socket.close();
}
}
An SSL Server Socket is a server-side socket that listens for secure SSL connections from clients.
👉 It accepts encrypted communication requests.
javax.net.ssl.SSLServerSocket
📊 Flow: Client → SSL request → SSLServerSocket → Accept → Secure session
| Method | Purpose |
|---|---|
accept() |
Accepts client connection |
close() |
Closes server socket |
setNeedClientAuth() |
Requires client authentication |
import javax.net.ssl.*;
public class SSLServer {
public static void main(String[] args) throws Exception {
SSLServerSocketFactory factory =
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket serverSocket =
(SSLServerSocket) factory.createServerSocket(8443);
System.out.println("Server waiting for secure connection...");
SSLSocket socket = (SSLSocket) serverSocket.accept();
System.out.println("Client connected securely");
socket.close();
serverSocket.close();
}
}
| Feature | SSLSocket | SSLServerSocket |
|---|---|---|
| Role | Client side | Server side |
| Purpose | Connects to server | Waits for clients |
| Method | createSocket() |
accept() |
| Direction | Initiates connection | Accepts connection |
📊 Steps:
👉 After handshake, all data is encrypted.
Both classes provide:
SSLServerSocketSSLSocket📊 SSL Communication Flow:
Client (SSLSocket) → SSL Handshake → Server (SSLServerSocket) → Secure Data Exchange (Encrypted)
Open this section to load past papers