AddressFamily Enumeration

System.Net.Sockets

Specifies the address family used by the Socket class.

Description

The AddressFamily enumeration defines the address families supported by the Socket class. These families determine the type of network addresses used for communication.

Members

Syntax

public enum AddressFamily

Remarks

When you create a Socket, you must specify an AddressFamily to indicate the network protocol to use. For example, to create a socket for IPv4 communication, you would use AddressFamily.InterNetwork. For IPv6, you would use AddressFamily.InterNetworkV6.

Many of the values in this enumeration are marked as deprecated. It is recommended to use AddressFamily.InterNetwork for IPv4 and AddressFamily.InterNetworkV6 for IPv6.

Examples

Creating an IPv4 TCP Socket

using System; using System.Net; using System.Net.Sockets; public class Example { public static void Main() { // Create a TCP socket for IPv4. Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Console.WriteLine($"Socket created with Address Family: {socket.AddressFamily}"); // Remember to close the socket when done. socket.Close(); } }

Creating an IPv6 UDP Socket

using System; using System.Net; using System.Net.Sockets; public class Example { public static void Main() { // Create a UDP socket for IPv6. Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); Console.WriteLine($"Socket created with Address Family: {socket.AddressFamily}"); // Remember to close the socket when done. socket.Close(); } }

Fields

Name Description
Unknown The address family is unknown.
InterNetwork Unspecified. Represents the IPv4 address family.
InterNetworkV6 Unspecified. Represents the IPv6 address family.
Max Used to specify the maximum number of address families that are supported.
NS Deprecated. Represents the Xerox Network Systems (XNS) address family.
OSI Deprecated. Represents the Open Systems Interconnection (OSI) address family.
Unix Deprecated. Represents the Unix domain socket address family.
Deprecated. Represents the implementation-specific link layer address family.
SNS_IP Deprecated. Represents the SNS IP address family.
IPX Deprecated. Represents the Novell NetWare IPX address family.
Sna Deprecated. Represents the IBM Systems Network Architecture (SNA) address family.
DecNet Deprecated. Represents the Digital Equipment Corporation (DEC) networking protocol address family.
Deprecated. Represents the data link layer address family.