SocketAddress 类

定义

存储派生类中的序列化信息 EndPoint

public ref class SocketAddress
public ref class SocketAddress : IEquatable<System::Net::SocketAddress ^>
public class SocketAddress
public class SocketAddress : IEquatable<System.Net.SocketAddress>
type SocketAddress = class
type SocketAddress = class
    interface IEquatable<SocketAddress>
Public Class SocketAddress
Public Class SocketAddress
Implements IEquatable(Of SocketAddress)
继承
SocketAddress
实现

示例

以下示例演示如何用于 SocketAddress 序列化类的 EndPoint 实例。 序列化后,基础字节缓冲区 SocketAddress 包含所有 IPEndPoint 状态信息。


//Creates an IpEndPoint.
IPAddress ipAddress = Dns.GetHostEntry("www.contoso.com").AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);

//Serializes the IPEndPoint.
SocketAddress socketAddress = ipLocalEndPoint.Serialize();

//Verifies that ipLocalEndPoint is now serialized by printing its contents.
Console.WriteLine("Contents of the socketAddress are: " + socketAddress.ToString());
//Checks the Family property.
Console.WriteLine("The address family of the socketAddress is: " + socketAddress.Family.ToString());
//Checks the underlying buffer size.
Console.WriteLine("The size of the underlying buffer is: " + socketAddress.Size.ToString());
   'Creates an IpEndPoint.
   Dim ipAddress As IPAddress = Dns.Resolve("www.contoso.com").AddressList(0)
   Dim ipLocalEndPoint As New IPEndPoint(ipAddress, 11000)
   
   'Serializes the IPEndPoint. 
   Dim socketAddress As SocketAddress = ipLocalEndPoint.Serialize()
   
   'Verifies that ipLocalEndPoint is now serialized by printing its contents.
   Console.WriteLine(("Contents of socketAddress are: " + socketAddress.ToString()))
   'Checks the Family property.
   Console.WriteLine(("The address family of socketAddress is: " + socketAddress.Family.ToString()))
   'Checks the underlying buffer size.
   Console.WriteLine(("The size of the underlying buffer is: " + socketAddress.Size.ToString()))
End Sub

注解

基础缓冲区的前 2 个字节保留为 AddressFamily 枚举值。 SocketAddress使用序列化IPEndPoint存储时,第三个和第四个字节用于存储端口号信息。 下一个字节用于存储 IP 地址。 可以通过引用其索引位置来访问此基础字节缓冲区中的任何信息;字节缓冲区使用从零开始的索引。 还可以使用 FamilySize 属性分别获取 AddressFamily 值和缓冲区大小。 若要以字符串形式查看此信息中的任何一个,请使用 ToString 该方法。

构造函数

名称 说明
SocketAddress(AddressFamily, Int32)

使用指定的地址系列和缓冲区大小创建类的新实例 SocketAddress

SocketAddress(AddressFamily)

为给定地址系列创建类的新实例 SocketAddress

属性

名称 说明
Buffer

获取可传递给本机 OS 调用的基础内存。

Family

AddressFamily获取当前SocketAddress值的枚举值。

Item[Int32]

获取或设置基础缓冲区中的指定索引元素。

Size

获取基础缓冲区大小 SocketAddress

方法

名称 说明
Equals(Object)

确定指定的 Object 值是否等于当前 Object

Equals(SocketAddress)

指示当前对象是否等于同一类型的另一个对象。

GetHashCode()

用作特定类型的哈希函数,适用于哈希算法和数据结构(如哈希表)。

GetMaximumAddressSize(AddressFamily)

获取给定 AddressFamily的最大所需缓冲区大小。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回有关套接字地址的信息。

适用于