System.Net.Sockets.AddressFamily

AddressFamily Enum

Specifies the address family for a socket.

Syntax

public enum AddressFamily

Remarks

The AddressFamily enumeration is used to specify the addressing scheme that the socket uses. For example, you can specify that the socket uses the Internet Protocol version 4 (IPv4) or Internet Protocol version 6 (IPv6).

The Socket class constructor takes an AddressFamily parameter to initialize the socket.

Note: When you use AddressFamily.InterNetwork, you are using IPv4. When you use AddressFamily.InterNetworkV6, you are using IPv6.

Members

Unknown The address family is unknown.
Unspecified The address family is unspecified.
Unix The UNIX domain socket family.
InterNetwork Internet Protocol version 4 (IPv4).
ImpLink ARPANET IMP address family.
Pup PARC Universal Packet (PUP) protocol address family.
Chains CHAOSnet protocol address family.
NetBios NetBIOS protocol address family.
AppleTalk AppleTalk protocol address family.
DecNet DECnet protocol address family.
DataKit Datakit protocol address family.
ccitt CCITT protocols address family.
Sna IBM SNA protocols address family.
IPX Novell IPX/SPX protocol address family.
Unix UNIX domain socket family.
NetDragon NDN protocols address family.
BAN BAN protocol address family.
Atm Asynchronous Transfer Mode (ATM) protocol address family.
Oam OAM protocol address family.
X25 X.25 protocol address family.
Isdn ISDN protocol address family.
Rrcp RC2000 protocol address family.
Infiniband InfiniBand protocol address family.
InterNetworkV6 Internet Protocol version 6 (IPv6).
Ecma ECMA protocol address family.
DataLink Data link protocol address family.
SMC SMC protocol address family.

Example Usage

Here's how you might use AddressFamily.InterNetwork when creating a socket:

using System;
using System.Net;
using System.Net.Sockets;

public class SocketExample
{
    public static void Main(string[] args)
    {
        try
        {
            // Create a TCP socket for IPv4
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Console.WriteLine("TCP socket created successfully for IPv4.");

            // You can then bind the socket, connect, or listen, etc.
            // Example: Binding to a local endpoint
            // IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
            // IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11111);
            // socket.Bind(localEndPoint);
            // Console.WriteLine("Socket bound to " + localEndPoint.ToString());

            socket.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }
}

See Also