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::Resolve( "www.contoso.com" )->AddressList[ 0 ];
IPEndPoint^ ipLocalEndPoint = gcnew 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: {0}", socketAddress );
//Checks the Family property.
Console::WriteLine( "The address family of the socketAddress is: {0}", socketAddress->Family );
//Checks the underlying buffer size.
Console::WriteLine( "The size of the underlying buffer is: {0}", socketAddress->Size );

//Creates an IpEndPoint.
IPAddress ipAddress = Dns.Resolve("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 个字节是为枚举值保留的 AddressFamilySocketAddress当 用于存储序列化IPEndPoint的 时,将使用第三个和第四个字节来存储端口号信息。 接下来的字节用于存储 IP 地址。 可以通过引用该缓冲区的索引位置来访问此基础字节缓冲区中的任何信息;字节缓冲区使用从零开始的索引。 还可以使用 FamilySize 属性分别获取 AddressFamily 值和缓冲区大小。 若要以字符串形式查看此信息中的任何一个,请使用 ToString 方法。

构造函数

SocketAddress(AddressFamily)

为给定的地址族创建 SocketAddress 类的新实例。

SocketAddress(AddressFamily, Int32)

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

属性

Buffer

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

Family

获取当前 AddressFamilySocketAddress 枚举值。

Item[Int32]

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

Size

获取 SocketAddress 的基础缓冲区大小。

方法

Equals(Object)

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

Equals(SocketAddress)

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

GetHashCode()

充当特定类型的哈希函数,适用于哈希算法以及哈希表之类的数据结构中。

GetMaximumAddressSize(AddressFamily)

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

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

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

适用于