System.Net.SocketAddress Class

Assembly: System.dll
Namespace: System.Net
Summary
Stores serialized information from EndPoint derived classes.
C# Syntax:
public class SocketAddress
Remarks
SocketAddress can be used to store a serialized IPEndPoint. The first byte of the underlying buffer is reserved for the AddressFamily enumerated value. The second byte is reserved for the size of the buffer. The third and fourth bytes are reserved for storing 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 SocketAddress.Family and SocketAddress.Size properties to get the AddressFamily value and the buffer size, respectively. To view any of this information as a string, use the SocketAddress.ToString method.
Example
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.

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


    
See also:
System.Net Namespace

System.Net.SocketAddress Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(AddressFamily family)

Creates a new instance of the SocketAddress class for the given address family.
ctor #2 Overloaded:
.ctor(AddressFamily family, int size)

Creates a new instance of the SocketAddress class using the specified address family and buffer size.
Public Properties
Family Read-only

Gets the AddressFamily enumerated value of the current SocketAddress.
Item Read-write

Gets or sets the specified index element in the underlying buffer.
Size Read-only

Gets the underlying buffer size of the SocketAddress.
Public Methods
Equals Overridden:
Determines whether two Object instances are equal.
GetHashCode Overridden:
Serves as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString Overridden:
Returns information about the socket address.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Net.SocketAddress Member Details

Overloaded ctor #1
Summary
Creates a new instance of the SocketAddress class for the given address family.
C# Syntax:
public SocketAddress(
   AddressFamily family
);
Parameters:

family

An AddressFamily enumerated value.

Remarks
This overload sets the underlying buffer size to 32 bytes.

Return to top


Overloaded ctor #2
Summary
Creates a new instance of the SocketAddress class using the specified address family and buffer size.
C# Syntax:
public SocketAddress(
   AddressFamily family,
   int size
);
Parameters:

family

An AddressFamily enumerated value.

size

The number of bytes to allocate for the underlying buffer.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException The size parameter is less than 2. These two bytes are needed to store family and size.
Remarks
Use this overload to create a new instance of the SocketAddress class with a particular underlying buffer size.

Return to top


Property: Family (read-only)
Summary
Gets the AddressFamily enumerated value of the current SocketAddress.
C# Syntax:
public AddressFamily Family {get;}
Remarks
This method gets the AddressFamily enumerated value representing the addressing scheme of the current SocketAddress. If you want to view the corresponding string representation of AddressFamily, use the SocketAddress.ToString method.
See also:
AddressFamily

Return to top


Property: Item (read-write)
Summary
Gets or sets the specified index element in the underlying buffer.
C# Syntax:
public byte this[int offset] {get; set;}
Parameters:

offset

The array index element of the desired information

Exceptions
Exception Type Condition
IndexOutOfRangeException The specified index does not exist in the buffer
Remarks
This property gets or sets the specified byte position in the underlying buffer. The first byte of the underlying buffer is reserved for the AddressFamily enumerated value. The second byte is reserved for the size of the buffer. The third and fourth bytes are reserved for storing port number information. The next bytes are used to store the address.

Note Be sure to call SocketAddress.Size before referring to elements in the underlying buffer. Referring to an index that does not exist will cause the SocketAddress to throw an IndexOutOfRangeException.
See also:
SocketAddress.Size

Return to top


Property: Size (read-only)
Summary
Gets the underlying buffer size of the SocketAddress.
C# Syntax:
public int Size {get;}
Remarks
This property gets the underlying buffer size of the SocketAddress in bytes.

Return to top


Overridden Method: Equals(
   object comparand
)
Summary
Determines whether two Object instances are equal.
C# Syntax:
public override bool Equals(
   object comparand
);
Parameters:

comparand

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~SocketAddress();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: GetHashCode()
Summary
Serves as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table.
C# Syntax:
public override int GetHashCode();

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: ToString()
Summary
Returns information about the socket address.
C# Syntax:
public override string ToString();
Return Value:
A string containing information about the SocketAddress.
Remarks
The SocketAddress.ToString method returns a string containing the AddressFamily enumerated value, the size of the underlying buffer of the SocketAddress structure, and the remaining contents of the buffer.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.