我让我的 Netduino Plus 2 使用 Web 服务来查找我希望它在我的项目中使用的一些值。我让 Netduino 检查的值之一是它的首选 IP 地址。如果 Netduino 的 IPAddress 与首选 IPAddress 不同,我想更改它。
我的项目中有一个名为 BindIPAddress(如下)的方法,它接受一个字符串。
对于无效参数,我收到一个代码为 10022 的 SocketException。当我调用 this.Socket.Bind 时会发生这种情况。我的班级有一个名为 Socket 的属性来保存 Socket 值。是因为我的套接字已经有一个端点吗?我尝试添加 this.Socket = null 然后 this.Socket = new (....... 认为我们需要一个新的套接字来使用,但这返回了相同的错误。
请告知如何将我的 IP 地址从一个静态 IP 地址更改为另一个。
public void BindIPAddress(string strIPAddress)
{
try
{
Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].EnableStaticIP(strIPAddress, "255.255.240.0", "10.0.0.1");
Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].EnableStaticDns(new string[] { "10.0.0.2", "10.0.0.3" });
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(strIPAddress), 80);
this.Socket.Bind(ep);
this.IpAddress = strIPAddress;
}
catch(SocketException exc)
{
Debug.Print(exc.Message);
Debug.Print(exc.ErrorCode.ToString());
}
catch(Exception ex)
{
Debug.Print(ex.Message);
}
//Debug.Print(ep.Address.ToString());
}