.NET API Documentation

System.Net.Sockets

Provides classes that enable a coarse-grained control over network access, including classes that support socket operations.

Namespace: System.Net

Assembly: System.Net.Sockets.dll

Classes

  • AddressFamily Enumeration that specifies the base address structures used by the Socket class.
  • IPAddress Represents a Transmission Control Protocol/Internet Protocol (IP) address.
  • IPEndPoint Represents a network endpoint as an IP address and a port number.
  • NetworkStream Provides the underlying stream of bytes for network access.
  • ProtocolFamily Enumeration that specifies the protocols used by the Socket class.
  • ProtocolType Enumeration that specifies Internet protocol types.
  • Socket Provides a highly flexible and extensible set of classes for network programming.
  • SocketAddress Represents a socket address.
  • SocketError Enumeration that describes socket errors.
  • SocketFlags Enumeration that specifies socket options.
  • SocketOptionLevel Enumeration that specifies socket option levels.
  • SocketOptionName Enumeration that specifies socket option names.
  • SocketType Enumeration that specifies the types of sockets available.
  • TcpClient Provides a stream-based TCP connection.
  • UdpClient Provides UDP network services.

Structures

Interfaces

Examples

Creating a simple TCP Listener

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

public class SimpleTcpListener
{
    public static void StartListening()
    {
        // Set the IP address and port number to listen on.
        IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
        int port = 11000;
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

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

        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(10); // Start listening for connections.

            Console.WriteLine("Waiting for a connection...");
            Socket handler = listener.Accept(); // Program is suspended while waiting for connection.

            // Incoming data from the client.
            string data = null;
            byte[] bytes = new byte[1024];
            int bytesRec = handler.Receive(bytes);
            data = Encoding.ASCII.GetString(bytes, 0, bytesRec);

            Console.WriteLine($"Text received : {data}");

            // Echo the data back to the client.
            byte[] msg = Encoding.ASCII.GetBytes($"Echo: {data}");
            handler.Send(msg);

            handler.Shutdown(SocketShutdown.Both);
            handler.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            if (listener != null)
            {
                listener.Close();
            }
        }
    }

    // You would call StartListening() from your Main method or another entry point.
    // public static void Main(string[] args) { StartListening(); }
}