0
        Packet packet = new Packet();

        while(packet != null )
        {

           packet = jpcap.getPacket();

           TCPPacket tcp = (TCPPacket)jpcap.getPacket();
           IPPacket ipp = (IPPacket)packet;
           UDPPacket udp = (UDPPacket)jpcap.getPacket();
           ipp = (IPPacket)tcp;
         }  

TCPPacket tcp = (TCPPacket)jpcap.getPacket(); this line got error jpcap.packet.Packet cannot be cast to jpcap.packet.TCPPacket why?please help to solve it .thanks

2 Answers2

1

Your code doesn't make any sense. You can't possibly know that the next two packets you receive will be a TCP packet followed by a UDP packet. Obviously you have received a packet that isn't a TCP packet and you are just casting it to TCPPacket based more on hope than experience. You have to look at the packet to see whatnot is before you start typecasting it.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

If (as I suspect) you are trying to work on one packet and cast it to various types you should read it once and then cast it.

From the docs I understand that consecutive calls will read consecutive packages: http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/javadoc/jpcap/JpcapCaptor.html#getPacket%28%29

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19