Class NetworkUtilities
A class containing methods for common networking related operations.
Namespace: Unity.LiveCapture .Networking
Assembly: Unity.LiveCapture.Networking.dll
Syntax
public static class NetworkUtilities
Methods
CompareIPAddresses(IPAddress, IPAddress)
Counts the matching leading bits of two IPv4 addresses.
Declaration
public static int CompareIPAddresses(IPAddress a, IPAddress b)
Parameters
Returns
Type | Description |
---|---|
int | The length of the shared prefix. |
CreateSocket(ProtocolType)
Creates a new socket.
Declaration
public static Socket CreateSocket(ProtocolType protocol)
Parameters
Type | Name | Description |
---|---|---|
Protocol |
protocol | The protocol to use on the created socket. |
Returns
Type | Description |
---|---|
Socket | The new socket instance. |
DisposeSocket(Socket, int)
Cleanly closes a socket. May block for as long as the provided timeout.
Declaration
public static void DisposeSocket(Socket socket, int timeout = 1)
Parameters
Type | Name | Description |
---|---|---|
Socket | socket | The socket to close. |
int | timeout | The timeout in seconds to wait for the connection to rend any remaining data then be cleanly shut down. |
FindClosestAddresses(params IPEndPoint[])
Finds the local IP address and remote IP address that share the largest prefix.
Declaration
public static (IPAddress localAddress, IPEndPoint remoteEndPoint) FindClosestAddresses(params IPEndPoint[] remoteEndPoints)
Parameters
Type | Name | Description |
---|---|---|
IPEnd |
remoteEndPoints | The remote IP addresses to pick from. |
Returns
Type | Description |
---|---|
(IPAddress local |
A tuple containing the most similar local IP address and remote IP address pair, or IPAddress.Any and null if no suitable pair was found. |
Remarks
IP addresses that share a prefix are likely to be on the same subnet.
GetAddressBits(IPAddress)
Gets an IPv4 address as an integer.
Declaration
public static uint GetAddressBits(IPAddress address)
Parameters
Type | Name | Description |
---|---|---|
IPAddress | address | An IPv4 address to get the bits for. |
Returns
Type | Description |
---|---|
uint | The IP address bits. |
GetIPAddresses(bool)
Get the online IPv4 addresses from all network interfaces in the system.
Declaration
public static IPAddress[] GetIPAddresses(bool includeLoopback)
Parameters
Type | Name | Description |
---|---|---|
bool | includeLoopback | Include any addresses on the loopback interface. |
Returns
Type | Description |
---|---|
IPAddress[] | A new array containing the available IP addresses. |
GetIPInterfaces(bool)
Get the online IPv4 addresses from all network interfaces in the system.
Declaration
public static (IPAddress IP, NetworkInterface Interface)[] GetIPInterfaces(bool includeLoopback)
Parameters
Type | Name | Description |
---|---|---|
bool | includeLoopback | Include any addresses on the loopback interface. |
Returns
Type | Description |
---|---|
(IPAddress IP, Network |
A new array containing the available pairs of IP address and the Network |
GetPhysicalAddress(IPAddress)
Gets the MAC address of the network interface corresponding to a local IP address.
Declaration
public static PhysicalAddress GetPhysicalAddress(IPAddress address)
Parameters
Type | Name | Description |
---|---|---|
IPAddress | address | A local IP address. |
Returns
Type | Description |
---|---|
Physical |
The MAC address, or null if no interfaces match the provided address. |
GetRoutingInterface(IPEndPoint)
Gets the interface used to communicate with the specified remote.
Declaration
public static IPAddress GetRoutingInterface(IPEndPoint remoteEndPoint)
Parameters
Type | Name | Description |
---|---|---|
IPEnd |
remoteEndPoint | The end point to connect to. |
Returns
Type | Description |
---|---|
IPAddress | The IP address of the interface to use. |
IsPortAvailable(int)
Checks if a port is not used by any running program.
Declaration
public static bool IsPortAvailable(int port)
Parameters
Type | Name | Description |
---|---|---|
int | port | The port number to check. |
Returns
Type | Description |
---|---|
bool | True if the port is free for each protocol on all network interfaces, false otherwise. |
Remarks
This checks the TCP and UCP ports on all available network interfaces.
Exceptions
Type | Condition |
---|---|
Argument |
Thrown if |
See Also
IsPortAvailable(ProtocolType, int)
Checks if a port is not used by any running program.
Declaration
public static bool IsPortAvailable(ProtocolType protocol, int port)
Parameters
Type | Name | Description |
---|---|---|
Protocol |
protocol | The protocol to check the ports for. Must be UDP or TCP. |
int | port | The port number to check. |
Returns
Type | Description |
---|---|
bool | True if the port is free for the given protocol on all network interfaces, false otherwise. |
Remarks
This checks on all available network interfaces.
Exceptions
Type | Condition |
---|---|
Argument |
Thrown if |
See Also
IsPortAvailable(ProtocolType, IPAddress, int)
Checks if a port is not used by any running program.
Declaration
public static bool IsPortAvailable(ProtocolType protocol, IPAddress address, int port)
Parameters
Type | Name | Description |
---|---|---|
Protocol |
protocol | The protocol to check the ports for. Must be UDP or TCP. |
IPAddress | address | The address of the port to check. |
int | port | The port number to check. |
Returns
Type | Description |
---|---|
bool | True if the port is free, false otherwise. |
Remarks
This checks the port for a specific protocol and network interface.
Exceptions
Type | Condition |
---|---|
Argument |
Thrown if |
See Also
IsPortValid(int, out string)
Checks if a port number is valid to use.
Declaration
public static bool IsPortValid(int port, out string message)
Parameters
Type | Name | Description |
---|---|---|
int | port | The port number to check. |
string | message | Returns null if the port is suitable, otherwise it returns a message explaining why the port is not valid or recommended. May contain a value even if this method returns true. |
Returns
Type | Description |
---|---|
bool | True if the port number is strictly valid. |
QueryRoutingInterface(Socket, IPEndPoint)
Looks up the interface used to communicate with the specified remote from the routing table.
Declaration
public static IPAddress QueryRoutingInterface(Socket socket, IPEndPoint remoteEndPoint)
Parameters
Type | Name | Description |
---|---|---|
Socket | socket | The socket used for the lookup query. If null, a temporary socket is used. |
IPEnd |
remoteEndPoint | The end point to connect to. |
Returns
Type | Description |
---|---|
IPAddress | The IP address of the interface to use. |