SocketAddress 类

定义

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

public ref class SocketAddress
public class SocketAddress
type SocketAddress = class
Public Class 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 个字节保留为 AddressFamily 枚举值。 SocketAddress当用于存储序列化IPEndPoint时,第三个和第四个字节用于存储端口号信息。 下一个字节用于存储 IP 地址。 可以通过引用其索引位置来访问此基础字节缓冲区中的任何信息;字节缓冲区使用从零开始的索引。 还可以使用 FamilySize 属性分别获取 AddressFamily 值和缓冲区大小。 若要将此信息中的任何一个视为字符串,请使用 ToString 该方法。

构造函数

SocketAddress(AddressFamily)

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

SocketAddress(AddressFamily, Int32)

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

属性

Family

获取当前 AddressFamilySocketAddress 枚举值。

Item[Int32]

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

Size

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

方法

Equals(Object)

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

GetHashCode()

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

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

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

适用于