0

I'm working on a small project and I'm having an issue. I have it set up so a user can paste a series of IP Addresses into a multiline textbox and ping each IP. I'm currently taking each value that's entered into the input box and adding it to a string array. The problem I'm having is converting each value in that array to an IP using the IPAddress.Parse method. Any tips would be greatly appriciated. It's in c#

using System;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;


namespace MultiPing
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    private void pingBtn_Click(object sender, EventArgs e)
    {
        try
        {
            int i;
            string[] allLines = inputBox.Text.Split('\n');
            Ping pingSender = new Ping();
            for (i = 0; i < allLines.Length; i++)
            {
                try
                {
                    IPAddress address = IPAddress.Parse(allLines[]);
                    PingReply reply = pingSender.Send(address);

                    if (reply.Status == IPStatus.Success)
                    {
                        outputBox.Text = address + " is up \n";
                    }
                    else
                    {
                        outputBox.Text = address + " is down \n";
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }


    }
}
}
4
  • 1
    IPAddress address = IPAddress.Parse(allLines[]); <-- supposed to be allLines[i]? Commented Jan 14, 2015 at 23:00
  • I tried that but it was was giving an error. I just tried it again and it randomly started working....I feel kind of dumb now. Commented Jan 14, 2015 at 23:03
  • 2
    Hint: It wasnt random. Probably if you errored, it was an off by 1 error in your for loop. Cant see why you wouldnt use a foreach here. Commented Jan 14, 2015 at 23:04
  • I tried a foreach to see if it made a difference. I'm still just learning so I do tend to make a lot of mistakes. Ultimately I kept the for loop. Commented Jan 14, 2015 at 23:06

1 Answer 1

2

Change this

IPAddress address = IPAddress.Parse(allLines[]);

to

IPAddress address = IPAddress.Parse(allLines[i]);
Sign up to request clarification or add additional context in comments.

4 Comments

That did it. It was giving an error a little bit ago. But now it's working like a charm. Thanks.
@Josh probably if you feed it a invalid IP
I triple checked the IP and printed the parsed result to the console to make sure it was ending up right. This issue is resolved but now I'm working on figuring out why it's not actually appending a line for each IP it pings
You overwrite the output box text instead of append it. Try change to this --> outputBox.Text += address + " is up \n";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.