SharpPcap - problem with package Capture to extract information from package

I am using SharpPcap to capture packets.

I am trying to get the value of a traffic class and I am using udp.ipv6.TrafficClass.ToString ().

the problem is that I am getting this exception:

An object reference is not set on an object instance.

private void packetCapturingThreadMethod()
{

   Packet packet = null;

   while ((packet = device.GetNextPacket()) != null)
   {
        packet = device.GetNextPacket();

        if (packet is UDPPacket)
        {
            UDPPacket udp = (UDPPacket)packet;

            MessageBox.Show(udp.ipv6.TrafficClass.ToString());
        }
   }
}

      

+2


a source to share


2 answers


This exception means that either udp

, udp.ipv6

or udp.ipv6.TrafficClass

is null. You need to check:



if (udp != null && udp.ipv6 != null && udp.ipv6.TrafficClass != null)
{
    MessageBox.Show(udp.ipv6.TrafficClass.ToString();
}

      

+3


a source


I think what is happening here is that you are actually only checking each package.

You don't need the second packet = device.GetNextPacket();

one because it is packet

already assigned at the top of the while loop.

Try this and see if you still have the exception:



private void packetCapturingThreadMethod()
{

   Packet packet = null;

   while ((packet = device.GetNextPacket()) != null)
   {
        if (packet is UDPPacket)
        {
            UDPPacket udp = (UDPPacket)packet;

            MessageBox.Show(udp.ipv6.TrafficClass.ToString());
        }
   }
}

      


If you are still getting the exception, then it is most likely because you are not receiving a valid ipv6 packet.

+4


a source







All Articles