using ip address component C#

江枫思渺然 提交于 2019-12-11 12:16:49

问题


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:


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.




回答2:


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




回答3:


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());


来源:https://stackoverflow.com/questions/3482880/using-ip-address-component-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!