This example uses the IBM® Toolbox for Java™ commtrace classes to print communications trace data to a monitor by using a communications trace binary file as the source for the data.
//////////////////////////////////////////////////////////////////////
//
// Example using the commtrace classes to print communications trace
// data to a monitor by using a communications trace binary file as
// the source for the data.
//
// Command syntax:
// java CommTraceExample
//
//////////////////////////////////////////////////////////////////////
import com.ibm.as400.util.commtrace.*;
public class CommTraceExample {
public CommTraceExample() {
// Create a FormatProperties. By default display everything.
FormatProperties fmtprop = new FormatProperties();
Format fmt = new Format("/path/to/file");
// Sets the filtering properties for this format
fmt.setFilterProperties(fmtprop);
fmt.formatProlog(); // Format the prolog
Prolog pro = fmt.getProlog();
System.out.println(pro.toString());
// If this is not a valid trace
if (!pro.invalidData()) {
Frame rec;
// Get the records
while ((rec = fmt.getNextRecord()) != null) {
// Print out the Frame Number
System.out.print("Record:" + rec.getRecNum());
// Print out the time
System.out.println(" Time:" + rec.getTime());
// Get this records packet
IPPacket p = rec.getPacket();
// Get the first header
Header h = p.getHeader();
// If IP6 IPPacket
if (p.getType() == IPPacket.IP6) {
// If IP6 Header
if (h.getType() == Header.IP6) {
// Cast to IP6 so we can access methods
IP6Header ip6 = (IP6Header) h;
System.out.println(h.getName() + " src:" + ip6.getSrcAddr() + " dst:" + ip6.getDstAddr());
// Print the header as hex
System.out.println(ip6.printHexHeader());
// Print a string representation of the header.
System.out.println("Complete " + h.getName() + ":\n" + ip6.toString(fmtprop));
// Get the rest of the headers
while ((h = h.getNextHeader()) != null) {
// If it is a TCP header
if (h.getType() == Header.TCP) {
// Cast so we can access methods
TCPHeader tcp = (TCPHeader) h;
System.out.println(h.getName() +" src:" + tcp.getSrcPort() +" dst:" + tcp.getDstPort());
System.out.println("Complete " + h.getName() + ":\n" + tcp.toString(fmtprop));
// If it is a UDP header
} else if (h.getType() == Header.UDP) {
// Cast so we can access methods
UDPHeader udp = (UDPHeader) h;
System.out.println(h.getName() +" src:" + udp.getSrcPort() + " dst:" + udp.getDstPort());
System.out.println("Complete " + h.getName() + ":\n" + udp.toString(fmtprop));
}
}
}
}
}
}
}
public static void main(String[] args) {
CommTraceExample e = new CommTraceExample();
}
}