Ping in .NET 1.1
-
I am writing a program in Visual Studio .NET 2003, and as such I am using version 1.1 of the .NET Framework. I need to include a function to ping a remote computer, and, while it is easy enough to pass it into the command prompt, I don't know of any way to read the result of the ping attempt. MSDN says that there is a Ping class in .NET 2.0 and 3.0 (System.Net.NetworkInformation.Ping - system.dll), but that doesn't really help me, as it's not in .NET 1.1 Is there any way I can import the functionality from .NET 2.0 (I have the Framwork and C# 2005 Express installed), or is there any other way to ping and receive a result? I would really appreciate some help with this, as I need to make this program for work.... and I need to have it done by Monday afternoon. Thanks again, MartyExodus
-
I am writing a program in Visual Studio .NET 2003, and as such I am using version 1.1 of the .NET Framework. I need to include a function to ping a remote computer, and, while it is easy enough to pass it into the command prompt, I don't know of any way to read the result of the ping attempt. MSDN says that there is a Ping class in .NET 2.0 and 3.0 (System.Net.NetworkInformation.Ping - system.dll), but that doesn't really help me, as it's not in .NET 1.1 Is there any way I can import the functionality from .NET 2.0 (I have the Framwork and C# 2005 Express installed), or is there any other way to ping and receive a result? I would really appreciate some help with this, as I need to make this program for work.... and I need to have it done by Monday afternoon. Thanks again, MartyExodus
You should have put "ping" in the search bar. You would have found this: http://www.codeproject.com/dotnet/CSharpPing.asp
-
I am writing a program in Visual Studio .NET 2003, and as such I am using version 1.1 of the .NET Framework. I need to include a function to ping a remote computer, and, while it is easy enough to pass it into the command prompt, I don't know of any way to read the result of the ping attempt. MSDN says that there is a Ping class in .NET 2.0 and 3.0 (System.Net.NetworkInformation.Ping - system.dll), but that doesn't really help me, as it's not in .NET 1.1 Is there any way I can import the functionality from .NET 2.0 (I have the Framwork and C# 2005 Express installed), or is there any other way to ping and receive a result? I would really appreciate some help with this, as I need to make this program for work.... and I need to have it done by Monday afternoon. Thanks again, MartyExodus
I suppose you could access a v2.0 assembly (dll) from a v1.1 executable. The method I have that wraps the ping is:
public static System.Net.NetworkInformation.IPStatus Ping ( string HostName ) { System.Net.NetworkInformation.IPStatus result = System.Net.NetworkInformation.IPStatus.Unknown ; try { result = (new System.Net.NetworkInformation.Ping()).Send ( HostName , 1000 ).Status ; } catch { } return ( result ) ; }
You don't even need the express version, just the SDK. -
I am writing a program in Visual Studio .NET 2003, and as such I am using version 1.1 of the .NET Framework. I need to include a function to ping a remote computer, and, while it is easy enough to pass it into the command prompt, I don't know of any way to read the result of the ping attempt. MSDN says that there is a Ping class in .NET 2.0 and 3.0 (System.Net.NetworkInformation.Ping - system.dll), but that doesn't really help me, as it's not in .NET 1.1 Is there any way I can import the functionality from .NET 2.0 (I have the Framwork and C# 2005 Express installed), or is there any other way to ping and receive a result? I would really appreciate some help with this, as I need to make this program for work.... and I need to have it done by Monday afternoon. Thanks again, MartyExodus
-
You can use the StandardOutput property of the Process class to capture the output of a commandline application.
-- Rules of thumb should not be taken for the whole hand.
Ha. Thank you everyone for your quick responses. However, I'm still having a slight problem, possibly due to that fact that i didn't articulate my original problem. ednrgc and PIEBALDconsult - The reason that i didn't do as the two of you said is simply this: System.Net.NetworkInformation doesn't exist in .NET 1.1, and as such, the Ping class doesn't exist either (Or I possibly don't know how to access them, but I'm pretty sure I'm right) dan neely - I believe this method will work, however, I'm rather uncertain as to HOW to use it. This is what I have so far:
using System;
using System.Diagnostics;
using System.IO;private void ping(string computer)
{
string args = "/k ping " + computer;
Process p = new System.Process()
ProcessStartInfo ps = new ProcessStartInfo("cmd", args);
p.StartInfo = ps;
p.Start();
StreamReader s = p.StandardOutput;
MessageBox.Show(s.Readline);
}The command prompt loads correctly, but alas, the StreamReader fails, and the MessageBox does not show. It generates an InvalidOperationException, and simply states "StandardOutput has not been redirected." Thanks everyone again for the quick response, and hopefully, you can help me further. Thanks, MartyExodus
-
Ha. Thank you everyone for your quick responses. However, I'm still having a slight problem, possibly due to that fact that i didn't articulate my original problem. ednrgc and PIEBALDconsult - The reason that i didn't do as the two of you said is simply this: System.Net.NetworkInformation doesn't exist in .NET 1.1, and as such, the Ping class doesn't exist either (Or I possibly don't know how to access them, but I'm pretty sure I'm right) dan neely - I believe this method will work, however, I'm rather uncertain as to HOW to use it. This is what I have so far:
using System;
using System.Diagnostics;
using System.IO;private void ping(string computer)
{
string args = "/k ping " + computer;
Process p = new System.Process()
ProcessStartInfo ps = new ProcessStartInfo("cmd", args);
p.StartInfo = ps;
p.Start();
StreamReader s = p.StandardOutput;
MessageBox.Show(s.Readline);
}The command prompt loads correctly, but alas, the StreamReader fails, and the MessageBox does not show. It generates an InvalidOperationException, and simply states "StandardOutput has not been redirected." Thanks everyone again for the quick response, and hopefully, you can help me further. Thanks, MartyExodus
If I had to spawn a child process to ping a remote computer I would never write code again. But that's just me I guess. http://en.wikipedia.org/wiki/Ping[^] http://www.eggheadcafe.com/articles/20020209.asp[^]
led mike
-
I am writing a program in Visual Studio .NET 2003, and as such I am using version 1.1 of the .NET Framework. I need to include a function to ping a remote computer, and, while it is easy enough to pass it into the command prompt, I don't know of any way to read the result of the ping attempt. MSDN says that there is a Ping class in .NET 2.0 and 3.0 (System.Net.NetworkInformation.Ping - system.dll), but that doesn't really help me, as it's not in .NET 1.1 Is there any way I can import the functionality from .NET 2.0 (I have the Framwork and C# 2005 Express installed), or is there any other way to ping and receive a result? I would really appreciate some help with this, as I need to make this program for work.... and I need to have it done by Monday afternoon. Thanks again, MartyExodus
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.
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 ( -
If I had to spawn a child process to ping a remote computer I would never write code again. But that's just me I guess. http://en.wikipedia.org/wiki/Ping[^] http://www.eggheadcafe.com/articles/20020209.asp[^]
led mike
Hey, thanks a lot. While I'm not using the exact code from the article you posted... (simply because I'd like to LEARN rather than simply USE) you've inspired me to learn how to use sockets - something I should have done a long time ago. But again, thanks to all who posted. -MartyExodus
-
Ha. Thank you everyone for your quick responses. However, I'm still having a slight problem, possibly due to that fact that i didn't articulate my original problem. ednrgc and PIEBALDconsult - The reason that i didn't do as the two of you said is simply this: System.Net.NetworkInformation doesn't exist in .NET 1.1, and as such, the Ping class doesn't exist either (Or I possibly don't know how to access them, but I'm pretty sure I'm right) dan neely - I believe this method will work, however, I'm rather uncertain as to HOW to use it. This is what I have so far:
using System;
using System.Diagnostics;
using System.IO;private void ping(string computer)
{
string args = "/k ping " + computer;
Process p = new System.Process()
ProcessStartInfo ps = new ProcessStartInfo("cmd", args);
p.StartInfo = ps;
p.Start();
StreamReader s = p.StandardOutput;
MessageBox.Show(s.Readline);
}The command prompt loads correctly, but alas, the StreamReader fails, and the MessageBox does not show. It generates an InvalidOperationException, and simply states "StandardOutput has not been redirected." Thanks everyone again for the quick response, and hopefully, you can help me further. Thanks, MartyExodus
MartyExodus wrote:
PIEBALDconsult - The reason that i didn't do as the two of you said is simply this: System.Net.NetworkInformation doesn't exist in .NET 1.1,
I understand that. But I meant compile the method into its own v2.0 dll and reference it from your v1.1 executable. You said you have the Express version so you must have the v2.0 dlls and compiler. I haven't tried it, nor do I have a way to try it.
-
Hey, thanks a lot. While I'm not using the exact code from the article you posted... (simply because I'd like to LEARN rather than simply USE) you've inspired me to learn how to use sockets - something I should have done a long time ago. But again, thanks to all who posted. -MartyExodus
-
I am writing a program in Visual Studio .NET 2003, and as such I am using version 1.1 of the .NET Framework. I need to include a function to ping a remote computer, and, while it is easy enough to pass it into the command prompt, I don't know of any way to read the result of the ping attempt. MSDN says that there is a Ping class in .NET 2.0 and 3.0 (System.Net.NetworkInformation.Ping - system.dll), but that doesn't really help me, as it's not in .NET 1.1 Is there any way I can import the functionality from .NET 2.0 (I have the Framwork and C# 2005 Express installed), or is there any other way to ping and receive a result? I would really appreciate some help with this, as I need to make this program for work.... and I need to have it done by Monday afternoon. Thanks again, MartyExodus
MartyExodus wrote:
Is there any way I can import the functionality from .NET 2.0
Sure! 1- First install DotNet 2.0 on your machine. 2- Then add a reference to your project. Choose browse. 3- Browse to the DotNet 2.0 folder -I believe it's under "windows\system32"-. and choose the assembly you want to reference. 4- In your case the assembly should be "
System.Net.NetworkInformation.dll
". PS. If you do this then DotNet 2.0 should also be installed on the user's machine.Regards:rose: