SocketAddress 类


存储 EndPoint 派生类的序列化信息。Stores serialized information from EndPoint derived classes.

public ref class SocketAddress
public class SocketAddress
type SocketAddress = class
Public Class SocketAddress


下面的示例演示如何使用 SocketAddress 序列化 EndPoint 类的实例。The following example demonstrates how to use SocketAddress to serialize an instance of the EndPoint class. 序列化后,SocketAddress 的基础字节缓冲区包含所有 IPEndPoint 的状态信息。After serialization, the underlying byte buffer of the SocketAddress contains all of the IPEndPoint state information.

//Creates an IpEndPoint.
IPAddress^ ipAddress = Dns::Resolve( "" )->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("").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("").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 枚举值。The first 2 bytes of the underlying buffer are reserved for the AddressFamily enumerated value. SocketAddress 用于存储序列化的 IPEndPoint时,第三个和第四个字节用于存储端口号信息。When the SocketAddress is used to store a serialized IPEndPoint, the third and fourth bytes are used to store port number information. 下一个字节用于存储 IP 地址。The next bytes are used to store the IP address. 您可以通过引用此基础字节缓冲区中的任何信息来访问它的索引位置;字节缓冲区使用从零开始的索引。You can access any information within this underlying byte buffer by referring to its index position; the byte buffer uses zero-based indexing. 你还可以使用 FamilySize 属性分别获取 AddressFamily 值和缓冲区大小。You can also use the Family and Size properties to get the AddressFamily value and the buffer size, respectively. 若要以字符串形式查看此信息,请使用 ToString 方法。To view any of this information as a string, use the ToString method.



为给定的地址族创建 SocketAddress 类的新实例。Creates a new instance of the SocketAddress class for the given address family.

SocketAddress(AddressFamily, Int32)

使用指定的地址族和缓冲区大小创建 SocketAddress 类的新实例。Creates a new instance of the SocketAddress class using the specified address family and buffer size.



获取当前 AddressFamilySocketAddress 枚举值。Gets the AddressFamily enumerated value of the current SocketAddress.


获取或设置基础缓冲区中指定的索引元素。Gets or sets the specified index element in the underlying buffer.


获取 SocketAddress 的基础缓冲区大小。Gets the underlying buffer size of the SocketAddress.



确定指定的 Object 是否等同于当前的 ObjectDetermines whether the specified Object is equal to the current Object.


充当特定类型的哈希函数,适用于哈希算法以及哈希表之类的数据结构中。Serves as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table.


获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回有关套接字地址的信息。Returns information about the socket address.