Windows.Networking Namespace

The Windows.Networking namespace provides classes for network operations in Universal Windows Platform (UWP) applications. This includes functionalities for IP addresses, host names, connections, sockets, and network status.

Core Classes

Windows.Networking

HostName

Represents a network host identified by a name or an IP address.

Properties:
  • string DisplayName
  • HostNameType Type
  • string IPInformation
Methods:
  • bool Equals(object)
  • int GetHashCode()
  • string ToString()

EndpointPair

Represents a pair of network endpoints (local and remote).

Properties:
  • HostName LocalHostName
  • string LocalServiceName
  • HostName RemoteHostName
  • string RemoteServiceName

Network Information

Windows.Networking.Connectivity

NetworkInformation

Provides static methods to retrieve network connectivity information.

Methods:
  • ConnectionProfile GetInternetConnectionProfile()
  • IMapView GetAdapters()
  • NetworkConnectivityLevel GetNetworkConnectivityLevel()
Events:
  • event TypedEventHandler NetworkStatusChanged

ConnectionProfile

Represents a network connection profile.

Properties:
  • NetworkCostType CostHint
  • string Name
  • bool IsWwan(NetworkWwanAccessCategory)
  • bool IsWlan

NetworkConnectivityLevel

Defines the level of network connectivity.

  • None
  • LocalAccess
  • ConstrainedInternetAccess
  • InternetAccess

Sockets

Windows.Networking.Sockets

StreamSocket

Represents a streaming socket for sending and receiving data.

Methods:
  • IAsyncAction ConnectAsync(HostName, string)
  • IAsyncAction ConnectAsync(HostName, string, HostName, string)
  • DataReader GetDataReader()
  • DataWriter GetDataWriter()
  • IAsyncAction DisconnectAsync()

DatagramSocket

Represents a datagram socket for sending and receiving UDP packets.

Methods:
  • IAsyncAction BindServiceNameAsync(string)
  • IAsyncAction BindEndpointAsync(HostName, string)
  • DataReader GetDataReader()
  • DataWriter GetDataWriter()
Events:
  • event TypedEventHandler MessageReceived

Example Usage (C#)

using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Networking.Connectivity;
using System;
using System.Threading.Tasks;

public async Task ConnectToServer(string hostname, string port)
{
    var socket = new StreamSocket();
    var hostName = new HostName(hostname);

    try
    {
        await socket.ConnectAsync(hostName, port);
        Console.WriteLine($"Connected to {hostname}:{port}");

        // Send/Receive data
        using (var writer = new DataWriter(socket.OutputStream))
        {
            writer.WriteString("Hello Server!");
            await writer.StoreAsync();
        }

        using (var reader = new DataReader(socket.InputStream))
        {
            var bytesLoaded = await reader.LoadAsync(256); // Load up to 256 bytes
            if (bytesLoaded > 0)
            {
                string response = reader.ReadString(bytesLoaded);
                Console.WriteLine($"Received: {response}");
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Connection failed: {ex.Message}");
    }
    finally
    {
        socket.Dispose();
    }
}

public void GetNetworkStatus()
{
    var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
    if (connectionProfile != null)
    {
        var connectivityLevel = connectionProfile.GetNetworkConnectivityLevel();
        Console.WriteLine($"Network Connectivity Level: {connectivityLevel}");
        Console.WriteLine($"Connection Profile Name: {connectionProfile.Name}");
    }
    else
    {
        Console.WriteLine("No internet connection profile found.");
    }
}