Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPClient vs Socket in C#

People also ask

Is TcpClient a socket?

TcpClient creates a Socket to send and receive data over a network. Classes deriving from TcpClient can use this property to get or set this Socket . Use the underlying Socket returned from Client if you require access beyond that which TcpClient provides.

What is a TcpClient?

The TcpClient class provides simple methods for connecting, sending, and receiving stream data over a network in synchronous blocking mode. In order for TcpClient to connect and exchange data, a TcpListener or Socket created with the TCP ProtocolType must be listening for incoming connection requests.

What is difference between socket and TCP?

Socket connection is used for continues exchange of data between nodes (it creates a session between them) but TCP connection makes a reliable transmission of data segments between nodes.

Is socket communication fast?

Sockets are faster than web services in general. However, on this last point as you're on a slow, unstable wifi connection it is not bad to reconnect for each request.


The use of TcpClient and TcpListener just means a few less lines of code. As you say it's just a wrapper over the Socket class so there is no performance difference between them it's purely a style choice.

Update: Since this answer was posted the .Net source code has become available. It does indeed show that TcpClient is a very light wrapper over the Socket class which is itself a wrapper on top of the native WinSock2 API*.

  • On Windows. Will be different for .Net Standard/Core etc. on other platforms.

Also, you can access the socket directly from the TCPClient object, it's under the property Client - so there is no performance difference.