Sockets (Client and Server Communication)
In the Java programming language, sockets are an endpoint client-server communication concept. Socket methods are used to properly establish communication between two dedicated programs over online and offline network communication.

In Java network communication, socket programming primarily helps you.
java.net
Basic Java client-server communication model.
Client Server Communication
| |
| —- Client Made Connection Request —> |
| |
| <—- Server Connection Accepted — |
| |
| —- Send Required Client Data ————> |
| <—- Receive Required Client Data ——— |
| |
Java provides two main classes for this.
- Socket — It is used on the client side in Java network communication.
- ServerSocket — It is used on the server side in Java network communication.
Server-side Socket is a Java communication concept.
A dedicated location or method of a socket server that waits for a client request to connect to the network.
Basic steps of a Server-side Socket.
First, create a ServerSocket.
↓
Wait for the client to connect to it
↓
Server accepts the client connection
↓
Get input/output streams
↓
Read/write data to a file
↓
Close the socket connection
Server-side Socket example in Java.
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args)
throws IOException {
ServerSocket server =
new ServerSocket(5000);
System.out.println(“Host Server is on”)
Socket socket = server.accept();
BufferedReader input =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()
)
);
String info = input.readLine();
System.out.println(
“Client request -: ” + info
);
socket.close();
server.close();
}
}
Here in the Server-side Socket example.
new ServerSocket(5000)
This starts a server on port 5000.
Main ServerSocket method.
server.accept()
This waits for a client request connection in ServerSocket communication.
Client-side network communication concept.
This method connects to a dedicated host server using a client-side network socket.
Example of client-side network communication.
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args)
throws IOException {
/ Here we connect to the server on port 5000 location
Socket socket = new Socket(“localhost”, 5000);
/ Here we create an output stream to send data
PrintWriter output = new PrintWriter(
socket.getOutputStream(), true
);
/ Here we send a message to the dedicated server
output.println(“Hi Server”);
/ Here we close the connection after done
output.close();
socket.close();
}
}
In a Java program, the client connects to the server socket using:
new Socket(“localhost”, 5000);
In a client-side network communication example.
- localhost → This is the local server address location.
- 5000 → This is a dedicated port location on the server.
Java Server Sockets send and receive data in client-server communication.
A socket provides input and output streams for client-server communication.
InputStream input =
socket.getInputStream();
OutputStream output =
socket.getOutputStream();
Generally, in Java network communication, these streams are wrapped with an Easy class.
Sending text in network communication.
PrintWriter writer =
new PrintWriter(
socket.getOutputStream(),
true
);
writer.println(“Hi server”);
Receiving text in network communication.
BufferedReader reader =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()
)
);
String info = reader.readLine();
Two-way Java client-server socket communication.
In Java network communication, both the client and the server can send and receive important data to each other over a dedicated network communication socket port.
Server data send-receive communication.
PrintWriter output =
newPrintWriter(
socket.getOutputStream(),
true
);
BufferedReader input =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()
)
;
String info = input.readLine();
System.out.println(“Client communication -: ” + info);
out.println(“Hi Client”);
Client data send-receive communication.
PrintWriter output =
newPrintWriter(
socket.getOutputStream(),
true
);
BufferedReader input =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()
)
);
output.println(“Hi Server”);
String reply = input.readLine();
System.out.println(
“Server output -: ” + reply
);
Thus it becomes client-server socket communication.
Client server send receive socket communication.
| |
| “Hi Server” ————> |
| |
| <———– “Hi Client” |
| |
Handling multiple clients in a Java Server Socket.
Java Client Server Port A basic server in network communication handles or manages only one client at a time.
For simultaneous network communication between multiple clients, a common Java method is to create a separate thread for each client.
while (true) {
socket socket =
server.accept();
new Thread(() -> {
// now it can handle multiple clients
}).start();
}
Conceptual Java Multiple Client Handling Concept.
Dedicated web server.
|
ServerSocket communication method
|
┌──────────┼─────────┐
↓ ↓ ↓
Client Request 1 Client Request 2 Client Request 3
↓ ↓ ↓
Client Thread 1 Client Thread 2 Client Thread 3
For the high-concurrency application in this example, Java NIO with SocketChannel and Selector can avoid the need for a client network communication thread for every connection.
Important Java Client-Server Socket Network Communication Classes
| # | Important Socket Class | What Is the Purpose of Client-Server Socket Class |
| 1. | Socket class | Socket class helps us to establish client-side TCP network connection |
| 2. | ServerSocket class | ServerSocket class used to listen for TCP client connections request |
| 3. | InetAddress class | InetAddress class used to display or represents an client system IP address information |
| 4. | InputStream class | InputStream class used to receive data in socket communication |
| 5. | OutputStream class | OutputStream class help us to sends data and information in communication |
| 6. | BufferedReader class | BufferedReader class help us to reads text efficiently in socket communication |
| 7. | PrintWriter class | PrintWriter class helping us to writes text conveniently in socket communication |
TCP vs. UDP in Java client-server socket communication.
Java client server fully supports both TCP and UDP communication in network socket communication.
TCP Network Communication port.
Using a TCP port.
- In a socket.
- In a ServerSocket.
The TCP network communication port provides a connection-oriented and accurate and reliable byte stream.
UDP Network Communication port.
Using a UDP port.
- In a DatagramSocket.
- In a DatagramPacket.
Remember, UDP is a connectionless method of network communication, and it does not provide guarantees of successful delivery of TCP data information to its users.
Socket communication flow in a Java client-server network.
SERVER socket communication flow.
ServerSocket
↓
client request accept()
↓
socket communication
↓
InputStream / OutputStream
↓
Read/Write task
CLIENT socket communication flow.
ClientSocket
↓
Connect to Server port
↓
InputStream / OutputStream
↓
Read/Write task
Conclusion of Sockets Client and Server Communication.
- ServerSocket listens for incoming TCP connections in dedicated network communication.
- In which Socket displays a TCP connection between a client and a server.
- The accept() function waits for all incoming client requests to the server.
- The getInputStream() function accepts data for communication.
- getOutputStream() function sends data in communication.
- Client server communication can handle multiple clients using threads or NIO.
- Remember to always close sockets and streams, preferably using try-with-resources.
In short, socket programming helps Java web offline applications to communicate over a network using a client-server model, in which the ServerSocket accepts the client connection, and the Socket provides a better communication channel.

