0

我无法从列表中检索值 Packet.net 和 SharpPcap 是这里使用的两个库!我有 2 个执行主要工作的函数 第一个 调用此函数的函数ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessPacket), ph); 位于后台工作人员内部 这是调用 ProcessTcpPacket() 的函数

private void ProcessPacket(object p)
    {
        PacketHolder pHolder = (PacketHolder) p;
        RawCapture raw = pHolder.RawPacket;

        Packet packet = null;
        if (raw.LinkLayerType == LinkLayers.Ethernet)
        {
            packet = Packet.ParsePacket(PacketDotNet.LinkLayers.Ethernet, raw.Data);
        }
        else if (raw.LinkLayerType == LinkLayers.LinuxSLL)
        {
            packet = Packet.ParsePacket(PacketDotNet.LinkLayers.LinuxSLL, raw.Data);
        }

        while (packet.PayloadPacket != null)
        {
            packet = packet.PayloadPacket;
        }

        if (packet is TcpPacket && packet.PayloadData != null)
        {
            if (packet.PayloadData.Length > 0)
            {
                TcpPacket tcp = (TcpPacket) packet;
                ProcessTcpPacket(tcp, raw.Timeval, pHolder.Index); //called here!
            }
        }
        else
        {
            if (packet.PayloadData.Length > 0)
            {
                UdpPacket udp = (UdpPacket) packet;
                ProcessUdpPacket(udp, raw.Timeval, pHolder.Index);
            }
        }

    }

private void ProcessTcpPacket(TcpPacket tcp, PosixTimeval posixTimeval, ulong Index)
    {
        IpPacket parentPacket = (IpPacket) tcp.ParentPacket;

        IPAddress tcpSrcAddress = SourceIPList.Find(srcIP => srcIP.Equals(parentPacket.SourceAddress));
        IPAddress tcpDstAddress = DestinationIPList.Find(dstIP => dstIP.Equals(parentPacket.DestinationAddress));

        if (tcpSrcAddress.Equals(parentPacket.SourceAddress) || tcpDstAddress.Equals(parentPacket.DestinationAddress))
        {
            PacketDetails pd = new PacketDetails(); //class with simple getters and setters
            pd.Index = Index;
            pd.Time = posixTimeval;
            pd.Buffer = tcp.PayloadData;
            //TcpPackets.Add(pd);
            socketHelper.tcpPackets.Add(pd); //properly adds all the packets to list 

        }
    }

现在下面的函数在 SocketHelper 类中,我想在其中检索 tcpPackets 列表中的数据

public void ProcessTCPMessage(TcpClient tcpClient, NetworkStream stream, byte[] clientByte)
    {
        tcpPackets = new List<PacketDetails>(128);
        strRequest = Encoding.ASCII.GetString(clientByte, 0, clientByte.Length);
        myClient = tcpClient;
        strRequest = strRequest.Substring(0, 5);
        _form1 = new Form1();
        if (strRequest.Equals("Hello"))
        {
            for (int i = 0; i < 100; i++)
            {
                strResponse = tcpPackets[i].Buffer.ToString(); // here I get the list count as null and throws an exception..
            }
        }
        else
        {
            strResponse = "What?";
        }
        bytesSent = Encoding.ASCII.GetBytes(strResponse);
        stream.Write(bytesSent, 0, bytesSent.Length);
    }

我不知道我做错了什么!请帮忙!如果我在做一些愚蠢的事情,请尽量不要粗鲁!只是回复以为我是菜鸟!:P:D

4

2 回答 2

0

tcpPackets为空:它被初始化为一个初始容量为 128 个项目的列表,但它不包含任何项目(没有向其中添加项目的代码),因此尝试访问其前 100 个项目将导致错误。

于 2013-10-22T09:46:46.840 回答
0

tcpPackets您在方法的开头实例化列表,但在循环ProcessTCPMessage中使用它之前没有放入任何元素。for因此,当您尝试 reedBuffer列表元素的属性时,该元素为空(或更准确地说,null),导致NullReferenceException.

于 2013-10-22T09:46:58.957 回答