Basic Capture with Typed Protocol Streams

This example demonstrates the core jNetWorks workflow using the fluent syntax:

  • Selecting capture ports with PortFilter

  • Creating typed ProtocolStream<IpDatagram>

  • Safe resource management and graceful shutdown

  • Integration with TaskScope for timeout control

/* 
 * Licensed under the Apache License, Version 2.0 - see http://www.apache.org/licenses/LICENSE-2.0
 */
package com.slytechs.sdk.jetnetworks;

import java.time.Duration;

import com.slytechs.sdk.common.session.SessionShutdownException;
import com.slytechs.sdk.common.util.Registration;
import com.slytechs.sdk.jnetworks.Net;
import com.slytechs.sdk.jnetworks.NetException;
import com.slytechs.sdk.jnetworks.PacketStreamSettings;
import com.slytechs.sdk.jnetworks.ProtocolStream;
import com.slytechs.sdk.jnetworks.device.PortFilter;
import com.slytechs.sdk.jnetworks.pcap.PcapBackend;
import com.slytechs.sdk.jnetworks.task.TaskScope;
import com.slytechs.sdk.protocol.core.stack.ProtocolStack;
import com.slytechs.sdk.protocol.tcpip.ip.IpDatagram;

public class BasicProtocolStreams {

    public static void main(String[] args) {
        new BasicProtocolStreams().run();
    }

    public void run() {

        Net.activateLicense();

        try (Net net = new PcapBackend()) {

            ProtocolStack stack = new ProtocolStack();
            PacketStreamSettings settings = new PacketStreamSettings();

            // Create a typed stream for IP datagrams
            ProtocolStream<IpDatagram> ipStream =
                    net.createProtocolStream("ip", IpDatagram.class, settings, stack);

            // Start capture: auto-select available Ethernet ports, filter IP traffic
            Registration captureRegistration = net.capture(PortFilter.ethernet().up())
                    .onPortSelection(port -> System.out.printf("Capturing on port: %s%n", port.name()))
                    .filter("ip")
                    .pipe(ipStream) // Direct packets into our typed stream
                    .apply();

            try (TaskScope scope = new TaskScope(net)) {
                scope.shutdownAfter(Duration.ofSeconds(15));

                scope.join(() -> {
                    while (ipStream.isActive()) {
                        try {
                            IpDatagram ipDatagram = ipStream.take();

                            // Process the fully-typed, processed datagram
                            System.out.printf("IP %s → %s (len=%d)%n",
                                    ipDatagram.src(), ipDatagram.dst(), ipDatagram.length());

                            ipStream.release(ipDatagram);

                        } catch (SessionShutdownException | InterruptedException e) {
                            // Graceful shutdown triggered
                            stream.getParentSession().shutdown();
                        }
                    }
                });
            }

        } catch (NetException e) {
            e.printStackTrace();
        }
    }
}

Key Concepts Demonstrated

  • PortFilter.ethernet().up() – Automatically selects active Ethernet interfaces

  • .pipe(ipStream) – Directly connects capture to a typed protocol stream

  • TaskScope – Clean timeout and shutdown management

  • Typed ProtocolStream – Receive fully dissected objects (IpDatagram) instead of raw packets

Last updated