2 Replies - 321 Views - Last Post: 10 November 2017 - 03:14 AM Rate Topic: -----

#1 j_tom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-November 17

problem in parsing PCAP file

Posted 09 November 2017 - 09:41 PM

package com.javahelps.pcapparser;

import io.pkts.PacketHandler;
import io.pkts.Pcap;
import io.pkts.buffer.Buffer;
import io.pkts.packet.Packet;
import io.pkts.packet.TCPPacket;
import io.pkts.packet.UDPPacket;
import io.pkts.protocol.Protocol;

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException {

        final Pcap pcap = Pcap.openStream("C://slowdownload.pcap");

        pcap.loop(new PacketHandler() {
        	
        	@Override
            public boolean nextPacket(Packet packet) throws IOException {

                if (packet.hasProtocol(Protocol.TCP)) {
                	

                    TCPPacket tcpPacket = (TCPPacket) packet.getPacket(Protocol.TCP);
                    Buffer buffer = tcpPacket.getPayload();
                    if (buffer != null) {
                        System.out.println("TCP: " + buffer);
                    }
                } else if (packet.hasProtocol(Protocol.UDP)) {

                    UDPPacket udpPacket = (UDPPacket) packet.getPacket(Protocol.UDP);
                    Buffer buffer = udpPacket.getPayload();
                    if (buffer != null) {
                        System.out.println("UDP: " + buffer);
                    }
                }
                return true;
            }
        });
    }
}



DONOT UNDERSTAND PROBLEM WITH CODE. OT PUT IS KIND OF GARBAGE CHARACTERS NOT THE CONTENTS OF PCAP FILE

This post has been edited by ndc85430: 10 November 2017 - 01:08 AM
Reason for edit:: Added code tags. Please do this yourself in future.


Is This A Good Question/Topic? 0
  • +

Replies To: problem in parsing PCAP file

#2 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,482
  • Joined: 13-June 14

Re: problem in parsing PCAP file

Posted 10 November 2017 - 01:14 AM

A couple of things first of all:

1. Please use code tags when posting code here, because it keeps the formatting and adds syntax highlighting and line numbering. I've done this for you this time, but please do it yourself in future as you're asked in the box where you type your posts.

2. Please don't type in all capitals, as it looks like you're shouting.

OK. I've not used this library for reading pcap files before, but it might help to show us what kind of output you're getting and what you expect. Have you tried reading the file with something else (like tcpdump) to see that it contains the data you expect?
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3444
  • View blog
  • Posts: 15,606
  • Joined: 20-September 08

Re: problem in parsing PCAP file

Posted 10 November 2017 - 03:14 AM

Try
System.out.println("TCP: " + buffer.dumpAsHex());
and the same with the other
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1