Need help with my very first c# application
-
I would like to build a basic ping application. I've seen a few ping applications here that are well developed and a little complicated for me to dissect and figure out how they work at this point in my learning. Does anyone have a console ping application that is bare bones, that I can build off of while I learn? Something that I can launch and hard code variables into, and break and the put back and see how it functions? Thank you!
I entered "C# basic ping application" in Bing (created by M$) link to MSDN (also M$)Console and all. Good luck! I wish I had access to the internet when I learned to program. http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx[^]
It was broke, so I fixed it.
-
I entered "C# basic ping application" in Bing (created by M$) link to MSDN (also M$)Console and all. Good luck! I wish I had access to the internet when I learned to program. http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx[^]
It was broke, so I fixed it.
Hi, I tried that and it fails, I then spent a few hours trying to figure out why and could not, so I came onto here for help looking for something as basic as a few lines, that maybe I would be able to understand. Visual C# Express says out of range exception was unhandled for this line? PingReply reply = pingSender.Send(args[0], timeout, buffer, options);
-
Hi, I tried that and it fails, I then spent a few hours trying to figure out why and could not, so I came onto here for help looking for something as basic as a few lines, that maybe I would be able to understand. Visual C# Express says out of range exception was unhandled for this line? PingReply reply = pingSender.Send(args[0], timeout, buffer, options);
if you want help with some code, you'd better show some of it, I mean more than a single line. if you look in MSDN, there is a full example on the Ping class; it is in fact a Console app, probably doing exactly what you want. if you look at Ping.Send in MSDN, it explains all, and shows possible exceptions; it also gives a possible source for OutOfRangeException. Now go and read! :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Hi, I tried that and it fails, I then spent a few hours trying to figure out why and could not, so I came onto here for help looking for something as basic as a few lines, that maybe I would be able to understand. Visual C# Express says out of range exception was unhandled for this line? PingReply reply = pingSender.Send(args[0], timeout, buffer, options);
Ok I see what I did wrong initially. I have now defined a variable called host and then put it in place of the "args[0]" array and it will ping it one time and return some values. I think I can build off of this, thank you very much!
-
I would like to build a basic ping application. I've seen a few ping applications here that are well developed and a little complicated for me to dissect and figure out how they work at this point in my learning. Does anyone have a console ping application that is bare bones, that I can build off of while I learn? Something that I can launch and hard code variables into, and break and the put back and see how it functions? Thank you!
A ping app is probably not the best choice for your first C# application. You may want to get yourself familiar with the language syntax and semantics first. Although, if you have prior C++ or Java experience, this approach should still work fine for you.
Regards, Nish
My technology blog: voidnish.wordpress.com Code Project Forums : New Posts Monitor This application monitors for new posts in the Code Project forums.
-
Ok I see what I did wrong initially. I have now defined a variable called host and then put it in place of the "args[0]" array and it will ping it one time and return some values. I think I can build off of this, thank you very much!
Yes but have you worked out why
args[0]
caused a problem andhost
didn't? What isargs[0]
? What does it hold? Is it important? Do you limit or cripple your software if you remove it? Or is removal an improvement? You do need to understand this basic stuff before you dive into anything more complex for a first app. Honest.Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Yes but have you worked out why
args[0]
caused a problem andhost
didn't? What isargs[0]
? What does it hold? Is it important? Do you limit or cripple your software if you remove it? Or is removal an improvement? You do need to understand this basic stuff before you dive into anything more complex for a first app. Honest.Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Hi, I believe it is because the array is empty or undefined. I doubt it is an improvement to remove it. :) I'm not sure how to get it to work with the array, the only time I've used an array is with a for statement and that isn't really applicable for holding multiple host names. I believe you can pass multiple values into the method with { } but I'm not sure on the syntax yet
-
Hi, I believe it is because the array is empty or undefined. I doubt it is an improvement to remove it. :) I'm not sure how to get it to work with the array, the only time I've used an array is with a for statement and that isn't really applicable for holding multiple host names. I believe you can pass multiple values into the method with { } but I'm not sure on the syntax yet
Hi, I would suggest you go buy and study an introductory book on C#. Hands-on experience is nice, getting the fundamentals is important however. Nothing beats a book in teaching you all that is required in an orderly structured way. Visit a real bookstore, or Amazon if you must. Look at some books, pick the one you like most. Which book one prefers is subjective. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Ok I see what I did wrong initially. I have now defined a variable called host and then put it in place of the "args[0]" array and it will ping it one time and return some values. I think I can build off of this, thank you very much!
More internet reading for you. http://dotnetperls.com/main[^] OriginalGriff is right on the money, understand what you are doing, and Luc Pattyn said the correct way to do just that, read. When you understand the basic underlying constructs and how data is used and passed around within the computer, it gets easier. (That's what they told me 27 years ago, but I'm still waiting for it to get easier... :laugh: )
It was broke, so I fixed it.
-
Hi, I believe it is because the array is empty or undefined. I doubt it is an improvement to remove it. :) I'm not sure how to get it to work with the array, the only time I've used an array is with a for statement and that isn't really applicable for holding multiple host names. I believe you can pass multiple values into the method with { } but I'm not sure on the syntax yet
So your first port of call is to find out just what
args[0]
is. Google can help: Google args[0] c#[^] This second result explains it, and gives examples: MSDN on command line arguments[^] This is your second (or first-and-a-half) port of call: MSDN. It knows everything (hah!) about C# and .NET And it's free. Impenetrable at times, but free. And very often worth tagging into a Google search: "args[0] c# MSDN" would give the article above as the first hit. So,args[0]
is the first command line argument to your console app. (or Winforms or whatever, but don't worry about that yet). This explains why it gave an ArgumentOutOfRange error when you tried to use it: you haven't supplied any command line arguments! So, change your code to:string host = "192.168.0.1"; // Default to my router
if (args.Count > 0)
{
host = args[0];
}And it would work with either a default value - my router - or the IP address you specify as a command line argument. This means you can run your console app with:
myPing
(which would ping your router) or
myPing 92.27.41.80
(which would ping me, for an hour or so until I turn the router off.) Now do you see what I mean about it being important to work out why something isn't working, rather than just "fixing it"? :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Hi, I would suggest you go buy and study an introductory book on C#. Hands-on experience is nice, getting the fundamentals is important however. Nothing beats a book in teaching you all that is required in an orderly structured way. Visit a real bookstore, or Amazon if you must. Look at some books, pick the one you like most. Which book one prefers is subjective. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Luc Pattyn wrote:
Which book one prefers is subjective.
...just avoid anything with "...In 7 Days!" in the title! :-D
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Hi, I would suggest you go buy and study an introductory book on C#. Hands-on experience is nice, getting the fundamentals is important however. Nothing beats a book in teaching you all that is required in an orderly structured way. Visit a real bookstore, or Amazon if you must. Look at some books, pick the one you like most. Which book one prefers is subjective. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Hi Luc, I'm watching/doing the video series on www.learnvisualstudio.net Have you watched these? I learn better seeing while reading ... but maybe the book is worth it also? Let me know what you think, thanks.
-
Hi, I would suggest you go buy and study an introductory book on C#. Hands-on experience is nice, getting the fundamentals is important however. Nothing beats a book in teaching you all that is required in an orderly structured way. Visit a real bookstore, or Amazon if you must. Look at some books, pick the one you like most. Which book one prefers is subjective. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
If the book store is too much bother, another link to M$, at least it doesn't contain the words ...in 7 days. :-D though you could probably cover it in less. http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx[^]
It was broke, so I fixed it.
-
Hi Luc, I'm watching/doing the video series on www.learnvisualstudio.net Have you watched these? I learn better seeing while reading ... but maybe the book is worth it also? Let me know what you think, thanks.
I think nothing beats a book, for a couple of reasons: 1. The quality tends to be much higher than anything on the web. 2. You can process material on your own pace, anything that moves may end up being watched as a TV show. 3. You can make little notes and apply highlights in a book (assuming you own it). I do a quick video if I need to get some insight in new stuff, not to study it thoroughly. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
If the book store is too much bother, another link to M$, at least it doesn't contain the words ...in 7 days. :-D though you could probably cover it in less. http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx[^]
It was broke, so I fixed it.
That is not an introduction or a tutorial, that is reference material. Which comes in handy once you start programming in some language, but first you need the intro, the fundamentals. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
That is not an introduction or a tutorial, that is reference material. Which comes in handy once you start programming in some language, but first you need the intro, the fundamentals. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
:-O Sorry, wrong link, had several pages open. See, it doesn't get easier. http://msdn.microsoft.com/en-us/library/aa288436(VS.71).aspx[^]
It was broke, so I fixed it.
-
So your first port of call is to find out just what
args[0]
is. Google can help: Google args[0] c#[^] This second result explains it, and gives examples: MSDN on command line arguments[^] This is your second (or first-and-a-half) port of call: MSDN. It knows everything (hah!) about C# and .NET And it's free. Impenetrable at times, but free. And very often worth tagging into a Google search: "args[0] c# MSDN" would give the article above as the first hit. So,args[0]
is the first command line argument to your console app. (or Winforms or whatever, but don't worry about that yet). This explains why it gave an ArgumentOutOfRange error when you tried to use it: you haven't supplied any command line arguments! So, change your code to:string host = "192.168.0.1"; // Default to my router
if (args.Count > 0)
{
host = args[0];
}And it would work with either a default value - my router - or the IP address you specify as a command line argument. This means you can run your console app with:
myPing
(which would ping your router) or
myPing 92.27.41.80
(which would ping me, for an hour or so until I turn the router off.) Now do you see what I mean about it being important to work out why something isn't working, rather than just "fixing it"? :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Thank you. Even though I have some reading to do now :) I will try and implement this and get it working.
-
So your first port of call is to find out just what
args[0]
is. Google can help: Google args[0] c#[^] This second result explains it, and gives examples: MSDN on command line arguments[^] This is your second (or first-and-a-half) port of call: MSDN. It knows everything (hah!) about C# and .NET And it's free. Impenetrable at times, but free. And very often worth tagging into a Google search: "args[0] c# MSDN" would give the article above as the first hit. So,args[0]
is the first command line argument to your console app. (or Winforms or whatever, but don't worry about that yet). This explains why it gave an ArgumentOutOfRange error when you tried to use it: you haven't supplied any command line arguments! So, change your code to:string host = "192.168.0.1"; // Default to my router
if (args.Count > 0)
{
host = args[0];
}And it would work with either a default value - my router - or the IP address you specify as a command line argument. This means you can run your console app with:
myPing
(which would ping your router) or
myPing 92.27.41.80
(which would ping me, for an hour or so until I turn the router off.) Now do you see what I mean about it being important to work out why something isn't working, rather than just "fixing it"? :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Here is what I have, did I do something wrong because it is still telling me I am outside of the bounds of the array
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
// args[0] can be an IPaddress or host name.public static void Main(string \[\] args) { Ping pingSender = new Ping(); PingOptions options = new PingOptions(); //string host = "servername"; string host = "servername"; if (args.Length > 0) { host = args\[0\]; } // Use the default Ttl value which is 128, // but change the fragmentation behavior. options.DontFragment = true; // Create a buffer of 32 bytes of data to be transmitted. string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; byte\[\] buffer = Encoding.ASCII.GetBytes(data); int timeout = 120; PingReply reply = pingSender.Send(args\[0\], timeout, buffer, options); if (reply.Status == IPStatus.Success) { Console.WriteLine("Reply from {0}", reply.Address.ToString()); Console.WriteLine("Bytes {0}", reply.Buffer.Length); Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime); Console.WriteLine("Time to live: {0}", reply.Options.Ttl); //Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment); Console.ReadLine(); } } }
}
-
I think nothing beats a book, for a couple of reasons: 1. The quality tends to be much higher than anything on the web. 2. You can process material on your own pace, anything that moves may end up being watched as a TV show. 3. You can make little notes and apply highlights in a book (assuming you own it). I do a quick video if I need to get some insight in new stuff, not to study it thoroughly. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Books are very good. Considering everything I know about C# was gained through library books and books bought at used book stores (no formal training) they are a priceless source of information. As a side note,
args[0]
sounds like the argument passed to all Console applications in the Main method. Soargs[0]
would be the first command line argument for the application e.g.ping.exe 255.255.255.255
. This being a hypothetical situation from the fact that replacing it with a variable called host (assuming it is the IP Adress to ping) made it work. Anyway. Books are good. -
Here is what I have, did I do something wrong because it is still telling me I am outside of the bounds of the array
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
// args[0] can be an IPaddress or host name.public static void Main(string \[\] args) { Ping pingSender = new Ping(); PingOptions options = new PingOptions(); //string host = "servername"; string host = "servername"; if (args.Length > 0) { host = args\[0\]; } // Use the default Ttl value which is 128, // but change the fragmentation behavior. options.DontFragment = true; // Create a buffer of 32 bytes of data to be transmitted. string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; byte\[\] buffer = Encoding.ASCII.GetBytes(data); int timeout = 120; PingReply reply = pingSender.Send(args\[0\], timeout, buffer, options); if (reply.Status == IPStatus.Success) { Console.WriteLine("Reply from {0}", reply.Address.ToString()); Console.WriteLine("Bytes {0}", reply.Buffer.Length); Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime); Console.WriteLine("Time to live: {0}", reply.Options.Ttl); //Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment); Console.ReadLine(); } } }
}
Yes! You are still using
args[0]
PingReply reply = pingSender.Send(args\[0\], timeout, buffer, options);
Try:
PingReply reply = pingSender.Send(host, timeout, buffer, options);
Also change the comment near the top:
// args\[0\] can be an IPaddress or host name.
It may be accurate, but it isn't very helpfull! How about:
// Command line arguments can be an IPaddress or host name to ping.
That describes better what you are trying to do, rather than how you are achieving them. However, you get a 5 just for putting comments in your first program. :laugh: Not a bad effort - keep it up! Later, you may want to put in a loop, so you can use a number of addresses in the command line. See
foreach
for details.Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.