AddressFamily Enum

Namespace: System.Net.Sockets
Assembly: System.Net.Primitives (in System.Net.Primitives.dll)

Specifies the protocol family for which the Socket is created.

Syntax


public enum AddressFamily
{
    // ... members ...
}
            

Members

InterNetwork

2 - Internetwork version 4 (IPv4) address.

InterNetworkV6

234 - Internetwork version 6 (IPv6) address.

AppleTalk

16 - AppleTalk address.

Banyan

21 - Novell NetWare, Banyan, or other network protocol.

DecNet

12 - DECnet address.

Ecma

8 - European Computer Manufacturers Association (ECMA) address.

Ipx

4 - IPX address.

Iso

1 - International Organization for Standardization (ISO) address.

NetBios

17 - NetBIOS address.

NS

6 - Xerox Network Systems (XNS) address.

Oda

13 - Open Data Applications (ODA) address.

Sna

11 - Systems Network Architecture (SNA) address.

Unix

1 - UNIX local identifiers.

Example

The following C# code demonstrates how to create a TCP/IP socket using the AddressFamily.InterNetwork.


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

public class SocketExample
{
    public static void Main(string[] args)
    {
        try
        {
            // Create a TCP/IP socket.
            // AddressFamily.InterNetwork specifies IPv4.
            // SocketType.Stream specifies a stream-based socket (TCP).
            // ProtocolType.Tcp specifies the TCP protocol.
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            Console.WriteLine("TCP/IP socket created successfully for IPv4.");
            Console.WriteLine($"Socket Type: {socket.SocketType}");
            Console.WriteLine($"Protocol Type: {socket.ProtocolType}");
            Console.WriteLine($"Address Family: {socket.AddressFamily}");

            // You would typically bind, listen, or connect here.
            // For demonstration, we'll just close it.
            socket.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}
                

Notes

When creating a Socket, the AddressFamily parameter determines the addressing scheme to be used. AddressFamily.InterNetwork is commonly used for IPv4 communication, while AddressFamily.InterNetworkV6 is used for IPv6. Choosing the correct AddressFamily is crucial for establishing network connections with the desired protocol.