# Q3 D9 x# C% D4 ?4 v/ c; {/ r { / t8 u# S3 A6 z, ^* e
) o2 N( Z7 S5 K' \. d. L if(argv.Length==0) ( b0 Q' Z* J$ t) o' U( V3 Q4 ] N2 I ) T2 C* \( [& j5 g8 H { & F. m9 P3 }2 m+ `2 `! }/ \
- ~/ Q# a6 }4 z* @, H! m //If user did not enter any Parameter inform him ) _4 }) @7 b$ N; B* B) ` . q$ z2 o# e7 E5 J/ j
Console.WriteLine("Usageing <hostname> /r") ; - [- {1 e& i# [) n1 x ( P& s: G# s g1 N# q Console.WriteLine("<hostname> The name of the Host who you want to ping"); % Q+ y* I: _' l, ^) x2 ~ / Q+ C/ q. Q' ~* T: Z% e Console.WriteLine("/r Ping the host continuously") ; / x& i M0 G- U2 S- d' I. ^
/ P9 @; U0 s* ? l# Q Z/ n
} 6 T8 w d+ L! s, W" P7 b
/ `7 j" |7 n5 m3 H" Z else if(argv.Length==1) 6 a! I7 L4 r/ a. i, r5 s1 R: X / F( |4 G7 _* i: \* ^6 J
{ . S e a6 z0 H* c 5 I' }$ x$ }6 _$ I z+ }/ d: E6 ]0 o //Just the hostname provided by the user + l# e" }6 z# f1 n( Y2 m
! p) E& [8 l, ~- {3 B4 ^
//call the method "ingHost" and pass the HostName as a parameter 8 m; H. ^% k3 X7 E8 B; A% r# t( ? ( A# \8 y0 b* @ PingHost(argv[0]) ; " b, u2 V/ n5 x+ ~, f 1 {: d% l) L4 J; Q& c& {
} $ D$ @7 D" d% M; z3 W " h& F) N9 W/ b/ E. i
else if(argv.Length==2) 1 \9 m9 p) N7 e7 F+ j6 u2 w
4 O* t5 n9 C' J) w& N8 M
{ ; `( V% X6 O. L& X) v1 k
9 t G+ ?% o; Q+ ~ G+ { //the user provided the hostname and the switch 9 R, K8 x5 `1 S7 t- V7 Q
8 c- T1 {# e* _- e
if(argv[1]=="/r") 1 z7 B N1 R: b+ Z# o& c
5 ~: D& U) A X% Y g- Q
{ * u! X) V1 |1 a1 Z: s# ] % Q" X' U: [' O6 ]* h; V6 W! x //loop the ping program 1 D/ T1 l \9 n3 `
& [7 w$ T) D/ _" S& X$ b, t while(true) 3 \ u! m. `, a" P 2 i; `1 D* ?& M F { * z0 h y+ R+ V& @$ K
8 o& e) {% G# R, O# { //call the method "ingHost" and pass the HostName as a parameter J( A9 H0 v( s. V2 z0 b# p; |, H ' O# v" w+ a( y# Y
PingHost(argv[0]) ; 6 I. \+ N" M6 Y! Y2 `( y2 V8 H9 H
1 D2 n6 f( ^$ G7 n$ `3 w
} 8 J' a W0 q( ~
; k; n; f! ]( g3 w
} 5 Q! V% {$ c7 h8 j7 S
& ~6 S I! Y/ J- n else * f; Z% g) z2 h. r6 T* H
, t% [6 i, W- o. B { : j7 c S. q: F I
8 D4 p) u) }0 J: [3 c2 [+ I
//if the user provided some other switch % H) r6 d" v+ v: s2 t) ?
0 `; R; r- v; S* e3 {
PingHost(argv[0]) ; $ s f9 W5 \7 G# L ; d) f9 z2 K5 J) N
} 8 J* }0 r7 u, ]1 P8 A
$ L _3 T3 @' n# W0 ~7 o, i# b } ( |0 r$ u, x& u: Y+ R( Q- U + \5 y0 i. J5 X$ @ else - Q: Y1 s* u" k8 c" i" i( G
, h0 K3 B s( \4 o8 s
{ 0 I# r$ u, G5 x5 F) R
8 \$ F. b2 a1 B! k* c! I+ q8 k7 s& f
//Some error occurred ) }; F" `8 j; m5 O % l2 @: E+ X. g6 R9 K, C$ `: ^" s9 y" {
Console.WriteLine("Error in Arguments") ; ' i& D. U+ V. J
- G9 k% Y/ C, V- x } 9 W9 F, o+ M% M/ X" w* a& a 8 D) }' O5 h' o6 _
} 3 b: o2 D/ J1 w* F6 L& g( M; h ! U' P+ j$ n8 e* g3 L 4 ~$ K* H* W- H) q) H: r
/// <summary> $ Q4 E: Z2 u% ?( p + N" f' ~, x+ ~# G- V /// 主要的方法,用来取得IP, - @# I C' T) q9 w ; D& ]4 X$ f2 P% y0 s /// 并计算响应时间 * t7 e5 G3 F( S6 u& ~1 \7 W' y
; n t4 S+ d/ h+ F- ^5 a$ j6 h5 c+ [; m
/// </summary> % t$ F1 I4 W" A
4 v$ Q S1 H) k4 r: _ public static void PingHost(string host) 6 e3 ~ ^+ Y" s- C
+ @9 S# `9 v4 G9 Q: S0 k8 a { * d- o' `9 I( k: Q* K* Y$ ] + m# |! T# l! w# `
//Declare the IPHostEntry # r1 \( \5 D" R* c% B, {5 ^3 a
8 a& }+ c4 l6 w9 J* }% P5 O
IPHostEntry serverHE, fromHE; ( U- \$ D( U% F5 b9 I8 S& g+ f2 _ . ~2 X, y4 ^4 i, w: k! P$ E/ N
int nBytes = 0; $ B H% h* _" U6 E, i, Y" A7 q
9 w9 F# a; ~( p) x( O( J2 h int dwStart = 0, dwStop = 0; 6 q0 l2 Z; U6 U3 y ; D# W1 r4 `) e
//Initilize a Socket of the Type ICMP ; |" @1 x8 Z, R& Q2 l# O1 n
& |' x) z2 m9 h8 X Socket socket = ) F1 y+ W, m9 b- ?5 A( i0 d | ( i5 T- N. L5 H0 B& T. J new Socket(AddressFamily.AfINet, SocketType.SockRaw, ProtocolType.ProtICMP); : G. H3 s! |0 L; h
5 P; V/ ^2 N( G8 s / n9 i/ X6 t6 n# f3 y
// Get the server endpoint ! {( H% ]0 ]& N
' G4 C8 _: t% H5 P4 o
try z9 p4 {* Q2 H* W. _9 k7 k E5 ~$ K
5 s) {+ u3 {% O+ ]9 C' N9 F { . u3 f1 p, f* p2 j7 R: R( n# X 8 h7 F8 E% h# Y8 r }2 G6 h serverHE = DNS.GetHostByName(host); 8 m7 n' S+ v1 ^0 a; e6 B 5 \' O! a. p {* k7 m } + M' \6 e1 C& Z/ y, [+ B/ y& j 9 `( x. m: K* N% F l
catch(Exception) ( L. \1 c) i" n+ M7 |
& A- Y6 E$ i& M
{ 4 m* }+ n J+ r6 K& N7 e5 n/ h
- u( }3 W9 O' A8 L3 v
Console.WriteLine("Host not found"); // fail ! q7 {4 K* ]; E B4 T$ ]
; r8 f# \& M- `; I' i* v) _ return ; - j% |* a) R" \5 L3 ~# L$ ^/ h: T
$ `! S" t, b& |% b
} $ x1 S* w$ y0 }3 _4 m; C) D 5 j& F2 J% r1 W7 V
Z" |# v' C- E1 \' G // Convert the server IP_EndPoint to an EndPoint ; E8 \- N7 l# J( v - V' f- c# B' Z/ J- B IPEndPoint ipepServer = new IPEndPoint(serverHE.AddressList[0], 0); & C8 T& D8 z' Y. o* [. G - h, j$ \. m5 e, [/ q& [ EndPoint epServer = (ipepServer); ; O- \9 X7 e; ]0 h8 Z4 f / d- ^0 W i) H6 v( I& Y2 } ' o* k8 g8 \/ v // Set the receiving endpoint to the client machine 6 t* Y. J" A3 t+ n1 d& S
3 j* u6 Q; }4 m- Z% G. X$ i
fromHE = DNS.GetHostByName(DNS.GetHostName()); 3 K1 N# M. y- q% Z7 t
7 X/ `" c2 k! x0 s q# n IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList[0], 0); 0 m2 }4 } I) K, V4 T: y8 E/ V9 x 8 [: `6 u$ H) r9 V! l" P" C, H
EndPoint EndPointFrom = (ipEndPointFrom); ! t& e2 E+ W/ m" E 3 z: w: E M8 j Y
2 F0 _9 M* z3 w/ D int PacketSize = 0; 7 G5 {5 h9 w6 h
. c7 n, k* i. y0 \7 x IcmpPacket packet = new IcmpPacket(); 6 ?0 p9 T3 N( o$ S; K
$ b; N0 V G* Z0 F
// Construct the packet to send 9 G: F& Y5 L9 y$ F, e+ }* e 7 S, {- w' l( I; u& u
packet.Type = ICMP_ECHO; //8 * K0 o2 P+ Z3 z9 P) f$ R7 O # x0 e' m' n4 ?+ q" h! E packet.SubCode = 0; , c7 I" Y8 P- w8 Q0 d4 j