[Q] How to define an array of IPAddress-es

  • Thread starter Thread starter Stuart Norris
  • Start date Start date
S

Stuart Norris

Dear Readers,

I am attempting to define an array of IPAddress-es in C#.

I wish to have a array of address so I can try in order to connect to
them in a loop to handle unavailable hosts.

Todate since I do not know how to define an array of IPAddress-es. I
have been defining variable like:

IPAddress ipAddress1 = IPAddress.Parse( "10.1.1.1" );
IPAddress ipAddress2 = IPAddress.Parse( "11.1.1.1" );

Then define the end point using the hard coded name.

IPEndPoint remoteServerEndPoint = new IPEndPoint( ipAddress1, port );


How do I declare an array of IPAddress-es so I can loop around them
using an index counter?

I thought I could do IPAddress[] ipAddress = new IPAddress[2];

and then use ipAddress[0] = IPAddress.Parse( "10.1.1.1" );

This did not work.

What is the correct way to define an array of IPAdresses-es?

Thanks

Stuart
 
Does this create a new IPAddress object? (I've never used this object
before...)
IPAddress ipAddress1 = IPAddress.Parse( "10.1.1.1" );

If so, then I don't see why this would have failed:
ipAddress[0] = IPAddress.Parse( "10.1.1.1" );

Regardless, you can just as easily create an ArrayList.

ArrayList alist = new ArrayList();
alist.Add(IPAddress.Parse("10.2.3.4"));

foreach (IPAddress myIP in alist)
{
// do something with myIP
}

In fact, you could just as easily store an array of IPEndpoint objects in
the ArrayList.

I haven't used any of these objects before, and am not aware of the
sideeffects of using them, or resources that may be allocated. My advice
may be off-base if these are expensive critters.

Good luck,
--- Nick

Stuart Norris said:
Dear Readers,

I am attempting to define an array of IPAddress-es in C#.

I wish to have a array of address so I can try in order to connect to
them in a loop to handle unavailable hosts.

Todate since I do not know how to define an array of IPAddress-es. I
have been defining variable like:

IPAddress ipAddress1 = IPAddress.Parse( "10.1.1.1" );
IPAddress ipAddress2 = IPAddress.Parse( "11.1.1.1" );

Then define the end point using the hard coded name.

IPEndPoint remoteServerEndPoint = new IPEndPoint( ipAddress1, port );


How do I declare an array of IPAddress-es so I can loop around them
using an index counter?

I thought I could do IPAddress[] ipAddress = new IPAddress[2];

and then use ipAddress[0] = IPAddress.Parse( "10.1.1.1" );

This did not work.

What is the correct way to define an array of IPAdresses-es?

Thanks

Stuart
 
How do I declare an array of IPAddress-es so I can loop around them
using an index counter?

I thought I could do IPAddress[] ipAddress = new IPAddress[2];
and then use ipAddress[0] = IPAddress.Parse( "10.1.1.1" );

You can.
This did not work.

It should have done. Could you post a short but complete program which
demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
private void button48_Click(object sender, System.EventArgs e)
{
IPAddress[] ips = new IPAddress[5];
string tempIP = "192.168.0.";
for(int i = 0; i < 5; i++)
{
ips = IPAddress.Parse(tempIP + (i + 1));
}
foreach(IPAddress ip in ips)
{
Console.WriteLine("IP:"+ip.ToString());
}
}
 
For perf it also takes a long...

ips = new IPAddress(byte1 << 24 + byte2 << 16 + byte3 << 8 + byte4);

In a loop, you just increment byte4...
I know, just give me the pleasure of pointing out perf wins. Thats all I get
from this newsgroup ;-) PS, quit storing string IPs in SQL too ;-)


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


William Stacey said:
private void button48_Click(object sender, System.EventArgs e)
{
IPAddress[] ips = new IPAddress[5];
string tempIP = "192.168.0.";
for(int i = 0; i < 5; i++)
{
ips = IPAddress.Parse(tempIP + (i + 1));
}
foreach(IPAddress ip in ips)
{
Console.WriteLine("IP:"+ip.ToString());
}
}

--
William Stacey, MVP

Stuart Norris said:
Dear Readers,

I am attempting to define an array of IPAddress-es in C#.

I wish to have a array of address so I can try in order to connect to
them in a loop to handle unavailable hosts.

Todate since I do not know how to define an array of IPAddress-es. I
have been defining variable like:

IPAddress ipAddress1 = IPAddress.Parse( "10.1.1.1" );
IPAddress ipAddress2 = IPAddress.Parse( "11.1.1.1" );

Then define the end point using the hard coded name.

IPEndPoint remoteServerEndPoint = new IPEndPoint( ipAddress1, port );


How do I declare an array of IPAddress-es so I can loop around them
using an index counter?

I thought I could do IPAddress[] ipAddress = new IPAddress[2];

and then use ipAddress[0] = IPAddress.Parse( "10.1.1.1" );

This did not work.

What is the correct way to define an array of IPAdresses-es?

Thanks

Stuart
 
yep. You can also do this if you happen to have the byte[] around:

uint ipnum = BitConverter.ToUInt32(ba, 0);
ipAddress = new IPAddress(ipnum);

The IPAddress byte[] constructor does not seem to work or I am using it
wrong.

--
William Stacey, MVP

Justin Rogers said:
For perf it also takes a long...

ips = new IPAddress(byte1 << 24 + byte2 << 16 + byte3 << 8 + byte4);

In a loop, you just increment byte4...
I know, just give me the pleasure of pointing out perf wins. Thats all I get
from this newsgroup ;-) PS, quit storing string IPs in SQL too ;-)


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


William Stacey said:
private void button48_Click(object sender, System.EventArgs e)
{
IPAddress[] ips = new IPAddress[5];
string tempIP = "192.168.0.";
for(int i = 0; i < 5; i++)
{
ips = IPAddress.Parse(tempIP + (i + 1));
}
foreach(IPAddress ip in ips)
{
Console.WriteLine("IP:"+ip.ToString());
}
}

--
William Stacey, MVP

Stuart Norris said:
Dear Readers,

I am attempting to define an array of IPAddress-es in C#.

I wish to have a array of address so I can try in order to connect to
them in a loop to handle unavailable hosts.

Todate since I do not know how to define an array of IPAddress-es. I
have been defining variable like:

IPAddress ipAddress1 = IPAddress.Parse( "10.1.1.1" );
IPAddress ipAddress2 = IPAddress.Parse( "11.1.1.1" );

Then define the end point using the hard coded name.

IPEndPoint remoteServerEndPoint = new IPEndPoint( ipAddress1, port );


How do I declare an array of IPAddress-es so I can loop around them
using an index counter?

I thought I could do IPAddress[] ipAddress = new IPAddress[2];

and then use ipAddress[0] = IPAddress.Parse( "10.1.1.1" );

This did not work.

What is the correct way to define an array of IPAdresses-es?

Thanks

Stuart

 
Back
Top