System.Net.Sockets

Namespace: System.Net.Sockets

Contains classes that provide low-level access to network sockets.

These classes allow you to implement custom network protocols or work with existing ones at a fundamental level.

Classes

🌐

Socket

public class Socket : IDisposable

Provides the `Socket` class, which represents a network endpoint. This is the core class for sending and receiving data over a network using the Socket API. It supports various protocols like TCP and UDP.

💻

SocketAsyncEventArgs

public class SocketAsyncEventArgs : EventArgs

Represents the state of an asynchronous socket operation. This class is used to pass state information between the caller and the operating system during asynchronous I/O operations.

📧

SocketException

public class SocketException : IOException

Represents errors that occur during socket operations. Provides a ErrorCode property to retrieve the specific Windows Sockets error code.

📡

SocketFlags

public enum SocketFlags

Specifies optional behavior for `Socket` send and receive operations. Common flags include `None`, `Broadcast`, `DontRoute`, `OutOfBand`, `Peek`, `Truncate`.

🔠

SocketOptionLevel

public enum SocketOptionLevel

Specifies socket option levels for socket options. Common levels include `Socket`, `IP`, `Tcp`, and `Udp`.

🎛️

SocketOptionName

public enum SocketOptionName

Specifies socket option names for socket options. Common names include `AcceptConnection`, `Broadcast`, `Debug`, `DontFragment`, `DontRoute`, `Error`, `HeaderIncluded`, `HopLimit`, `KeepAlive`, `Linger`, `OOBInline`, `ReceiveBuffer`, `ReceiveTimeout`, `ReuseAddress`, `SendBuffer`, `SendTimeout`, `Type`.

🔗

SocketShutdown

public enum SocketShutdown

Specifies the direction for terminating a `Socket` connection. Values include `Receive`, `Send`, and `Both`.

Common Scenarios & Usage

Creating and Connecting a TCP Socket


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

// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

// Bind the socket to the local endpoint and listen for incoming connections.
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 11000);
listener.Bind(localEndPoint);
listener.Listen(10);

Console.WriteLine("Waiting for a connection...");
Socket handler = await listener.AcceptAsync();

// ... send/receive data ...

handler.Shutdown(SocketShutdown.Both);
handler.Close();
listener.Close();
            

Sending and Receiving UDP Data


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

// UDP client socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("192.168.1.1"), 11000);

byte[] msg = Encoding.ASCII.GetBytes("Hello UDP!");
socket.SendTo(msg, remoteEP);

byte[] buffer = new byte[1024];
int bytesReceived = socket.ReceiveFrom(buffer, ref remoteEP);

Console.WriteLine($"Received: {Encoding.ASCII.GetString(buffer, 0, bytesReceived)}");
socket.Close();