【问题标题】:How to properly change a static IP address using the Microframework?如何使用微框架正确更改静态 IP 地址?
【发布时间】:2015-04-17 15:50:22
【问题描述】:

我让我的 Netduino Plus 2 使用 Web 服务来查找我希望它在我的项目中使用的一些值。我让 Netduino 检查的值之一是它的首选 IP 地址。如果 Netduino 的 IPAddress 与首选 IPAddress 不同,我想更改它。

我的项目中有一个名为 BindIPAddress(如下)的方法,它接受一个字符串。

我收到一个 SocketException 代码为 10022 的无效参数。当我调用 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());
    }

【问题讨论】:

  • 我的方法错了吗?
  • 您是否在绑定之前通过正确调用其构造函数来创建套接字对象?默认 Socket 构造函数采用三个参数:AddressFamily、SocketType 和 ProtocolType。在将您的套接字绑定到端点之前,请确保您已成功创建一个。

标签: c# sockets microcontroller .net-micro-framework netduino


【解决方案1】:

这个问题可能有 2 种可能的解决方案。第一个是,您可以按照您尝试的方式以编程方式设置首选 IP 地址,第二个是,您可以使用 .NET Micro Framework SDK 包附带的 MFDeploy 工具这允许您在运行应用程序之前静态设置嵌入式设备网络配置。

1) 由于您没有提供其余代码,这是将您的套接字绑定EndPoint 的正确方法(实际上,我不会将该类和绑定函数设计为方式您在此处发布,但只是想强调代码中缺少的部分):

    public void BindIPAddress(string strIPAddr)
    {
        Socket sock = null;
        IPEndPoint ipe = null;
        NetworkInterface[] ni = null;

        try
        {
            ipe = new IPEndPoint(IPAddress.Parse(strIPAddr), 80);
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);    // Assuming the WebService is connection oriented (TCP)
            // sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);  // if it broadcasts UDP packets, use this line (UDP)
            ni = NetworkInterface.GetAllNetworkInterfaces();

            if (ni != null && ni.Length > 0)
            {
                ni[0].EnableStaticIP(strIPAddr, "255.255.240.0", "10.0.0.1");
                ni[0].EnableStaticDns(new string[2] { "10.0.0.2", "10.0.0.3" });
                sock.Bind(ipe);
                this.Socket = sock;
                this.IpAddress = strIPAddr;
            }
            else
                throw new Exception("Network interface could not be retrieved successfully!");
        }
        catch(Exception ex)
        {
            Debug.Print(ex.Message);
        }
    }

2) 或者无需编程,只需使用 MFDeploy 工具,您可以在将嵌入式设备插入 PC 后,按照以下路径设置首选 IP 地址:

MFDeploy > 目标 > 配置 > 网络

然后输入首选 IP 地址。仅此而已。

【讨论】:

  • (ps:我不确定您的嵌入式设备在以编程方式设置静态网络设置后是否需要进一步的函数调用(例如释放底层网络资源然后使用新资源等。请在实现您的代码后通知我,最好的问候。)
  • 感谢您的回复。我会尝试并报告。我很好奇你为什么不像我那样设计类和绑定函数?我试图在我的项目中分离一些问题。例如,我发布的这个方法是一个名为 Network 的类的一部分。我希望网络类负责设置 IP 地址。对于任何反馈,我们都表示感谢。我正在努力遵循最佳做法。
猜你喜欢
  • 2014-02-09
  • 2022-12-20
  • 1970-01-01
  • 2020-06-12
  • 2021-09-22
  • 2020-04-26
  • 2015-05-05
  • 2012-09-26
  • 2018-11-04
相关资源
最近更新 更多