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.
Implements
Inherited Members
Namespace: Unity.Networking.Transport
Assembly: Unity.Networking.Transport.dll
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"
for example.
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 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 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 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 bool IsAny { get; }
Property Value
Type | Description |
---|---|
bool | True if the address is 0.0.0.0 or ::. |
IsLoopback
Whether the endpoint is for a loopback address.
Declaration
public bool IsLoopback { get; }
Property Value
Type | Description |
---|---|
bool | True if the address is 127.0.0.1 or ::1. |
IsValid
Whether the endpoint is valid or not (based on the address family).
Declaration
public bool IsValid { get; }
Property Value
Type | Description |
---|---|
bool | True if family is IPv4, IPv6 or custom, false otherwise. |
Length
Length of the raw address representation. Does not include the size of the port and of the family. Generally, there's no use for this property except for low-level code.
Declaration
public int Length { get; }
Property Value
Type | Description |
---|---|
int | Length in bytes. |
LoopbackIpv4
Shortcut for the loopback/localhost IPv4 address (127.0.0.1).
Declaration
public static 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 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 (IPv4 or IPv6 only).
Declaration
public ushort Port { get; set; }
Property Value
Type | Description |
---|---|
ushort | 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
[Obsolete("Use Port instead, and use standard C# APIs to convert to/from network byte order.")]
public ushort RawPort { get; set; }
Property Value
Type | Description |
---|---|
ushort | 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 |
---|---|
bool |
Overrides
Equals(NetworkEndpoint)
Declaration
public bool Equals(NetworkEndpoint other)
Parameters
Type | Name | Description |
---|---|---|
NetworkEndpoint | other |
Returns
Type | Description |
---|---|
bool |
GetHashCode()
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int |
Overrides
GetRawAddressBytes()
Get the raw representation of the endpoint's address. This is only useful for low-level code that must interface with native libraries, for example if writing a custom implementation of INetworkInterface.
Declaration
public NativeArray<byte> GetRawAddressBytes()
Returns
Type | Description |
---|---|
NativeArray<byte> | Temporary native array with raw representation of the endpoint. |
Parse(string, ushort, 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. |
ushort | 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's address and set its family. This is only useful for low-level code that must interface with native libraries, for example if writing a custom implementation of INetworkInterface.
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, ushort, 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 | |
ushort | port | |
NetworkEndpoint | endpoint | |
NetworkFamily | family |
Returns
Type | Description |
---|---|
bool |
WithPort(ushort)
Get a copy of the endpoint that uses the specified port.
Declaration
public NetworkEndpoint WithPort(ushort port)
Parameters
Type | Name | Description |
---|---|---|
ushort | port | Port number of the new endpoint. |
Returns
Type | Description |
---|---|
NetworkEndpoint | Copy of the endpoint that uses the given port. |
Operators
operator ==(NetworkEndpoint, NetworkEndpoint)
Declaration
public static bool operator ==(NetworkEndpoint lhs, NetworkEndpoint rhs)
Parameters
Type | Name | Description |
---|---|---|
NetworkEndpoint | lhs | |
NetworkEndpoint | rhs |
Returns
Type | Description |
---|---|
bool |
operator !=(NetworkEndpoint, NetworkEndpoint)
Declaration
public static bool operator !=(NetworkEndpoint lhs, NetworkEndpoint rhs)
Parameters
Type | Name | Description |
---|---|---|
NetworkEndpoint | lhs | |
NetworkEndpoint | rhs |
Returns
Type | Description |
---|---|
bool |