Understanding Blocking I/O via TCP Socket Interaction
TCP Socket Interaction and the Read Operation
After a TCP connection is established via the three-way handshake, data transmission begins. For the server, once accept() completes, the socket is ready and the process typically calls read(). This read() call is blocking, meaning the server process will wait, suspended, until the call returns with data or an error condition.
The total blocking duration of a read() call consists of several phases spent waiting to data to become available and to be processed:
- T1 - Send Buffer Wait: The client may be delayed if its TCP send window is full.
- T2 - Network Transmission: Time for packets to travel across the network.
- T3 - Kernel-to-User Copy: Time for the operaitng system to copy received data from kernel buffers into the application's user-space memory.
The read() call remains blocked for at least T1 + T2 + T3. The period T1 + T2 represents the wait for data to arrive at the receiving host's network stack.
Java Demonstration of Blocking Socket I/O
The following code demonstrates blocking I/O (BIO). The client includes intentional delays to simulate I/O processing time, during which the server's read call remains blocked.
Server Implementation
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class BioSocketServer {
private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
public static void main(String[] args) {
try (ServerSocket server = new ServerSocket(8080)) {
while (true) {
Socket connection = server.accept();
System.out.println(getTime() + " - Connection accepted, waiting to read...");
String metrics = readData(connection.getInputStream());
System.out.println(getTime() + " - " + metrics);
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String readData(InputStream input) throws Exception {
long waitStart = System.currentTimeMillis();
int bytesRead;
long dataStart = 0;
int totalBytes = 0;
while ((bytesRead = input.read()) != -1) {
if (dataStart == 0) {
dataStart = System.currentTimeMillis();
}
totalBytes += bytesRead;
}
long readEnd = System.currentTimeMillis();
long waitTime = (dataStart == 0) ? 0 : (dataStart - waitStart);
long readTime = (dataStart == 0) ? 0 : (readEnd - dataStart);
return String.format("Wait=%dms, Read=%dms, Total=%d bytes",
waitTime, readTime, totalBytes);
}
private static String getTime() {
return timeFormat.format(new Date());
}
}
Client Implementation
import java.net.Socket;
import java.io.OutputStream;
public class BioSocketClient {
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 8080)) {
OutputStream out = socket.getOutputStream();
// Simulate processing delay before sending
Thread.sleep(5000);
out.write(generateTestData());
// Simulate time taken for the write operation
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
private static byte[] generateTestData() {
int size = 1024 * 1024; // 1 MB
byte[] data = new byte[size];
for (int i = 0; i < size; i++) {
data[i] = 1;
}
return data;
}
}
Program Output
23:03:14 - Connection accepted, waiting to read...
23:03:20 - Wait=5005ms, Read=1002ms, Total=1048576 bytes
The output shows the server's read call was blocked for approximate 5 seconds (Wait time) while the client was preparing and sending data, clearly demonstrating the blocking nature of the I/O operation.