views:

105

answers:

3

I am trying to use a component which named as A C# IP Address Control but it has a problem I think. because when I increase its value 1, it gives me some wrong result. forexample

ipAddressControl3.Text = "192.168.1.25";
IPAddress ipAddress1 = new IPAddress(ipAddressControl3.GetAddressBytes());
ipAddress1.Address++;
MessageBox.Show(ipAddress1.ToString());

returns : "193.168.1.25" ! but I expect "192.168.1.26"

what is the problem ?

here is the components link : A C# IP Address Control

edit : Maybe solution like this but I couldnt implemented it..

+1  A: 

IP address are stored in network byte order (big-endian), whereas integers on Intel platforms are little-endian.

Stephen Cleary
Hmm I see, soo what is the solution ?
Rapunzo
Well, a quick hack to increment only the last octet is to replace `++` with `+= 0x1 << 24`.
Stephen Cleary
I tried it but in this situaton it fails..ıpAddressControl3.Text = "0.0.0.255";IPAddress ipAddress1 = new IPAddress(ıpAddressControl3.GetAddressBytes());ipAddress1.Address += 0x1 << 24;MessageBox.Show(ipAddress1.ToString());resunt is : 0.0.0.0
Rapunzo
Yes. That's exactly what I said - "a quick hack to increment *only* the last octet".
Stephen Cleary
if so can I convert the value (big-endian) to (little-endian) or antipodean and increase 0.0.0.255 to 0.0.1.0 ?
Rapunzo
Yes. That would work. Once the value is in little-endian, then you could just increment it using `++` and it would work as expected.
Stephen Cleary
+1  A: 

Try this:

ipAddressControl3.Text = "192.168.1.25";

byte[] ip = ipAddressControl3.GetAddressBytes();
ip[3] = (byte) (++ip[3]);

IPAddress ipAddress1 = new IPAddress(ip);
MessageBox.Show(ipAddress1.ToString());
kofucii
unfortunately it gives 0.0.0.0 result for 0.0.0.255I expect it have to be 0.0.1.0
Rapunzo
A: 

I convert my ip big endian to little like this :

int ipaddress= IPAddress.NetworkToHostOrder(BitConverter.ToInt32(IPAddress.Parse(ipAddressControl3.Text).GetAddressBytes(), 0));

and it work.

Rapunzo