Export (0) Print
Expand All

SocketAddress Class

Stores serialized information from EndPoint derived classes.

For a list of all members of this type, see SocketAddress Members.

System.Object
   System.Net.SocketAddress

[Visual Basic]
Public Class SocketAddress
[C#]
public class SocketAddress
[C++]
public __gc class SocketAddress
[JScript]
public class SocketAddress

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The first two bytes of the underlying buffer are reserved for the AddressFamily enumerated value. When the SocketAddress is used to store a serialized IPEndPoint. The third and fourth bytes are used to store port number information. 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. You can also use the Family and Size properties to get the AddressFamily value and the buffer size, respectively. To view any of this information as a string, use the ToString method.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use SocketAddress to serialize an instance of the EndPoint class. After serialization, the underlying byte buffer of the SocketAddress contains all of the IPEndPoint state information.

[Visual Basic] 
'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 'MySerializeIPEndPointClassMethod
    

[C#] 

//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());


[C++] 
//Creates an IpEndPoint.
IPAddress* ipAddress = Dns::Resolve(S"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(S"Contents of the socketAddress are: {0}", socketAddress);
//Checks the Family property.
Console::WriteLine(S"The address family of the socketAddress is: {0}",
   __box( socketAddress->Family));
//Checks the underlying buffer size.
Console::WriteLine(S"The size of the underlying buffer is: {0}",
   __box( socketAddress->Size));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Net

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System (in System.dll)

See Also

SocketAddress Members | System.Net Namespace

Show:
© 2014 Microsoft