dress; }
}
public string Source
{
get
{
if ( m_SourcePort != -1 )
{
return SourceAddress.ToString() + ":" + m_SourcePort.ToString();
}
else
{
return SourceAddress.ToString();
}
}
}
public string Destination
{
get
{
if (this.m_DestinationPort != -1)
{
return DestinationAddress.ToString() + ":" + m_DestinationPort.ToString();
}
else
{
return DestinationAddress.ToString();
}
}
}
public string DestinationAddress
{
get
{
return m_DestinationAddress;
}
}
}
}
在主
程序里
private Monitor m_PacketMonitors;
private ArrayList m_Packets;
private System.Windows.Forms.StatusBar statusBar1;
private int m_PacketsSize;
执行方法中
private void StartMonitor()
{
IPAddress hosts = Dns.Resolve(Dns.GetHostName()).AddressList;
if (hosts.Length == 0) { throw new NotSupportedException("This computer does not have non-loopback interfaces installed!");}
for (int i=0; i<hosts.Length; i++)
{
}
m_PacketMonitors = new Monitor;
m_Packets = new ArrayList();
m_PacketMonitors[0] = new Monitor(hosts[0]);
// 添加代理,每次有新的packet到时都出发下面哪个动作
m_PacketMonitors[0].NewPacket+=new Monitor.NewPacketEventHandler(this.OnNewPacket);
m_PacketMonitors[0].Start();
}
// 这个方法用于把packet显示到一个地方
private void OnNewPacket(Monitor m, Packet p)
{
m_Packets.Add(p);
m_PacketsSize += p.TotalLength;
try
{
txtLog.Text += p.Time.ToString()+p.Protocol.ToString()+p.Source.ToString()+p.Destination.ToString()+p.TotalLength.ToString();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
statusBar1.Text = String.Format("Intercepted {0} packet(s) [{1} bytes]", m_Packets.Count, m_PacketsSize);
}