<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0065)http://www.tamirgal.com/home/PageView.aspx?Item=SharpPcapTutorial -->
<HTML dir=ltr><HEAD><TITLE>SharpPcap tutorial: a step by step guide to using SharpPcap</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<STYLE type=text/css>BODY {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
P {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
H1 {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
H2 {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
H3 {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
H4 {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
H5 {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
H6 {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
LI {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
TD {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
TH {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
DD {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
DT {
FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif
}
BODY {
FONT-WEIGHT: 100; FONT-SIZE: 12px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; TEXT-DECORATION: none
}
P {
FONT-WEIGHT: 100; FONT-SIZE: 12px; WIDTH: 92%; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; TEXT-DECORATION: none
}
H3 {
FONT-WEIGHT: bold; FONT-SIZE: 18px
}
H4 {
FONT-WEIGHT: bold; FONT-SIZE: 16px
}
H5 {
FONT-WEIGHT: bold; FONT-SIZE: 10px
}
DIV#CodeBlock {
BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 3pt; BORDER-TOP: black 1px solid; PADDING-LEFT: 3pt; PADDING-BOTTOM: 3pt; FONT: 9pt "Courier New", Courier, mono; BORDER-LEFT: black 1px solid; WIDTH: 90%; PADDING-TOP: 3pt; BORDER-BOTTOM: black 1px solid; WHITE-SPACE: pre
}
BODY {
MARGIN: 0px
}
.style3 {
FONT-WEIGHT: 100; FONT-SIZE: 12px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; TEXT-DECORATION: none
}
A.style3:link {
TEXT-DECORATION: none
}
A.style3:hover {
TEXT-DECORATION: underline
}
.fixlink {
FONT-WEIGHT: 100; FONT-SIZE: 12px; COLOR: #000000; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; TEXT-DECORATION: none
}
A.fixlink:link {
TEXT-DECORATION: none
}
A.fixlink:hover {
TEXT-DECORATION: underline
}
.small {
FONT-WEIGHT: 100; FONT-SIZE: 10px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; TEXT-DECORATION: none
}
.small_pad {
PADDING-LEFT: 25px; FONT-WEIGHT: 100; FONT-SIZE: 10px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; TEXT-DECORATION: none
}
.menu {
FONT-WEIGHT: 100; FONT-SIZE: 12px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; TEXT-DECORATION: none
}
A.menu {
COLOR: #0000cc; TEXT-DECORATION: none
}
A.menu:hover {
TEXT-DECORATION: underline
}
A.menu:visited {
COLOR: #0000cc
}
A:link {
TEXT-DECORATION: none
}
A:visited {
TEXT-DECORATION: none
}
A:active {
TEXT-DECORATION: underline
}
A:hover {
TEXT-DECORATION: underline
}
.navtable {
BORDER-RIGHT: black 1px dashed; PADDING-RIGHT: 10px; PADDING-LEFT: 10px
}
.table {
PADDING-RIGHT: 10px; PADDING-LEFT: 10px
}
.packages {
PADDING-RIGHT: 10px; BORDER-TOP: black 1px dashed; PADDING-LEFT: 10px
}
.style5 {
FONT-WEIGHT: bold; FONT-SIZE: 18px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif
}
.style7 {
FONT-SIZE: 12px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif
}
.textsm {
FONT-SIZE: 14px; COLOR: #000000; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif
}
.textbg {
FONT-SIZE: 20px; COLOR: #000000; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif
}
.textreg {
FONT-SIZE: 12px; COLOR: #979797; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif
}
.counter {
FONT-SIZE: 10px; COLOR: #979797; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; TEXT-ALIGN: center
}
DIV#menupanel {
BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; BACKGROUND: #ffcc33; FLOAT: left; PADDING-BOTTOM: 10px; BORDER-LEFT: black 1px solid; PADDING-TOP: 10px; BORDER-BOTTOM: black 1px solid
}
</STYLE>
<META content="MSHTML 6.00.6000.16640" name=GENERATOR></HEAD>
<BODY>
<TABLE style="BACKGROUND-IMAGE: url(images/bg.gif)" cellSpacing=0 cellPadding=0
width="100%" border=0>
<TBODY>
<TR>
<TH vAlign=top scope=col width="50%" height=85>
<DIV align=left><IMG alt="tamirgal.com logo"
src="SharpPcap%20tutorial%20a%20step%20by%20step%20guide%20to%20using%20SharpPcap_files/logo.gif"
align=top></DIV></TH></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=navtable vAlign=top><BR><BR>
<DIV id=menupanel>
<P class=menu align=left><STRONG>tamirgal.com</STRONG><BR><A class=menu
href="http://www.tamirgal.com/home/default.aspx">Home</A><BR><A class=menu
href="http://www.tamirgal.com/home/dev.aspx">Programming</A><BR><A
class=menu
href="http://www.tamirgal.com/home/gallery.aspx">Gallery</A><BR><A
class=menu
href="http://www.tamirgal.com/home/links.aspx">Links</A><BR></P></DIV></TD>
<TD class=table vAlign=top width=1131><BR><BR>
<H2>SharpPcap tutorial: a step by step guide to using SharpPcap</H2>
<P>The text of this tutorial is taken directly from WinPcap's <A
href="http://www.winpcap.org/docs/docs31/html/group__wpcap__tut.html">official
tutorial </A>but is modified to show the C# use of the SharpPcap library.
All examples can be downloaded together with SharpPcap source code from <A
href="http://www.tamirgal.com/home/dev.aspx?Item=SharpPcap">SharpPcap</A>
's homepage. The WinPcap library must be installed before attempting to
run any of these examples, so please download and install the latest
version from <A
href="http://www.winpcap.org/install/default.htm">WinPcap's download
page</A>.</P>
<P>SharpPcap was written and tested using .NET v1.1 and Windows 2000/XP. I
have no idea about other .NET and Windows versions. If you do try it,
please report your results.</P>
<P>The following topics are covered in this tutorial:</P>
<OL>
<LI><A
href="http://www.tamirgal.com/home/PageView.aspx?Item=SharpPcapTutorial#ifList">Obtaining
the device list</A>
<LI><A
href="http://www.tamirgal.com/home/PageView.aspx?Item=SharpPcapTutorial#ifListAdv">Obtaining
advanced information about installed devices </A>
<LI><A
href="http://www.tamirgal.com/home/PageView.aspx?Item=SharpPcapTutorial#basicCap">Opening
an adapter and capturing packets</A>
<LI><A
href="http://www.tamirgal.com/home/PageView.aspx?Item=SharpPcapTutorial#basicCapNoCallback">Capturing
packets without the event handler</A>
<LI><A
href="http://www.tamirgal.com/home/PageView.aspx?Item=SharpPcapTutorial#filter">Filtering
the traffic</A>
<LI><A
href="http://www.tamirgal.com/home/PageView.aspx?Item=SharpPcapTutorial#dumpTCP">Interpreting
the packets</A>
<LI><A
href="http://www.tamirgal.com/home/PageView.aspx?Item=SharpPcapTutorial#offline">Handling
offline dump files</A>
<LI><A
href="http://www.tamirgal.com/home/PageView.aspx?Item=SharpPcapTutorial#sendPackets">Sending
Packets</A>
<LI><A
href="http://www.tamirgal.com/home/PageView.aspx?Item=SharpPcapTutorial#statistics">Gathering
Statistics on the network traffic</A> </LI></OL>
<H3 id=ifList>1. Obtaining the device list </H3>
<P>Typically, the first thing that a WinPcap-based application does is get
a list of attached network adapters. SharpPcap provide the
<CODE>GetAllDevices()</CODE> function for