1

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..

Community
  • 1
  • 1
Rapunzo
  • 966
  • 5
  • 21
  • 42
  • possible duplicate of [How to use IPAddress and IPv4Mask to obtain IP address range?](http://stackoverflow.com/questions/996848/how-to-use-ipaddress-and-ipv4mask-to-obtain-ip-address-range) – Darin Dimitrov Aug 14 '10 at 10:37

3 Answers3

2

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
  • 966
  • 5
  • 21
  • 42
1

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

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Hmm I see, soo what is the solution ? – Rapunzo Aug 14 '10 at 10:42
  • Well, a quick hack to increment only the last octet is to replace `++` with `+= 0x1 << 24`. – Stephen Cleary Aug 14 '10 at 10:44
  • 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 Aug 14 '10 at 10:59
  • Yes. That's exactly what I said - "a quick hack to increment *only* the last octet". – Stephen Cleary Aug 14 '10 at 11:38
  • 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 Aug 14 '10 at 12:06
  • 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 Aug 14 '10 at 12:38
1

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
  • 7,393
  • 12
  • 51
  • 79