Socket Options TCP_NODELAY On SocketChannel

TCP_NODELAY disables Nagle batching on a SocketChannel; getOption reads back the values the kernel actually accepted, not what was merely requested.

JavaResilience

Code

snippet
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class SocketTuning {

    public static void main(String[] args) throws Exception {
        try (ServerSocketChannel server = ServerSocketChannel.open()) {
            server.bind(new InetSocketAddress(0)); // port 0: the kernel picks a free port
            int port = ((InetSocketAddress) server.getLocalAddress()).getPort();

            try (SocketChannel client = SocketChannel.open()) {
                client.connect(new InetSocketAddress("127.0.0.1", port));

                // TCP_NODELAY disables Nagle batching: small interactive packets go out at once
                client.setOption(StandardSocketOptions.TCP_NODELAY, true);
                client.setOption(StandardSocketOptions.SO_KEEPALIVE, true);

                // getOption reads back the values the kernel actually accepted
                System.out.println("tcp_nodelay=" + client.getOption(StandardSocketOptions.TCP_NODELAY));
                System.out.println("keepalive=" + client.getOption(StandardSocketOptions.SO_KEEPALIVE));
                System.out.println("send_buffer=" + client.getOption(StandardSocketOptions.SO_SNDBUF));

                client.write(StandardCharsets.UTF_8.encode("ping
"));
                client.shutdownOutput(); // signal the peer that writing is done
            }

            ByteBuffer buf = ByteBuffer.allocate(64);
            try (SocketChannel accepted = server.accept()) {
                while (accepted.read(buf) != -1) { /* drain until EOF */ }
                String text = StandardCharsets.UTF_8.decode(buf.flip()).toString().trim();
                System.out.println("received: " + text);
            }
        }
    }
}

What the demo tunes

A ServerSocketChannel binds to port 0 so the kernel picks a free port, and a client connects to it over loopback. Two options go in: TCP_NODELAY true, which turns off Nagle batching so tiny interactive writes are not held back, and SO_KEEPALIVE true.

getOption then reads both values back from the channel, the kernel-accepted truth rather than merely the request. SO_SNDBUF is also printed to show buffer sizes are system-decided. The client writes ping, calls shutdownOutput so the peer sees EOF, and the server side drains the buffer and prints received: ping.

Points worth noting

  • Port 0 binding is the standard way to get an ephemeral port.
  • shutdownOutput closes the write half while the socket stays open for reads.

Interactive Tools for This Topic

FAQ

What does TCP_NODELAY change?
It disables Nagle batching on the connection so small packets leave immediately; the option is set, then read back as true through getOption to confirm the kernel accepted it.