C#SharpPcap问题

时间:2011-01-04 15:58:27

标签: c# tcp sharppcap

我一直对使用SharpPcap非常感兴趣,但到目前为止它并不顺利。

主要问题是以下代码:

   private static void device_OnPacketArrival(object sender, CaptureEventArgs packet)
   {
        if(packet is TCPPacket)
        {                
            DateTime time = packet.Timeval.Date;
            int len = packet.PcapHeader.len;

            TCPPacket tcp = (TCPPacket)packet;
            string srcIp = tcp.SourceAddress;
            string dstIp = tcp.DestinationAddress;
            int srcPort = tcp.SourcePort;
            int dstPort = tcp.DestinationPort;

            Console.WriteLine("{0}:{1}:{2},
                {3} Len={4} {5}:{6} -> {7}:{8}", 
                time.Hour, time.Minute, time.Second, 
                time.Millisecond, len, srcIp, srcPort, 
                dstIp, dstPort);
        }
    }
  

“TCPPacket可以使用的类型或名称空间   找不到“

好的,所以我认为它一定是TcpPacket? - 然后它出现了这个错误:

  

“给定的表达式永远不会出现   提供('PacketDotNet.TcpPacket')   类型“

忽略:

  

“'SharpPcap.CaptureEventArgs'没有   包含'Timeval'的定义   没有扩展方法'Timeval'   接受第一个类型的参数   'SharpPcap.CaptureEventArgs'可能是   发现“

等等,等等。所以我的问题是,我错过了什么吗?

我有PacketDotNet和SharpPcap库,并添加了两个using语句。

解决方案:         包pack = Packet.ParsePacket(packet.Packet);         TcpPacket tcpPacket = TcpPacket.GetEncapsulated(pack);

    DateTime time = packet.Packet.Timeval.Date;
    int len = packet.Packet.Data.Length;

    if (tcpPacket != null)
    {
        IpPacket ipPacket = (IpPacket)tcpPacket.ParentPacket;


            IPAddress srcIp = ipPacket.SourceAddress;
            IPAddress dstIp = ipPacket.DestinationAddress;
            ushort srcPort = tcpPacket.SourcePort;
            ushort dstPort = tcpPacket.DestinationPort;

            MessageBox.Show(String.Format("{0}:{1}:{2},{3} Len={4} {5}:{6} -> {7}:{8}",
                                time.Hour, time.Minute, time.Second, time.Millisecond, len,
                                srcIp, srcPort, dstIp, dstPort)
                );
    }

2 个答案:

答案 0 :(得分:3)

解决方案:

Packet pack = Packet.ParsePacket(packet.Packet);
TcpPacket tcpPacket = TcpPacket.GetEncapsulated(pack);

DateTime time = packet.Packet.Timeval.Date;
int len = packet.Packet.Data.Length;

if (tcpPacket != null)
{
    IpPacket ipPacket = (IpPacket)tcpPacket.ParentPacket;


        IPAddress srcIp = ipPacket.SourceAddress;
        IPAddress dstIp = ipPacket.DestinationAddress;
        ushort srcPort = tcpPacket.SourcePort;
        ushort dstPort = tcpPacket.DestinationPort;

        MessageBox.Show(String.Format("{0}:{1}:{2},{3} Len={4} {5}:{6} -> {7}:{8}",
                            time.Hour, time.Minute, time.Second, time.Millisecond, len,
                            srcIp, srcPort, dstIp, dstPort)
            );
}

答案 1 :(得分:2)

查看代码的第一部分......

private static void device_OnPacketArrival(object sender, CaptureEventArgs packet)
   {
        if(packet is TCPPacket)
        {    

数据包似乎是CaptureEventArgs类型,而不是TCPPacket。可能事件args的某些属性是你的实际数据包。如果这是正确的,那么

  

“'SharpPcap.CaptureEventArgs'没有   包含'Timeval'的定义   没有扩展方法'Timeval'   接受第一个类型的参数   'SharpPcap.CaptureEventArgs'可能是   发现“

因为这个原因可能是真的; CaptureEventArgs和Packet不是一回事。

修改

我会尝试类似的事情:

private static void device_OnPacketArrival(object sender, CaptureEventArgs packet)
   {
        if(packet.packet is TCPPacket)
        {               
            TCPPacket tcpPack = (TCPPacket)(packet.packet);
            DateTime time = tcpPack.Timeval.Date;
            int len = tcpPack.PcapHeader.len;           
            string srcIp = tcpPack.SourceAddress;
            string dstIp = tcpPack.DestinationAddress;
            int srcPort = tcpPack.SourcePort;
            int dstPort = tcpPack.DestinationPort;

            Console.WriteLine("{0}:{1}:{2},
                {3} Len={4} {5}:{6} -> {7}:{8}", 
                time.Hour, time.Minute, time.Second, 
                time.Millisecond, len, srcIp, srcPort, 
                dstIp, dstPort);
        }
    }