Socket options in C#

Socket options determine the behavior of an instance of the Socket class. Some socket options apply only to specific protocols while others apply to all types. Members of the SocketOptionLevel enumeration specify which protocol applies to a specific socket option.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SocketOptionNameSample
{
    public static void Main()
    {
        IPAddress ip = IPAddress.Parse("127.0.0.1");
        IPEndPoint ep = new IPEndPoint(ip, 9999);
        Socket skt = new Socket(AddressFamily.InterNetwork,
        SocketType.Dgram, ProtocolType.Udp);
        skt.SetSocketOption(SocketOptionLevel.Socket,
        SocketOptionName.SendTimeout, 5000);
        try
        {
            Byte[] req = Encoding.ASCII.GetBytes("Test");
            skt.SendTo(req, ep);
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
        }
        finally
        {
            skt.Close();
        }
    }
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.