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