2 c! P+ t& u6 s9 }7 {8 I class Ping - y* l" |0 ~5 D @2 @) Q) R
$ s; N- L; T/ V% b
{ ; k1 e) ^- x* [
9 y$ x$ n; F9 ~3 O, e //声明几个常量 # r! Q; U/ b& v% u- H3 P! X& d
4 u6 t/ k: [0 }: u+ \7 Q; V const int SOCKET_ERROR = -1; " P/ l/ l' e3 }: Y% z
7 L1 ?' ~2 }' _0 r, `
const int ICMP_ECHO = 8; 0 k; Z; s5 P* A$ p* q1 p2 ^$ a 4 W" ~+ Z1 i" `$ v. Q
/// <summary> " r k! J |3 O% Q
. T# E( K: U% A3 q, Q9 _( s /// 这里取得Hostname参数 5 u. ~. S& ^, V# Q1 F ( N3 M- s: H7 i! [) g" w# P" w* w /// </summary> 4 S- w A7 w" a9 c6 e0 X& B
# e! ?4 X$ k+ z2 l1 [ public static void Main(string[] argv) $ o% ~0 P( a1 Z$ P0 k# c2 D' s/ h/ m 3 c0 _8 y+ A5 j7 H; n; Y$ Q { / n& C% Z s5 ^ H$ U
' S4 T% E$ y. n* |" t
if(argv.Length==0) ; T0 W+ D2 X \7 g; N5 C3 K9 C/ r 0 f) P# m& P$ n& s+ m' @3 e { 7 | t- s# V! h: m- q* c/ K
2 }5 K+ V+ p8 i8 B. ^7 }' i1 u3 o* p
//If user did not enter any Parameter inform him 0 b( F% y" z" ` + Y. N% S! `2 T: U, @
Console.WriteLine("Usageing <hostname> /r") ; 9 g/ P, i8 k) f) j% \' I2 b% E + Z$ V' K' A/ W; A0 o
Console.WriteLine("<hostname> The name of the Host who you want to ping"); & m- p3 Q! r' |6 G. G9 w
' O" t* m( |. |
Console.WriteLine("/r Ping the host continuously") ; . u3 C' Q0 b6 W ' g$ k% E3 H" e( o' G
} " b" f4 h* S$ `6 ]+ E5 I
; k2 [4 t1 Z2 ]: \3 `
else if(argv.Length==1) 7 I/ ^' L9 U2 u' P) D; i6 C0 k
3 D1 q, n& [( z# n s
{ $ J, {: A/ w1 t* f, ^/ ]8 Q( b
5 y5 |# F! }( n8 I4 l //Just the hostname provided by the user " P- l0 o, m" i, i" S* r" k5 k3 S
0 r5 \; x. f' G% b. R( O
//call the method "ingHost" and pass the HostName as a parameter ( i; P9 @$ a- T+ _, J v& V
! D, A/ R2 w% y9 Z/ u
PingHost(argv[0]) ; - A% B4 N+ D( q2 u- Y $ y2 b! B0 ^/ m
} : F0 q1 U- e5 n5 D
. W z! v8 q# q/ B2 b8 R else if(argv.Length==2) 3 H& n3 K0 `( ]3 {+ I: g% J8 j- s9 K ' f' J- b. S& T4 r `% h# q4 k; {7 o
{ : p9 ]% G9 X$ g! i5 ` ) j3 p4 B+ d2 v9 X, d4 C# N //the user provided the hostname and the switch ) X4 M8 D% l* V4 [" e ' ?7 K; h( W- n. ]1 N E
if(argv[1]=="/r") 4 B5 N) p! v6 ]2 Z' W$ V/ u3 S
' s% J7 L6 w/ ~: {
{ s- t: c7 ?3 [3 Y) z7 e 4 Z, W* R+ f- [. V+ q# {/ m4 j //loop the ping program . A8 f+ w f8 v) V; b
+ X8 u( w0 ]: B% Y3 O0 I while(true) . q; T+ j( Z# U* }3 v % `9 E3 K D! G& g* q4 X0 f- j
{ # E0 E; G3 K0 l/ E/ D 8 {4 K3 W9 J3 V0 [: S( O
//call the method "ingHost" and pass the HostName as a parameter ) C5 g$ y. H- S4 A
; V7 T" }; m" |- @* {% ]* U$ ^/ i
PingHost(argv[0]) ; & Q* d" f4 g0 A. K4 a; }: s$ w
; r+ B1 b' C9 j. Z4 F x0 M } % a% }2 h& }: b; g: _& h, h + p1 L/ ]7 [' j' H, @% P* N } 5 u2 N# V, B" Y; `$ \
! Q: V. C9 ~+ ~- m9 }3 T else 1 _' U/ @6 Y4 y 3 `1 ^/ V0 N' h; A* }3 p
{ - {& ~1 G+ X& ^ ' z3 c: ~+ h p7 d# Q: o //if the user provided some other switch $ \4 |( x3 s; {" b8 b
$ j l# Z8 t4 y0 K1 O
PingHost(argv[0]) ; 9 e6 `5 I$ H+ H8 X- K" {: o s. v/ |5 i7 w& t
} - G! ? B# C' k* p: x6 C ! Z% E6 X+ d z1 v% h: j2 M } / p; |& l9 q& R$ L
4 d, `( V8 D: f5 B0 d* @: o else - w/ j% j+ U+ p$ o; D
# A! {1 g, j3 l
{ " q4 l% ]' o l5 W; w) N6 t 9 E0 W- |3 m9 n
//Some error occurred 8 m4 ^8 r# [9 R; I+ X2 }6 R8 D9 R , O/ r/ @* H6 F( `7 M2 H& p3 `& X
Console.WriteLine("Error in Arguments") ; 4 q/ i- n; m" ? k # L- R! s% K! |. x$ q R } 2 L$ l" U A7 D
2 P8 _2 J& G! X* m } 8 s0 J. Y( I9 @. t$ S/ z# m/ V" h 3 k$ R( l5 E- @
5 @! B* ]2 ]+ Y) `9 m" h2 p
/// <summary> ) H4 S7 l3 K- p w
5 a, C; E- L4 ^0 u6 v
/// 主要的方法,用来取得IP, 5 g! K, v0 d- W+ `& q- { - f1 W( e! I4 f5 B W3 N! K, v /// 并计算响应时间 & w6 `% Y! q! T& ]4 k' a2 l
s6 p% ^: a* X' F& N4 J" c. P /// </summary> 5 j' r2 X$ z1 y' r4 g" x0 B$ P6 v+ u 1 u: Z1 |) x; o! f x& ?% u) ~
public static void PingHost(string host) & o. p/ [" S' c" Y% D( r0 h# b 9 x g- p" Q$ `) {8 k
{ 5 s' I& I9 x7 s- C
7 m* z' L" Y' e" e2 Y) L
//Declare the IPHostEntry " ^+ `6 ?# G. G% e4 x% S
+ N' X. {6 G" P, g( F1 Q: `6 D. g
IPHostEntry serverHE, fromHE; 1 o; I& J1 \0 P2 l5 |, g) ?% L: S
: R0 ]/ \9 L. x int nBytes = 0; 6 n- {! L7 ?" h+ N5 t
; q# M* g6 V4 O, s* L
int dwStart = 0, dwStop = 0; , q+ v/ I- P* z% Y % ]. I9 U, Y0 T: j5 w
//Initilize a Socket of the Type ICMP 8 J% N% J) d' |4 f' a; p' _ 9 d( Z6 B* a- y3 Q/ F* N: v
Socket socket = _1 o+ j+ [& P0 o7 v/ ?
% l! o2 ~* g* w
new Socket(AddressFamily.AfINet, SocketType.SockRaw, ProtocolType.ProtICMP); " a' s/ L" N# o2 Q
; s. T4 P5 L5 B1 ]
. e* k* Y6 h* p7 T# K2 b# r( |
// Get the server endpoint ; Z( |: B' d2 Q6 w4 [/ Q J/ X8 i" t! T2 ? Y' y. D try % l8 n9 p+ c) g- V/ X& A+ U 0 w% M: ]7 X7 e( i: G. P
{ 8 U. t$ F- U6 K" s/ P! b" b1 d
$ v* L. e) ^2 _* G, e+ ^9 l serverHE = DNS.GetHostByName(host); - F( s8 y3 u7 k7 h6 B: k( j
$ R2 s2 X. @. p* P
} 7 p, w$ u: u1 t1 B3 s. }
5 x+ Y: S9 m( g
catch(Exception) - p" t! ]- |- y+ f6 y4 k& D1 P / s. P2 p' r6 X8 i. X
{ ) Q& E2 y+ c' b5 R - y' I- H- S3 x Console.WriteLine("Host not found"); // fail 0 [4 p* i2 @% N$ G( T1 T" h8 U
. T1 @) H( Z7 y. c, t
return ; . M5 S; [+ H6 v" h2 ]1 d
5 f+ n( M3 A2 }: N* U1 t! n0 ]
} # l3 \4 U9 p; { x \6 G8 M . L" v3 G: }( g: G + @) A* G: E) ?% ~3 v* |
// Convert the server IP_EndPoint to an EndPoint / l8 c; ]6 T( ^3 V" s* _ : u r; o1 O2 J! i/ ~
IPEndPoint ipepServer = new IPEndPoint(serverHE.AddressList[0], 0); ' w- V L: E: Z
# J: x/ G1 ? t3 `% H EndPoint epServer = (ipepServer); , ^ n& I* c8 z: S2 n4 {) B. q $ I( j" D7 i# d6 Y
8 Y' f$ V, F- w, a; T, ]
// Set the receiving endpoint to the client machine . {& R0 k( o$ k/ |
3 _; b: Q+ x- _ fromHE = DNS.GetHostByName(DNS.GetHostName()); 4 G& U5 Y, X3 l3 \
5 r; }, k/ }' c5 R5 r5 v
IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList[0], 0); ; L7 |; K, P) @5 G4 k
. _% H; q$ C6 s2 y( {( @7 l
EndPoint EndPointFrom = (ipEndPointFrom); : W. l& J; R3 \5 F
- L) X: |8 @ p/ {+ y+ M5 `$ G
; _6 O& Z0 S1 d int PacketSize = 0; " F( e. Q |; x & s6 H" z9 L% m- ?/ S! ]1 l
IcmpPacket packet = new IcmpPacket(); ( a K& C0 ^5 r9 K8 v0 j' R
, e, y! ]2 B; |' g" M' m+ B
// Construct the packet to send * c" ]* k- [( c4 H) {( p1 {5 k " \# o4 u3 s1 r( Q) m
packet.Type = ICMP_ECHO; //8 " h& e2 C. K" V g' q* v9 t
8 e9 n/ {& s1 O
packet.SubCode = 0; . p" G9 B5 w. z7 K! }) a
/ |) [9 I# ]. ]6 K; E* R packet.CheckSum = UInt16.Parse("0"); 2 [5 W4 m( f7 m( B * [& l6 g4 `1 U/ s) U: e
packet.Identifier = UInt16.Parse("45"); . P3 y' R3 Q4 k) _% g+ b* c : Z: o& `' R- h4 T9 d, ]
packet.SequenceNumber = UInt16.Parse("0"); 9 \1 ]& X2 r9 R% n . F2 I! f) w; Z6 T* y0 c r
int PingData = 32; // sizeof(IcmpPacket) - 8; 6 j( D& Q) z! e; }6 N" K + z i* j# F$ Y
packet.Data = new Byte[PingData]; 5 i- d2 m" i7 u% ^ g" ]; o0 X! _ : J% m3 } s9 X w0 I$ b/ F) F6 N9 ?9 c //Initilize the Packet.Data ; u3 S% V# r8 K - V# a* i9 Y# t& A
for (int i = 0; i < PingData; i++) 7 X# d* B3 M3 t8 C f+ `
/ d: B+ r- Q5 l" V
{ % T8 O: p0 p6 b h% O
0 v; j8 S6 z; [ v9 I packet.Data = (byte)'#'; ' C5 n8 V4 l' r0 `! _- q' ] - ?; i7 C: z; ^2 } H. N+ e0 u
} 4 V7 l7 B5 w0 B# Q0 V; p1 s
' T4 D: R! c/ z) C% N+ f8 a) Z
* i& d- ?7 x# j) v7 @- o7 H/ i' J
//Variable to hold the total Packet size - `* }9 V5 }3 ~+ J0 `, g/ Y
8 N' x/ i$ ?+ z/ T) M
PacketSize = PingData + 8; " I+ R: b5 W1 R% |' ^& T . f0 [0 {; j3 L9 w' r/ X" x' P Byte [] icmp_pkt_buffer = new Byte[ PacketSize ]; 8 |5 P' E" o# k
, \* N, [1 N; b2 T6 `
Int32 Index = 0; # t" K3 O- l2 D- ?7 V" K9 M" | $ g Y! R' V3 ~; ?& d0 l( w& [2 a6 q //Call a Method Serialize which counts 8 M! Z }# w* n+ W, F - a5 X) Q0 Z- }0 c7 B0 W0 E //The total number of Bytes in the Packet 8 O1 E. z- w {4 r
+ E/ N5 v L' L/ m* Z: ~
Index = Serialize( 5 e1 {. s2 U% i! W" R; w# u
" M$ e9 z, L1 `: T4 I- }+ h
packet, ) ]" |7 v: k% n( s, z
* c& ^' N4 X1 s. R. e
icmp_pkt_buffer, * m. j2 K' B& G9 V: H4 O+ `
( V" L- @/ F2 r0 G1 I
PacketSize, & r5 W* `# ?& l- {( [ W * p8 Q' i; ~# e1 w
PingData ); ( m1 [4 b5 f' q7 z1 k) r, U1 q
7 f5 ?9 S6 o% o) ~1 [ ^0 v. R
//Error in Packet Size , u# C/ l4 ?8 {# G& p ; m% \. v2 w9 G2 b; L
if( Index == -1 ) 8 c" L! c O- t' o ' D2 s7 s4 T( y8 r
{ ; O K3 q9 W; i
1 v+ F- ~4 x# l/ U' O6 M
Console.WriteLine("Error in Making Packet"); ( t! t$ \5 E' m . [1 ]0 E& ~" P1 ^' s4 D5 g return ; # C0 g2 x3 q8 j" K% [. s ' z: |, T& V% N3 q } 6 P# x/ z) [* Z7 \+ z2 ~% b4 k' n
! d% B1 ^1 Q. F& c6 X$ e! k, a
/ {5 u Q- ~2 ]3 Q5 \ // now get this critter into a UInt16 array ' K' H2 }# k/ v! c0 \ ^4 u
' f/ R0 h& f6 {
0 s3 W* n3 P) @2 @9 A5 H
//Get the Half size of the Packet z1 Q/ n }6 R+ @) p
9 [( {. K" j8 U, _" {7 S
Double double_length = Convert.ToDouble(Index); & X; ^' {9 s' N9 |: {
/ g5 @% Q' I+ Y. l' l8 n Double dtemp = Math.Ceil( double_length / 2); , c2 p; j. k% H- [5 F 8 g! w2 d) V! d0 I1 X int cksum_buffer_length = Convert.ToInt32(dtemp); # W) U q0 v& B7 b / Q" n4 \) [8 c" H. F
//Create a Byte Array % C( ]+ e' m( Q0 R6 p 8 Q2 G/ B8 d' U; d, R
UInt16 [] cksum_buffer = new UInt16[cksum_buffer_length]; # ^. U; Y/ Z6 A3 l! G5 I 0 G. C% ]. q; h( w! k8 F1 E //Code to initialize the Uint16 array 4 K3 K" ]! z6 i' N0 S
$ h4 F) ~4 Y8 k- t' C7 s8 @
int icmp_header_buffer_index = 0; 9 a+ w, O# Q# O b, q * G+ y& k! W8 N( d' t# E7 v
for( int i = 0; i < cksum_buffer_length; i++ ) { ! T6 d f$ _: e6 S8 F
+ _4 C& m7 Y" k cksum_buffer = 7 [9 {5 ~2 K3 S ^( o6 g7 x
. |, g5 M6 y! V& ?; r* I+ x BitConverter.ToUInt16(icmp_pkt_buffer,icmp_header_buffer_index); 7 \% W p( R a8 |