none
Windows 10 IoT Core - How do you create a TCP Server and Client? RRS feed

  • Question

  • First my rant: Why are so many of the .Net methods missing from Windows 10 IoT Core? 

    I have written a series of programs in c# that run on a Raspberry Pi 2 with the Raspbian OS and the Mono Run time. 

    When I try to use these same code on the Raspberry Pi 2 with the Windows 10 IoT Core; I find that many of the methods we are using are missing. 

    What is worse I simple cannot find any guides or explanations on how to do this in any other way. 

    For example: System.Net.Sockets is missing IPHostEntry, TcpListener, TcpClient, NetworkStream, etc. 

    What am I attempting to do is get the local IP address of the Raspberry Pi 2 and then use this information to create TCP Clients and TCP Servers.

    If I can't resolve these issues we will have to abandon Windows 10 IoT and go back to Raspbian. 

    At least can someone explain why Windows 10 IoT Core is inferior to Raspbian running Mono? 

    Wednesday, September 23, 2015 11:34 AM

Answers

  • A section extracted from IotCoreDefaultApp, networkpresenter.cs shows how to get some of the basic stuff.

            public static string GetDirectConnectionName()
            {
                var icp = NetworkInformation.GetInternetConnectionProfile();
                if (icp != null)
                {
                    if(icp.NetworkAdapter.IanaInterfaceType == EthernetIanaType)
                    {
                        return icp.ProfileName;
                    }
                }
    
                return null;
            }
    
            public static string GetCurrentNetworkName()
            {
                var icp = NetworkInformation.GetInternetConnectionProfile();
                if (icp != null)
                {
                    return icp.ProfileName;
                }
    
                var resourceLoader = ResourceLoader.GetForCurrentView();
                var msg = resourceLoader.GetString("NoInternetConnection");
                return msg;
            }
    
            public static string GetCurrentIpv4Address()
            {
                var icp = NetworkInformation.GetInternetConnectionProfile();
                if (icp != null && icp.NetworkAdapter != null && icp.NetworkAdapter.NetworkAdapterId != null)
                {
                    var name = icp.ProfileName;
    
                    var hostnames = NetworkInformation.GetHostNames();
    
                    foreach (var hn in hostnames)
                    {
                        if (hn.IPInformation != null &&
                            hn.IPInformation.NetworkAdapter != null &&
                            hn.IPInformation.NetworkAdapter.NetworkAdapterId != null &&
                            hn.IPInformation.NetworkAdapter.NetworkAdapterId == icp.NetworkAdapter.NetworkAdapterId &&
                            hn.Type == HostNameType.Ipv4)
                        {
                            return hn.CanonicalName;
                        }
                    }
                }
    
                var resourceLoader = ResourceLoader.GetForCurrentView();
                var msg = resourceLoader.GetString("NoInternetConnection");
                return msg;
            }
    
    

    And yes, NetworkInformation.GetInternetConnectionProfile(); does exist in Windows 10 IoT/UWP.


    riclh


    Thursday, September 24, 2015 10:11 AM

