The ProtocolType enumeration defines a set of network protocols. You use these values with the Socket constructor to specify the protocol that the Socket will use.
public enum ProtocolType
The following example demonstrates how to create a Socket that uses UDP.
using System;
using System.Net;
using System.Net.Sockets;
public class SocketExample
{
public static void Main(string[] args)
{
// Create a Socket that uses UDP and IPv4.
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Console.WriteLine("UDP Socket created successfully.");
// You would typically bind or connect the socket here.
// For demonstration, we'll just close it.
socket.Close();
}
}
When creating a Socket, you must specify the address family, socket type, and protocol type. For example, to create a TCP socket for IPv4, you would use:
Socket tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
This enumeration provides a convenient way to select the desired protocol without having to know its underlying integer value.