Overview

The Socket class provides a rich set of methods and properties for network communication using the Berkeley sockets interface. It supports both synchronous and asynchronous operation, multiple protocols (TCP, UDP, etc.), and various socket options.

Creating a Socket

Instantiate a Socket by specifying the address family, socket type, and protocol type.

var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

Connecting to a Remote Endpoint

Use Connect for synchronous connections or ConnectAsync for asynchronous.

socket.Connect(IPAddress.Parse("203.0.113.10"), 8080);

Sending and Receiving Data

Data transmission can be performed with Send/Receive (blocking) or their async counterparts.

byte[] buffer = Encoding.UTF8.GetBytes("Hello, Server!");
socket.Send(buffer);
int received = socket.Receive(buffer);
string response = Encoding.UTF8.GetString(buffer, 0, received);

Asynchronous Pattern (Task‑based)

using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

async Task RunAsync()
{
    using var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    await s.ConnectAsync("example.com", 80);
    var request = Encoding.UTF8.GetBytes("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n");
    await s.SendAsync(request, SocketFlags.None);
    var buffer = new byte[4096];
    int received = await s.ReceiveAsync(buffer, SocketFlags.None);
    Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, received));
}
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading.Tasks

Async Function RunAsync() As Task
    Using s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Await s.ConnectAsync("example.com", 80)
        Dim request = Encoding.UTF8.GetBytes("GET / HTTP/1.1" & vbCrLf & "Host: example.com" & vbCrLf & vbCrLf)
        Await s.SendAsync(request, SocketFlags.None)
        Dim buffer(4095) As Byte
        Dim received = Await s.ReceiveAsync(buffer, SocketFlags.None)
        Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, received))
    End Using
End Function

Complete Example

A simple TCP client that connects, sends a message, receives a reply, and closes the socket.

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

class TcpClientDemo
{
    static void Main()
    {
        var endpoint = new IPEndPoint(IPAddress.Loopback, 5000);
        using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        
        Console.WriteLine("Connecting...");
        socket.Connect(endpoint);
        Console.WriteLine("Connected.");

        string message = "Hello Server!";
        byte[] data = Encoding.UTF8.GetBytes(message);
        socket.Send(data);
        Console.WriteLine($"\u003e Sent: {message}");

        byte[] buffer = new byte[1024];
        int bytesRead = socket.Receive(buffer);
        string response = Encoding.UTF8.GetString(buffer, 0, bytesRead);
        Console.WriteLine($"\u003c Received: {response}");

        socket.Shutdown(SocketShutdown.Both);
        socket.Close();
    }
}