Click here to Skip to main content
15,890,690 members
Home / Discussions / C#
   

C#

 
AnswerRe: Ping in .NET 1.1 Pin
PIEBALDconsult5-Jan-07 4:03
mvePIEBALDconsult5-Jan-07 4:03 
AnswerRe: Ping in .NET 1.1 Pin
Dan Neely5-Jan-07 4:07
Dan Neely5-Jan-07 4:07 
GeneralRe: Ping in .NET 1.1 Pin
MartyExodus5-Jan-07 4:41
MartyExodus5-Jan-07 4:41 
GeneralRe: Ping in .NET 1.1 Pin
led mike5-Jan-07 5:19
led mike5-Jan-07 5:19 
GeneralRe: Ping in .NET 1.1 Pin
MartyExodus5-Jan-07 7:20
MartyExodus5-Jan-07 7:20 
GeneralRe: Ping in .NET 1.1 Pin
led mike5-Jan-07 9:31
led mike5-Jan-07 9:31 
GeneralRe: Ping in .NET 1.1 Pin
PIEBALDconsult5-Jan-07 7:33
mvePIEBALDconsult5-Jan-07 7:33 
AnswerRe: Ping in .NET 1.1 Pin
Luc Pattyn5-Jan-07 6:12
sitebuilderLuc Pattyn5-Jan-07 6:12 
Well I once did my own ping in .NET 1.1
Her is the code; it will only compile and run within my environment,
but I'll show it anyway.

env.log() is just a logging function, and can be ignored (or used!)
class LP_Format does string formatting, is obvious
class LP_Thread basically is Thread, LP_BackgroundThread is derived from Thread
(sets IsBackground and calls Start).

remark: this is not thread-safe (since I use a static pingResult internally),
but that could easily be solved.
<pre>

public static bool Ping(string remoteIPadrString) {
pingResult=false;
LP_BackgroundThread thread=new LP_BackgroundThread("PING("+remoteIPadrString+")",
new LP_ObjectHandler(Pinger), remoteIPadrString);
if (!thread.Join(10000)) {
env.log(0, "Aborting PING("+remoteIPadrString+") due to timeout");
//thread.Abort();
LP_Thread.Sleep(1000);
}
return pingResult;
}

protected static bool pingResult;

protected static void Pinger(object arg) {
string remoteIPadrString=arg as string;
env.log(0, "---------");
env.log(0, "Ping("+remoteIPadrString+")");
IPAddress remoteIPadr=IPAddress.Parse(remoteIPadrString);
pingResult=false;
Socket socket=null;
try {
env.log(env.DETAIL2, "remoteIPadr="+remoteIPadr.ToString());
IPAddress[] IPAS=getIPAddresses();
env.log(env.DETAIL2, "got IP addresses");
socket=null;
foreach (IPAddress IPA in IPAS) {
try {
env.log(env.DETAIL2, "Ping("+remoteIPadrString+") using "+IPA.ToString());
socket=new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
socket.Blocking=false;
env.log(env.DETAIL1, "got socket");
EndPoint localEP=new IPEndPoint(IPA, IPEndPoint.MinPort);
EndPoint remoteEP=new IPEndPoint(remoteIPadr, IPEndPoint.MinPort);
socket.Bind(localEP);
ushort[] sdata=new ushort[20];
sdata[0]=0x0800; // ICMP echo request
sdata[1]=0; // ICMP checksum
sdata[2]=0x0100; // ICMP identifier
sdata[3]=0x0400; // ICMP sequence number
ushort val=0x6162;
for (int j=0; j<16; j++) {
sdata[j+4]=val;
val+=0x0202;
if (val==0x7963) val=0x6263;
if (val==0x7778) val=0x7761;
}
sdata[1]=ICMPchecksum(sdata);
env.log(env.DETAIL2,"checksum="+LP_Format.Hex4(sdata[1]));
byte[] bdata=new byte[40];
for (int j=0; j<20; j++) {
bdata[2*j]=(byte)((sdata[j]>>8)&0xFF);
bdata[2*j+1]=(byte)(sdata[j]&0xFF);
}
bool dump=false;
int len=socket.SendTo(bdata, remoteEP);
env.log(env.DETAIL1, "sent");
if (dump) {
env.log(env.DETAIL2,"sent len="+len);
for (int j=0; j<len; j+=4) {
env.log(env.DETAIL2, LP_Format.Hex2(bdata[j])+" "+
LP_Format.Hex2(bdata[j+1])+" "+
LP_Format.Hex2(bdata[j+2])+" "+LP_Format.Hex2(bdata[j+3]));
}
}
LP_Thread.Sleep(50);
int len2=0;
byte[] bdata2=new byte[300];
len2=socket.ReceiveFrom(bdata2, SocketFlags.Peek, ref remoteEP);
env.log(env.DETAIL1, "received");
if (dump) {
env.log(env.DETAIL2,"received len="+len2);
for (int j=0; j<len2; j+=4) {
env.log(env.DETAIL2, LP_Format.Hex2(bdata2[j])+" "+
LP_Format.Hex2(bdata2[j+1])+" "+
LP_Format.Hex2(bdata2[j+2])+" "+LP_Format.Hex2(bdata2[j+3]));
}
} else {
env.log(env.DETAIL2,"len="+len+" len2="+len2);
}
pingResult=true;
env.log(0, "Ping("+remoteIPadrString+") using "+IPA.ToString()+" is OK");
} catch(Exception) {}
try {if (socket!=null) socket.Shutdown(SocketShutdown.Both);} catch(Exception) {}
try {if (socket!=null) socket.Close();} catch(Exception) {}
socket=null;
}
} catch(Exception e) {
env.error(e);
}
try {if (socket!=null) socket.Shutdown(SocketShutdown.Both);} catch(Exception) {}
try {if (socket!=null) socket.Close();} catch(Exception) {}
env.log(env.DETAIL1,"ping("+remoteIPadrString+")="+pingResult);
}