All replies

  • Hi,

    Windows IoT supports the moft of the Universal Windows Platform features. Due to the hard sandboxing and modernizations of some libraries, not all .net source codes are compatible. Mono has a quite nice feature set according to the *original* .net environment

    The page https://msdn.microsoft.com/en-us/library/mt185489.aspx lists all accessable System.Net libs within inthe UWP aspect.

    Kind regards, Toby


    Best Regards. When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer. This helps us build a healthy and positive community.

    • Proposed as answer by Matthew McKee Wednesday, September 23, 2015 11:18 PM
    Wednesday, September 23, 2015 12:09 PM
  • The following will kick out the local IP address: 

    public string getip()
    {
      var icp = NetworkInformation.GetInternetConnectionProfile();
    
      if (icp != null && icp.NetworkAdapter != null)
      {
          var hostname =
              NetworkInformation.GetHostNames()
                  .SingleOrDefault(
                      hn =>
                      hn.IPInformation != null && hn.IPInformation.NetworkAdapter != null
                      && hn.IPInformation.NetworkAdapter.NetworkAdapterId
                      == icp.NetworkAdapter.NetworkAdapterId);
    
          if (hostname != null)
          {
              // the ip address
              return hostname.CanonicalName;
          }
    
      }
    }
    There is a ton of stuff in that NetworkInformation class.  Don't know how much of that will be useful to you.

    Wednesday, September 23, 2015 1:36 PM
  • Welcome to the world of async (only). That's the main difference between IoT and Mono.

    Basically all that you want to do can be done in IoT but none of the synchronous calls you are expecting in the libraries will be present, only the async ones.

    If you look at the IoTCoreDefaultApp you will see how UWP apps can get basic IP information (use the NetworkInformation class).

    There are also many examples of UWP clients and servers on GitHub

    https://github.com/ms-iot/samples

    Wednesday, September 23, 2015 1:45 PM
  • Thanks for the reference I'll take a look. 
    Thursday, September 24, 2015 9:35 AM
  • Unfortunately the "GetInternetConnectionProfile" method does not exist in the UWP, but thanks for the suggestion. 
    Thursday, September 24, 2015 9:37 AM
  • A section extracted from IotCoreDefaultApp, networkpresenter.cs shows how to get some of the basic stuff.

            public static string GetDirectConnectionName()
            {
                var icp = NetworkInformation.GetInternetConnectionProfile();
                if (icp != null)
                {
                    if(icp.NetworkAdapter.IanaInterfaceType == EthernetIanaType)
                    {
                        return icp.ProfileName;
                    }
                }
    
                return null;
            }
    
            public static string GetCurrentNetworkName()
            {
                var icp = NetworkInformation.GetInternetConnectionProfile();
                if (icp != null)
                {
                    return icp.ProfileName;
                }
    
                var resourceLoader = ResourceLoader.GetForCurrentView();
                var msg = resourceLoader.GetString("NoInternetConnection");
                return msg;
            }
    
            public static string GetCurrentIpv4Address()
            {
                var icp = NetworkInformation.GetInternetConnectionProfile();
                if (icp != null && icp.NetworkAdapter != null && icp.NetworkAdapter.NetworkAdapterId != null)
                {
                    var name = icp.ProfileName;
    
                    var hostnames = NetworkInformation.GetHostNames();
    
                    foreach (var hn in hostnames)
                    {
                        if (hn.IPInformation != null &&
                            hn.IPInformation.NetworkAdapter != null &&
                            hn.IPInformation.NetworkAdapter.NetworkAdapterId != null &&
                            hn.IPInformation.NetworkAdapter.NetworkAdapterId == icp.NetworkAdapter.NetworkAdapterId &&
                            hn.Type == HostNameType.Ipv4)
                        {
                            return hn.CanonicalName;
                        }
                    }
                }
    
                var resourceLoader = ResourceLoader.GetForCurrentView();
                var msg = resourceLoader.GetString("NoInternetConnection");
                return msg;
            }
    
    

    And yes, NetworkInformation.GetInternetConnectionProfile(); does exist in Windows 10 IoT/UWP.


    riclh


    Thursday, September 24, 2015 10:11 AM
  • Thanks everyone for the help

    Thursday, September 24, 2015 10:12 AM
  • Thanks for continuing to help, I've found the problem; and I had to go outside for a bit to shout a brick wall for a few minutes. 

    The reference I was looking at was the System.Net, however the functions you pointed out were in the Windows.Networking.Connectivity. 

    Either way now I know where to look to get stuff working. 


    Thursday, September 24, 2015 10:53 AM
  • Finding the 'right' documentation in the MSDN is not easy!

    I tend to use Intellisense and let it prompt me with the include to add :)


    riclh

    Thursday, September 24, 2015 4:27 PM
  • Yeah, basically I was stubbornly using the basic System references for cross platform compatibility, but now I know the functionality I need is actually in the Windows UWP libraries I'll have to start using those. 

    Success! Using the code you suggested I have finally got the Pi to do what I needed. 

    Friday, September 25, 2015 8:59 AM
  • For me, it doesn't work, EthernetIanaType is unknown, do you know what I would need to reference?
    Friday, February 5, 2016 7:27 PM
  • can you share your server and client code? 

    i am trying to create a server to sendo commands over the internet without sucess :/

    thanks!! 

    Tuesday, October 18, 2016 7:28 PM