% U, T$ W# S$ |5 J0 {0 v" I# @8 R else if(argv.Length==1) , T2 ~) X2 L* R6 [: E
9 Y7 E4 L( V/ a" S0 ?+ b7 t J
{ 4 m9 J% E- p0 j$ X( b$ L ; [- S! r% _; i4 t( B( w
//Just the hostname provided by the user 3 }% c2 ]* V) A
2 P2 ?- }' P" G4 A
//call the method "ingHost" and pass the HostName as a parameter % W, W/ z% o0 S1 t8 E
# j. f O1 K" y3 Q
PingHost(argv[0]) ; 0 W% Q2 s3 R/ B }* Y + o3 o! y7 v" f: j2 x } $ Y @0 A7 Q* D. ]9 |9 }+ o0 g! s , u, W: ]- p2 Y8 w: f. {+ \( s, d
else if(argv.Length==2) 1 H0 ?( j: J: Z- e+ ?9 y& S7 m
8 m: }; e) }7 Z4 ?; H* l: P0 B; `% t { 0 j# L7 l! A# ~, z& Q v+ M: `
& B/ e' d( ]1 a+ Z. v/ k$ }5 p# A
//the user provided the hostname and the switch 6 C2 S5 l2 d! n/ \6 W8 s
3 l3 N* M) ^/ W( Y6 N9 s; x if(argv[1]=="/r") 2 [' y% O) ~1 Z7 e" H, ?
# s/ x9 P$ K% i+ g { ; J" V1 j' B# p$ H! W
/ Q& M$ p7 b; g6 ~ `. F //loop the ping program 4 j7 ?, n/ E3 e6 L2 d i0 o7 j # S- F+ L/ S& h+ M0 R6 ]
while(true) . V2 y" o8 n8 B2 }+ E 2 v5 j+ Z; Y* ^ { 9 Y, I0 d6 G y0 ?( s1 `5 ]% @9 u a5 E2 m) i8 \) |$ l8 _
//call the method "ingHost" and pass the HostName as a parameter 1 p8 |( m+ B4 j 6 t3 \0 I6 j% U$ M PingHost(argv[0]) ; ; _2 f5 A! U0 X
2 g3 V- M. Y0 [$ U, j- h% O& z3 p } / B( ~+ P' I0 t% m6 }9 ~ 4 c4 P# x+ ^ H8 Z7 B } 7 x3 p5 Y0 Q) I$ l: R3 t# G; U 1 n5 \8 n p: i% `7 A; R else : U/ P T6 x j( k- n
* W4 u* w( x: P% i, z0 c, s
{ 8 i. B% @5 f4 E# |8 p
8 q3 ]0 z, d5 c# w! e% p
//if the user provided some other switch 0 A) d! V/ b& ^
* E& B0 I3 l# d PingHost(argv[0]) ; ) e( ~0 T: I# G# K+ i ' W' G. w4 y% j+ Z6 |0 d9 T0 q } + a& [! U% {- I$ g) r/ c & i b" L0 ] b9 c, w; w/ j
} * v4 o( M5 Q7 k$ l
2 y+ X# ^ `7 O8 A/ p% z6 f6 c$ s5 F
else 0 F4 B0 R7 y, i ' [+ E' v% R- [3 w( W# u! G2 o2 P { 8 i6 G9 x. n1 f( f2 J( d/ c 9 n, I; n# M- T# [; P+ k
//Some error occurred 7 l2 W5 n" J: k7 M4 ]5 S/ E2 H ' M9 [+ N# S3 G; t) C: b( ^
Console.WriteLine("Error in Arguments") ; $ Q9 l m: [7 [1 b o! Q
. Q0 J# p, n. v5 j } 3 h9 e$ ^4 k4 q! n. b: @& P ! O0 o' l7 b) D: C2 B- k0 Q
} 1 k/ V9 {, F4 x# b" a
; }: K- s5 v% ]& ?. G; P
6 C" U4 N7 N( E2 h r5 z/ z
/// <summary> / i6 L9 v, A4 d
: @/ e% g0 s2 |9 F /// 主要的方法,用来取得IP, 7 B% U& o% j; p
7 _' `3 Y& ?0 n" J* _9 Z/ u" X /// 并计算响应时间 - a8 b; s0 {2 K- ?) v" O" H Y( d ; p6 V# s5 `; |4 D* } /// </summary> 0 \% j1 E! x. q' T6 J1 s 9 \# p! |, G; c! F' r public static void PingHost(string host) 9 D2 q o/ B" x- q' f3 y& `
2 s8 J) _7 P4 U# s3 y$ [ { 3 f% O# m, T. _! ~ * G V! a9 @) ?7 s( |! d
//Declare the IPHostEntry - k% F5 f0 l) n* J: c- s2 _8 k# v
/ y3 T$ F+ A+ K
IPHostEntry serverHE, fromHE; ( E: N" t/ t0 x" ?/ R& G . _) n8 G1 h- ]8 x' A$ k- J
int nBytes = 0; + e+ {* B6 A' b9 o
# T" o/ m1 m: H+ y
int dwStart = 0, dwStop = 0; . O' X( Y W9 d- x0 Y' F9 s 6 [6 w: g0 e5 A; K5 j3 T //Initilize a Socket of the Type ICMP & U) z2 C4 j( P; d * B/ G- j' q3 z" a7 X. [ Socket socket = $ p! n v' q( X8 l1 b - W( m4 @2 l5 x( q/ w. f% {9 k. |, U! E+ h new Socket(AddressFamily.AfINet, SocketType.SockRaw, ProtocolType.ProtICMP); I$ X6 Z: h: {; H$ @
6 ~" N& o6 h1 [/ C $ P! p. p. I9 `1 M
// Get the server endpoint % ~! K4 ]% a# z9 \, @ ?0 m' t& z8 g3 |- k- @: A+ a! ]
try ' k) y8 _& ~* j8 C# Q
/ X- x, W9 h+ r } ! X- D% B! e: R* W - ^4 c; _! ^3 Y/ K* y4 j ; e- j2 z- ?3 u7 ^* Y
// now get this critter into a UInt16 array 2 j: t" {3 |5 U+ a( Y. S ! k! M# Q* H# T% P+ O
2 ^( v, U3 r! K1 u2 L, P8 f* n //Get the Half size of the Packet . }- a4 H6 Q4 _8 p, D# S% y. W $ X3 N* G- p4 N' s+ k
Double double_length = Convert.ToDouble(Index); ; V2 ]& ^. E4 L9 N* R. i
" J1 M3 n6 j5 U% h4 S
Double dtemp = Math.Ceil( double_length / 2); 2 C9 M4 J; N, w% l& x- s- e , T( G1 Z% _+ N0 q, N# s. R" o
int cksum_buffer_length = Convert.ToInt32(dtemp); * S( X" M) z& Z% H
5 T& p% G% _7 _ //Create a Byte Array 2 M$ n( d6 V/ O; N. b" v * |4 l' S* h7 K0 d$ G+ v) J R UInt16 [] cksum_buffer = new UInt16[cksum_buffer_length]; ) `% s' p. G4 _+ H" w% U* Z 5 m/ t3 Z' l7 @4 r5 f* T
//Code to initialize the Uint16 array 1 D( @4 i0 E: e
4 u. w9 X, u" M ?1 [1 N int icmp_header_buffer_index = 0; % @- v3 ? _. W) g+ p) j7 M% t& L, o
3 a3 b' q8 e/ a for( int i = 0; i < cksum_buffer_length; i++ ) { % S4 S1 z" r# B3 t# s% m $ ?' r1 l/ y% D/ N
cksum_buffer = : [6 o; Q# ?9 Q0 D0 E7 D8 n