</pre>

Cheers




Luc Pattyn
AnswerRe: Ping in .NET 1.1 Pin
Nader Elshehabi5-Jan-07 11:02
Nader Elshehabi5-Jan-07 11:02 
JokeNEED HELP - Urgent Pin
ednrgc5-Jan-07 3:38
ednrgc5-Jan-07 3:38 
GeneralRe: NEED HELP - Urgent Pin
Not Active5-Jan-07 3:52
mentorNot Active5-Jan-07 3:52 
GeneralRe: NEED HELP - Urgent Pin
lost in transition 5-Jan-07 4:51
lost in transition 5-Jan-07 4:51 
GeneralRe: NEED HELP - Urgent Pin
J4amieC5-Jan-07 5:00
J4amieC5-Jan-07 5:00 
GeneralRe: NEED HELP - Urgent Pin
ednrgc5-Jan-07 5:02
ednrgc5-Jan-07 5:02 
GeneralRe: NEED HELP - Urgent Pin
led mike5-Jan-07 5:14
led mike5-Jan-07 5:14 
Questiontree view & XML? Pin
sajid.salim.khan5-Jan-07 3:36
sajid.salim.khan5-Jan-07 3:36 
AnswerRe: tree view & XML? Pin
Not Active5-Jan-07 3:46
mentorNot Active5-Jan-07 3:46 
GeneralRe: tree view & XML? Pin
Pete O'Hanlon5-Jan-07 4:15
mvePete O'Hanlon5-Jan-07 4:15 
GeneralBut Sir Pin
sajid.salim.khan5-Jan-07 7:09
sajid.salim.khan5-Jan-07 7:09 
QuestionSet width of text part of combo Pin
Russell Jones5-Jan-07 2:29
Russell Jones5-Jan-07 2:29 
AnswerRe: Set width of text part of combo Pin
Russell Jones5-Jan-07 3:25
Russell Jones5-Jan-07 3:25 
AnswerRe: Set width of text part of combo Pin
Larantz5-Jan-07 13:57
Larantz5-Jan-07 13:57 
Generaldirectory of execution Pin
V.5-Jan-07 1:57
professionalV.5-Jan-07 1:57 
GeneralRe: directory of execution Pin
User 66585-Jan-07 2:14
User 66585-Jan-07 2:14 
GeneralRe: directory of execution Pin
V.5-Jan-07 2:21
professionalV.5-Jan-07 2:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.