Networking: TcpClient vs. Socket

  • Thread starter Thread starter Laurent
  • Start date Start date
L

Laurent

Hi,
I am new to C# and .NET.

On a TcpListener I can choose between AcceptSocket and AcceptTcpClient.
These 2 classes look very similar:
* on TcpClient you call GetStream (and Read and Write the NetworkStream)
* on Socket you can Send and Receive directly

Which is best?

Thanks.
 
Hi,

Socket is the raw option, all operations use byte buffers, and the
constructor uses an IPEndPoint.

The TCPClient hides all these low level details. The contstructor takes a
string that can either represent an IP address or more readable address e.g
www.microsoft.com As you observed TCPClient gives you a Stream class, this
can be fed into other .NET classes, e.g. XMLReader.

The TCPClient is a convenience class, if all you wan't to do is to set up a
TCP connection, use it. If you require more control over your connection use
the Socket.


Chris
 
Back
Top