1

I have my Netduino Plus 2 go out to a web service to look up some values that I would like it to use in my project. One of the values that I have the Netduino check is its preferred IP address. If the Netduino has a different IPAddress than its preferred, I want to change it.

I have a method in my project called BindIPAddress (below) that takes a string.

I am getting a SocketException with a code of 10022 for invalid argument. This happens when I call this.Socket.Bind. My class has a property called Socket to hold the Socket value. Is it because my socket already has an endpoint ? I tried adding this.Socket = null and then this.Socket = new (....... thinking we need a new socket to work with, but this returned the same error.

Please advise how I can change my IP address from one static IP address to another.

 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());
    }
Bill Greer
  • 3,046
  • 9
  • 49
  • 80
  • Is my approach wrong ? – Bill Greer Apr 19 '15 at 00:26
  • Have you created a socket object by calling its constructor properly before binding it? Default Socket constructor takes three arguments: AddressFamily, SocketType and ProtocolType. Before binding your socket to an EndPoint, make sure that you have successfully created one. – Unavailable Apr 22 '15 at 08:00

1 Answers1

2

There may be 2 possible solutions to this problem. The first one is, you can programatically set the preffered IP addresses as the way you have tried to do so and the second one is, you can use MFDeploy tool, which comes with .NET Micro Framework SDK bundle that allows you to set your embedded device network configuration statically before running your application on it.

1) Since you have not provided rest of the code, here's a proper way to bind your socket to an EndPoint (actually, I would not design that class and binding function as the way you posted here, but just wanted to underline the missing parts of your code):

    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) Or without programming, just by using MFDeploy tool you can set the preferred IP addresses after plugging your embedded device into your PC and following the path below:

MFDeploy > Target > Configuration > Network

then enter the preferred IP addresses. That's simply all.

Unavailable
  • 681
  • 4
  • 13
  • (ps: I'm not sure if your embedded device requires further function calls after setting static network settings programatically (like releasing the underlying network resources then using new ones etc. Please inform me after implementing your code, best regards.) – Unavailable Apr 22 '15 at 09:34
  • I appreciate your response. I will try it and report back. I'm curious as to why you would not design the class and binding function as I have ? I'm trying to separate some concerns in my project. For example, this method I have posted is part of a class called Network. I want the network class to be responsible for going setting the IP Address. Any feedback would be appreciated. I am trying to follow best practices. – Bill Greer Apr 22 '15 at 15:08