-
使用
sharppcap
抓数据包
首先用到
PacketDotNet,Sharp
Pcap
这两个
DLL,
工具
Ethereal
可以分析包内容
class
Program
{
staticbool
showDetails =
false
;
//
查看详情的参数
privatestaticbool
BackgroundThreadSt
op =
false
;
//
线程停止标识
privatestaticobject
QueueLock
=
newobject
();
//
线程锁变量
privatestatic
List
new
List
//
待处理数据包队列
staticvoid
Main(
string
[] args)
{
//
显示
SharpPcap
版本
string
ver = nString;
ine(
, ver);
//
获取网络设备
var devices = ce;
if
(<
1
)
{
ine(
找不到网络设备
);
return
;
}
ine();
ine(
以下是目前本计算机上的活动网络设备
:
);
ine(
<
/p>
--
);
ine();
int
i =
0
;
foreach
(LivePcapDevicedev
in
devices)
{
ine(
, i, ,
ption);
i++;
}
//
选择要监听的网络设备
ine();
ine(
选择一个需要监听的网络设备
--
);
i =
int
.Parse(ne());
LivePcapDevice device = devices[i];
(
请选择操作:
监听
通讯
[C/c]
,
多线程监听通讯
p>
[T/t]
,
监听
统计
[F/f]
,发送随机数据包
p>
[S/s]?
);
string
resp = ne().ToUpper();
while
(!(With(
) ||
With(
) ||
With(
) ||
With(
)))
{
resp = ne().ToUpper();
}
try
{
if
(With(
) ||
With(
) ||
With(
))
{
//
监听过滤条件
string
filter =
;
//
连接设备
backgroundThread =
null
;
int
readTimeoutMilliseconds =
1000
;
if
(With(
))
{
(cuous, readTimeoutMilliseconds);
=filter;
= tics;
//
抓包统计
etArrival+=
new
Pa
cketArrivalEventHandler(device_OnPacketArrival); <
/p>
//
抓数据包回调
事件
< br>
Statistics +=
new
StatisticsModeEventHandler(device_OnPcapStatis
tics);
//
抓包统计回
调事件
}
elseif
(With(
))
{
(cuous, readTimeoutMilliseconds);
= filter;
= s;
//
抓包数据
showDetails =
th(
);
//
当抓数据包时,检查是否要查看详
情
etArrival +=
new
P
acketArrivalEventHandler(device_OnPacketArrival);
//
抓数据包回调
事件
}
elseif
(With(
))
{
backgroundThread =
new
(BackgroundThread);
();
();
=
filter;
= s;
//
抓数据包
showDetails =
th(
);
//
当抓数据包时,检查是否要查看详情
etArrival
+=
new
PacketArrivalEventHand
ler(device_OnThreadPacketArrival);
//
抓数
据包回调事件
}
ine();
ine(
当前
TCPdump
过滤条件
:
{
0
}
, filter);
ine(
p>
正在监听设备
{0},
按
'
回车
'
键以停止监听
...
,
ption);
//
开始监听
apture();
//
停止监听
ne();
pture();
in
e(
停止监听
.
);
if
(backgroundThread !=
null
)
{
BackgroundThreadStop =
true
;
();
}
}
elseif
(With(
))
{
//
连接设备
();
//
生成随机数据包
byte
[] bytes =
GetRandomPacket();
try
{
//
发送数据
cket(bytes);
SendQueuesqueue
=
new
SendQueue(
20
00
);
ine(
< br>单个数据包发送成功
.
);
for
(
int
j =
0
; j
<
10
; j++)
{
if
(!(bytes))
{
ine(
警告
:
队列大小不足以存放所有数据包,
将只发送部分
数据包
.
);
break
;
}
}
eue(squeue, onized);
ine(
数据包队列发送完毕
.
);
}
catch
(Exception e)
{
ine(
+ e);
}
}
}
catch
(Exception e)
{
ine(
+ e);
}
finally
{
if
()
{
//
断开设备连接
ine(tics().ToString());
();
ine(
断开设备连接
);
(
按
'
回车
'
键以退出
...
);
();
}
}
();
}
///
///
抓包方法
///
staticvoid
device_OnPacketArrival(
object
sender, CaptureEventArgs e)
-
-
-
-
-
-
-
-
-
上一篇:UPS启动和关闭操作说明
下一篇:艾默生UPS操作手册