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.
Socket
Represents a socket, which is an endpoint for sending or receiving data across a network.
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()
TcpClient
Provides client connections for TCP network services.
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 underlyingSocket.Connected: Gets a value indicating whether the client is connected to a remote host.
Methods
Connect(string hostname, int port)GetStream(): Returns theNetworkStreamused to send and receive data.
TcpListener
Listens for incoming connection requests.
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 aTcpClient.Stop(): Stops listening for incoming connection requests.
UdpClient
Provides UDP network services.
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()
IPAddress
Represents an Internet Protocol (IP) address.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).
EndPoint
Represents an endpoint for a remote or local network connection.The abstract base class for all endpoint representations. An endpoint is a combination of an IP address and a port number.
IPEndPoint
Represents a network endpoint as an IP address and a port number.
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.
SocketException
Represents an error that occurred during a socket operation.This exception is thrown when a socket operation fails. It provides details about the specific Winsock error code encountered.
SocketFlags
Specifies the behavior of socket operations.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
NonePeekDontRouteBroadcastUse>∕MulticastDontFragmentMoreComponentsTruncateOobWaitForAllToCompleteNotificationMaxSockets
SocketOptionLevel
Specifies socket option levels.Defines socket options that can be set or retrieved for a socket.
Members
SocketIPTCPUdp
SocketOptionName
Specifies socket option names.Defines specific socket options that can be set or retrieved within a given option level.
Members
DebugReuseAddressDontLingerLingerBroadcastUse>∕MulticastTimeToLiveMulticastLoopbackOptionBroadcast⟨⟨⟨...⟩⟩⟩DontFragmentDontRouteIPOptionsHeaderIncludedHopLimitHopByHopOptionsStrictRoutingRecvTimeOutSendTimeOutMaxConnectionsIPTimeToLiveTypeErrorSendBufferReceiveBufferKeepAliveOutOfBandInlineExpeditedDataInlineConditionalExpressionBcErrorSndBufLimitRcvBufLimitAuthenticationAcceptFilterConnectionTrackingAddressListExtendedErrorBlockNonTtl10UseLastLocalAddressNonBlockingIoConditionalExpression⟨⟨⟨...⟩⟩⟩ConnectDataConnectExListenExGroupMembershipsNegotiateSocketSecurityRecvAnyAddressNoDelayTcpKeepAliveTcpAckFrequencyTcpAckDelayedTimeTcpKeepIdleTcpKeepIntvlTcpUserTimeoutSocketStateUdpUngracefulTriesTunneledDiscConnectDataNeedDisconnectSetPeerApplicationInfoGetPeerApplicationInfoPortReservedImpersonationReservationPortLingerOnShutdownCongestionControlQueryCongestionControlCustom
SocketAddress
Stores socket address information.
A low-level representation of a socket address, used internally by the
Socket class. It's generally not used directly by application code.
SocketType
Specifies the type of socket.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.
ProtocolType
Specifies the transport protocol.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.IPv6InformationIPv6RoutingHeaderIPv6FragmentHeaderIPv6routingHeaderIPv6HomeAddressIPv6ICMPIPv6NoneIPv6RTHdrUnknown