TLDR;
What are the differences in functionality and intended usage between TcpListener.AcceptSocket(Async)
and TcpListener.AcceptTcpClient(Async)
? Specifically, is NetworkStream
of particular note?
Full detail and background
I'm trying to get to grips with the .NET socket functionality in order to write a server which maintains persistent TCP-IP connections for 2-way communication with multiple clients over a pre-defined protocol.
Initially Google led me to MS' Asynchronous Socket Example, which is heavily featured in SO questions on the topic.
Digging deeper into questions and answers, I saw people recommend using TcpClient
/ TcpListener
over Socket
.
Now TcpListener
has (blocking and asynchronous versions of) two methods: AcceptSocket
and AcceptTcpClient
. MS' documentation briefly suggests the latter is simpler and the former gives more control but doesn't explain why or how. I can see from AcceptTcpClient vs AcceptSocket and TCPClient vs Socket in C# the latter gives access to a NetworkStream
via TcpClient
while the former exposes Send
/ Receive
methods on Socket
but that doesn't really explain why one is preferred.
So what are the differences and how should one choose which to use? Are there use-cases each is designed for? Why do both exist?