添加元素后,列表计数为null

时间:2013-10-22 09:39:13

标签: c# sharppcap packet.net

我无法从列表中检索值 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);
    }

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您正在tcpPackets方法的开头实例化ProcessTCPMessage列表,但在for循环中使用它之前不会放入任何元素。因此,当您尝试reed list元素的Buffer属性时,该元素为空(或更准确地说,null),从而产生NullReferenceException