TCP in .NET

To use the Transmission Control Protocol (TCP) services in .NET, use the TcpClient and TcpListener classes. These protocol classes are built on top of the System.Net.Sockets.Socket class and take care of the details of transferring data.

The protocol classes use the underlying Socket class to provide simple access to network services without the overhead of maintaining state information or knowing the details of setting up protocol-specific sockets. To use asynchronous Socket methods, you can use the asynchronous methods supplied by the NetworkStream class. To access features of the Socket class not exposed by the protocol classes, you must use the Socket class.

TcpClient and TcpListener represent the network using the NetworkStream class. You use the GetStream method to return the network stream, and then call the stream's NetworkStream.ReadAsync and NetworkStream.WriteAsync methods. The NetworkStream does not own the protocol classes' underlying socket, so closing it does not affect the socket.

See also