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:
stringDisplayNameHostNameTypeTypestringIPInformation
Methods:
boolEquals(object)intGetHashCode()stringToString()
EndpointPair
Represents a pair of network endpoints (local and remote).
Properties:
HostNameLocalHostNamestringLocalServiceNameHostNameRemoteHostNamestringRemoteServiceName
Network Information
Windows.Networking.Connectivity
NetworkInformation
Provides static methods to retrieve network connectivity information.
Methods:
ConnectionProfileGetInternetConnectionProfile()IMapViewGetAdapters()NetworkConnectivityLevelGetNetworkConnectivityLevel()
Events:
event TypedEventHandlerNetworkStatusChanged
ConnectionProfile
Represents a network connection profile.
Properties:
NetworkCostTypeCostHintstringNameboolIsWwan(NetworkWwanAccessCategory)boolIsWlan
NetworkConnectivityLevel
Defines the level of network connectivity.
NoneLocalAccessConstrainedInternetAccessInternetAccess
Sockets
Windows.Networking.Sockets
StreamSocket
Represents a streaming socket for sending and receiving data.
Methods:
IAsyncActionConnectAsync(HostName, string)IAsyncActionConnectAsync(HostName, string, HostName, string)DataReaderGetDataReader()DataWriterGetDataWriter()IAsyncActionDisconnectAsync()
DatagramSocket
Represents a datagram socket for sending and receiving UDP packets.
Methods:
IAsyncActionBindServiceNameAsync(string)IAsyncActionBindEndpointAsync(HostName, string)DataReaderGetDataReader()DataWriterGetDataWriter()
Events:
event TypedEventHandlerMessageReceived
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.");
}
}