Namespace: System.Net.Sockets

This namespace provides low-level access to the Windows Sockets API (Winsock), allowing you to implement custom network protocols. It includes classes for creating and managing sockets, handling network addresses, and performing send and receive operations.

System.Net.Sockets

Socket

Represents a socket, which is an endpoint for sending or receiving data across a network.
public class Socket : System.IDisposable

The Socket class provides the core functionality for network communication. You can create sockets for different protocols (TCP, UDP) and address families (IPv4, IPv6). It handles low-level network operations like binding, connecting, listening, sending, and receiving.

Constructors

  • Socket(SocketType socketType, ProtocolType protocolType)
  • Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)

Methods

  • Bind(EndPoint localEP)
  • Connect(EndPoint remoteEP)
  • Listen(int backlog)
  • Accept()
  • Send(byte[] buffer, int offset, int size, SocketFlags socketFlags)
  • Receive(byte[] buffer, int offset, int size, SocketFlags socketFlags)
  • Close()
System.Net.Sockets

TcpClient

Provides client connections for TCP network services.
public class TcpClient : System.IDisposable

TcpClient offers a higher-level abstraction over the Socket class for establishing TCP connections. It simplifies client-side operations like connecting to a server and obtaining streams for sending and receiving data.

Constructors

  • TcpClient()
  • TcpClient(string hostname, int port)

Properties

  • Client: Gets or sets the underlying Socket.
  • Connected: Gets a value indicating whether the client is connected to a remote host.

Methods

  • Connect(string hostname, int port)
  • GetStream(): Returns the NetworkStream used to send and receive data.
System.Net.Sockets

TcpListener

Listens for incoming connection requests.
public class TcpListener : System.IDisposable

TcpListener is used by servers to listen for incoming TCP connection requests. It binds to a local endpoint and waits for clients to connect. Once a connection is accepted, it returns a Socket object representing the connection.

Constructors

  • TcpListener(IPEndPoint localEP)
  • TcpListener(IPAddress localaddr, int port)

Methods

  • Start(): Starts listening for incoming connection requests.
  • AcceptSocket(): Accepts an incoming connection request.
  • AcceptTcpClient(): Accepts an incoming connection request and returns a TcpClient.
  • Stop(): Stops listening for incoming connection requests.
System.Net.Sockets

UdpClient

Provides UDP network services.
public class UdpClient : System.IDisposable

UdpClient is used for sending and receiving UDP datagrams. UDP is a connectionless protocol, making it suitable for applications where reliability is less critical than speed, or where the application handles reliability itself.

Constructors

  • UdpClient()
  • UdpClient(int port)
  • UdpClient(IPEndPoint localEP)

Methods

  • Send(byte[] dgram, int bytes, IPEndPoint remoteEP)
  • Receive(ref IPEndPoint remoteEP)
  • Connect(IPEndPoint remoteEP)
  • Close()
System.Net.Sockets

IPAddress

Represents an Internet Protocol (IP) address.
public sealed class IPAddress

This class represents an IP address (either IPv4 or IPv6). It provides methods for parsing IP address strings and for determining the type of IP address.

Properties

  • AddressFamily: Gets the IP address family.
  • IsIPv4MappedToIPv6: Gets a value indicating whether the IP address is an IPv4-mapped IPv6 address.
  • IsLoopback: Gets a value indicating whether the IP address is a loopback address.

Static Methods

  • Parse(string ipString)
  • IsLoopback(IPAddress address)

Constants

  • Loopback: Represents the loopback IP address (127.0.0.1 for IPv4, ::1 for IPv6).
  • Any: Represents the wildcard IP address (0.0.0.0 for IPv4, :: for IPv6).
System.Net.Sockets

EndPoint

Represents an endpoint for a remote or local network connection.
public abstract class EndPoint

The abstract base class for all endpoint representations. An endpoint is a combination of an IP address and a port number.

System.Net.Sockets

IPEndPoint

Represents a network endpoint as an IP address and a port number.
public class IPEndPoint : EndPoint

A concrete implementation of EndPoint for IP-based protocols. It combines an IPAddress with a port number to specify a unique network destination.

Constructors

  • IPEndPoint(long newAddress, int port)
  • IPEndPoint(IPAddress address, int port)

