docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class NetworkUtilities

    A class containing methods for common networking related operations.

    Inheritance
    object
    NetworkUtilities
    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
    Type Name Description
    IPAddress a

    The first IP address.

    IPAddress b

    The second IP address.

    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
    ProtocolType 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
    IPEndPoint[] remoteEndPoints

    The remote IP addresses to pick from.

    Returns
    Type Description
    (IPAddress localAddress, IPEndPoint remoteEndPoint)

    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, NetworkInterface Interface)[]

    A new array containing the available pairs of IP address and the NetworkInterface that it belongs to.

    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
    PhysicalAddress

    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
    IPEndPoint 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
    ArgumentOutOfRangeException

    Thrown if port is not a valid port number.

    See Also
    IsPortAvailable(ProtocolType, int)
    IsPortAvailable(ProtocolType, IPAddress, int)

    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
    ProtocolType 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
    ArgumentOutOfRangeException

    Thrown if port is not a valid port number.

    See Also
    IsPortAvailable(int)
    IsPortAvailable(ProtocolType, IPAddress, int)

    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
    ProtocolType 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
    ArgumentOutOfRangeException

    Thrown if port is not a valid port number.

    See Also
    IsPortAvailable(int)
    IsPortAvailable(ProtocolType, int)

    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.

    IPEndPoint remoteEndPoint

    The end point to connect to.

    Returns
    Type Description
    IPAddress

    The IP address of the interface to use.

    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)