Struct NetworkEndpoint
Representation of an endpoint on the network. Typically, this means an IP address and a port
number, and the API provides means to make working with this kind of endpoint easier.
Analoguous to a sockaddr structure in traditional BSD sockets.
Inherited Members
Namespace: Unity.Networking.Transport
Syntax
public struct NetworkEndpoint : IEquatable<NetworkEndpoint>
Remarks
While this structure can store an IP address, it can't be used to store domain names. In
general, the Unity Transport package does not handle domain names and it is the user's
responsibility to resolve domain names to IP addresses. This can be done with
System.Net.Dns.GetHostEntryAsync".
Examples
The code below shows how to obtain endpoint structures for different IP addresses and port
combinations (noted in comments in the IP_ADDRESS:PORT format):
// 127.0.0.1:7777
NetworkEndpoint.LoopbackIpv4.WithPort(7777);
// 0.0.0.0:0
NetworkEndpoint.AnyIpv4;
// 192.168.0.42:7778
NetworkEndpoint.Parse("192.168.0.42", 7778);
// [fe80::210:5aff:feaa:20a2]:52000
NetworkEndpoint.Parse("fe80::210:5aff:feaa:20a2", 52000, NetworkFamily.Ipv6);
Properties
Address
String representation of the endpoint. Same as ToString().
Declaration
public readonly string Address { get; }
Property Value
| Type | Description |
|---|---|
| String | Endpoint represented as a string. |
AnyIpv4
Shortcut for the wildcard IPv4 address (0.0.0.0).
Declaration
public static readonly NetworkEndpoint AnyIpv4 { get; }
Property Value
| Type | Description |
|---|---|
| NetworkEndpoint | Endpoint structure for the 0.0.0.0 IPv4 address. |
AnyIpv6
Shortcut for the wildcard IPv6 address (::).
Declaration
public static readonly NetworkEndpoint AnyIpv6 { get; }
Property Value
| Type | Description |
|---|---|
| NetworkEndpoint | Endpoint structure for the :: IPv6 address. |
Family
Get or set the family of the endpoint.
Declaration
public NetworkFamily Family { get; set; }
Property Value
| Type | Description |
|---|---|
| NetworkFamily | Address family of the endpoint. |
IsAny
Whether the endpoint is for a wildcard address.
Declaration
public readonly bool IsAny { get; }
Property Value
| Type | Description |
|---|---|
| Boolean | True if the address is 0.0.0.0 or ::. |
IsLoopback
Whether the endpoint is for a loopback address.
Declaration
public readonly bool IsLoopback { get; }
Property Value
| Type | Description |
|---|---|
| Boolean | True if the address is 127.0.0.1 or ::1. |
IsValid
Whether the endpoint is valid or not (whether it's IPv4 or IPv6).
Declaration
public readonly bool IsValid { get; }
Property Value
| Type | Description |
|---|---|
| Boolean | True if family is IPv4 or IPv6, false otherwise. |
Length
Length of the raw endpoint representation.
Declaration
public readonly int Length { get; }
Property Value
| Type | Description |
|---|---|
| Int32 | Length in bytes. |
LoopbackIpv4
Shortcut for the loopback/localhost IPv4 address (127.0.0.1).
Declaration
public static readonly NetworkEndpoint LoopbackIpv4 { get; }
Property Value
| Type | Description |
|---|---|
| NetworkEndpoint | Endpoint structure for the 127.0.0.1 IPv4 address. |
LoopbackIpv6
Shortcut for the loopback/localhost IPv6 address (::1).
Declaration
public static readonly NetworkEndpoint LoopbackIpv6 { get; }
Property Value
| Type | Description |
|---|---|
| NetworkEndpoint | Endpoint structure for the ::1 IPv6 address. |
Port
Get or set the port number of the endpoint.
Declaration
public ushort Port { get; set; }
Property Value
| Type | Description |
|---|---|
| UInt16 | Port number. |
RawPort
Get or set the raw value of the endpoint's port number. This is only useful to interface with low-level native libraries. Prefer Port in most circumstances, since that value will always match the endianness of the current platform.
Declaration
public ushort RawPort { get; set; }
Property Value
| Type | Description |
|---|---|
| UInt16 | Port value in network byte order. |
Methods
Equals(Object)
Declaration
public override bool Equals(object other)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | other |
Returns
| Type | Description |
|---|---|
| Boolean |
Overrides
Equals(NetworkEndpoint)
Declaration
public bool Equals(NetworkEndpoint other)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkEndpoint | other |
Returns
| Type | Description |
|---|---|
| Boolean |
Implements
GetHashCode()
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| Int32 |
Overrides
GetRawAddressBytes()
Get the raw representation of the endpoint. This is only useful for low-level code that
must interface with native libraries, for example if writing a custom implementation of
INetworkInterface. The raw representation of an endpoint will match that
of an appropriate sockaddr structure for the current platform.
Declaration
public NativeArray<byte> GetRawAddressBytes()
Returns
| Type | Description |
|---|---|
| NativeArray<Byte> | Temporary native array with raw representation of the endpoint. |
Parse(String, UInt16, NetworkFamily)
Parse the provided IP address and port. Prefer this method when parsing IP addresses and ports that are known to be good (e.g. hardcoded values).
Declaration
public static NetworkEndpoint Parse(string address, ushort port, NetworkFamily family = NetworkFamily.Ipv4)
Parameters
| Type | Name | Description |
|---|---|---|
| String | address | IP address to parse. |
| UInt16 | port | Port number to parse. |
| NetworkFamily | family | Address family of the provided address. |
Returns
| Type | Description |
|---|---|
| NetworkEndpoint | Parsed endpoint, or a default value if couldn't parse successfully. |
SetRawAddressBytes(NativeArray<Byte>, NetworkFamily)
Set the raw representation of the endpoint. This is only useful for low-level code that
must interface with native libraries, for example if writing a custom implementation of
INetworkInterface. The raw representation of an endpoint must match that
of an appropriate sockaddr structure for the current platform.
Declaration
public void SetRawAddressBytes(NativeArray<byte> bytes, NetworkFamily family = NetworkFamily.Ipv4)
Parameters
| Type | Name | Description |
|---|---|---|
| NativeArray<Byte> | bytes | Raw representation of the endpoint. |
| NetworkFamily | family | Address family of the raw representation. |
ToFixedString()
Get a fixed string representation of the endpoint. Useful for contexts where managed types (like String) can't be used (e.g. Burst-compiled code).
Declaration
public FixedString128Bytes ToFixedString()
Returns
| Type | Description |
|---|---|
| FixedString128Bytes | Fixed string representation of the endpoint. |
ToString()
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| String |
Overrides
TryParse(String, UInt16, out NetworkEndpoint, NetworkFamily)
Declaration
public static bool TryParse(string address, ushort port, out NetworkEndpoint endpoint, NetworkFamily family = NetworkFamily.Ipv4)
Parameters
| Type | Name | Description |
|---|---|---|
| String | address | |
| UInt16 | port | |
| NetworkEndpoint | endpoint | |
| NetworkFamily | family |
Returns
| Type | Description |
|---|---|
| Boolean |
WithPort(UInt16)
Get a copy of the endpoint that uses the specified port.
Declaration
public NetworkEndpoint WithPort(ushort port)
Parameters
| Type | Name | Description |
|---|---|---|
| UInt16 | port | Port number of the new endpoint. |
Returns
| Type | Description |
|---|---|
| NetworkEndpoint | Copy of the endpoint that uses the given port. |
Operators
Equality(NetworkEndpoint, NetworkEndpoint)
Declaration
public static bool operator ==(NetworkEndpoint lhs, NetworkEndpoint rhs)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkEndpoint | lhs | |
| NetworkEndpoint | rhs |
Returns
| Type | Description |
|---|---|
| Boolean |
Inequality(NetworkEndpoint, NetworkEndpoint)
Declaration
public static bool operator !=(NetworkEndpoint lhs, NetworkEndpoint rhs)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkEndpoint | lhs | |
| NetworkEndpoint | rhs |
Returns
| Type | Description |
|---|---|
| Boolean |