Properties

  • Address: Gets or sets the IP address.
  • Port: Gets or sets the port number.
System.Net.Sockets

SocketException

Represents an error that occurred during a socket operation.
public class SocketException : System.Net.Sockets.SocketError, System.Runtime.Serialization.ISerializable

This exception is thrown when a socket operation fails. It provides details about the specific Winsock error code encountered.

System.Net.Sockets

SocketFlags

Specifies the behavior of socket operations.
public enum SocketFlags

An enumeration that specifies options for socket send and receive operations, such as whether to peek at incoming data or to send data without blocking.

Members

  • None
  • Peek
  • DontRoute
  • Broadcast
  • Use>∕
  • Multicast
  • DontFragment
  • MoreComponents
  • Truncate
  • Oob
  • WaitForAllToComplete
  • Notification
  • MaxSockets
System.Net.Sockets

SocketOptionLevel

Specifies socket option levels.
public enum SocketOptionLevel

Defines socket options that can be set or retrieved for a socket.

Members

  • Socket
  • IP
  • TCP
  • Udp
System.Net.Sockets

SocketOptionName

Specifies socket option names.
public enum SocketOptionName

Defines specific socket options that can be set or retrieved within a given option level.

Members

  • Debug
  • ReuseAddress
  • DontLinger
  • Linger
  • Broadcast
  • Use>∕
  • MulticastTimeToLive
  • MulticastLoopbackOption
  • Broadcast⟨⟨⟨...⟩⟩⟩
  • DontFragment
  • DontRoute
  • IPOptions
  • HeaderIncluded
  • HopLimit
  • HopByHopOptions
  • StrictRouting
  • RecvTimeOut
  • SendTimeOut
  • MaxConnections
  • IPTimeToLive
  • Type
  • Error
  • SendBuffer
  • ReceiveBuffer
  • KeepAlive
  • OutOfBandInline
  • ExpeditedDataInline
  • ConditionalExpression
  • BcError
  • SndBufLimit
  • RcvBufLimit
  • Authentication
  • AcceptFilter
  • ConnectionTracking
  • AddressList
  • ExtendedError
  • BlockNonTtl10
  • UseLastLocalAddress
  • NonBlockingIo
  • ConditionalExpression⟨⟨⟨...⟩⟩⟩
  • ConnectData
  • ConnectEx
  • ListenEx
  • GroupMemberships
  • NegotiateSocketSecurity
  • RecvAnyAddress
  • NoDelay
  • TcpKeepAlive
  • TcpAckFrequency
  • TcpAckDelayedTime
  • TcpKeepIdle
  • TcpKeepIntvl
  • TcpUserTimeout
  • SocketState
  • UdpUngracefulTries
  • Tunneled
  • DiscConnectData
  • NeedDisconnect
  • SetPeerApplicationInfo
  • GetPeerApplicationInfo
  • PortReserved
  • Impersonation
  • ReservationPort
  • LingerOnShutdown
  • CongestionControl
  • QueryCongestionControl
  • Custom
System.Net.Sockets

SocketAddress

Stores socket address information.
public struct SocketAddress

A low-level representation of a socket address, used internally by the Socket class. It's generally not used directly by application code.

System.Net.Sockets

SocketType

Specifies the type of socket.
public enum SocketType

Defines the types of sockets available for communication.

Members

  • Stream: Provides a reliable, two-way, connection-based byte stream.
  • Dgram: Provides a connectionless datagram service.
  • Raw: Provides raw network protocol access.
  • Rdm: Provides a reliable datagram service.
  • SeqPacket: Provides a reliable, two-way, connection-based sequence packet interface.
System.Net.Sockets

ProtocolType

Specifies the transport protocol.
public enum ProtocolType

Defines the transport protocols that can be used for socket communication.

Members

  • IP: Internet Protocol.
  • Icmp: Internet Control Message Protocol.
  • IGMP: Internet Group Management Protocol.
  • GGP: Gateway-to-Gateway Protocol.
  • TCP: Transmission Control Protocol.
  • UDP: User Datagram Protocol.
  • IPv6Information
  • IPv6RoutingHeader
  • IPv6FragmentHeader
  • IPv6routingHeader
  • IPv6HomeAddress
  • IPv6ICMP
  • IPv6None
  • IPv6RTHdr
  • Unknown