Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits of using TcpClient over a Socket directly?

Tags:

I understand that a TcpClient is a wrapper around the socket class, and I can access the underlying socket if using the TcpClient, but what exactly does the wrapper do?

When using the TCPClient do i need to keep calling Receive() like I do with a socket or does the wrapper ensure all my data appears?

Lastly, can I use the TcpClient on both the server and the client to wrap the socket (after using TcpListener to accept the original connection on the server)

like image 362
Dermot Avatar asked Mar 04 '12 06:03

Dermot


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 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 socket programming in C#?

Socket programming is a way of connecting two nodes on a network to communicate with each other. Basically, it is a one-way Client and Server setup where a Client connects, sends messages to the server and the server shows them using socket connection.

How do I connect to a TCP Client?

Connects the client to a remote TCP host using the specified remote network endpoint. Connects the client to a remote TCP host using the specified IP address and port number. Connects the client to a remote TCP host using the specified IP addresses and port number.


1 Answers

what exactly does the wrapper do?

Let me explain this with an example. You have a method in C# File.ReadAllLines. It reads all lines in the file for you. Now you can also achieve same results via FileStream class or any other class which reads file .. BUT.. wrapper i.e. File.ReadAllLines, allows you to achieve the same with less lines of code. Wrappers always increase productivity by abstracting out the low level details

When using the TCPClient do i need to keep calling Receive() like I do with a socket or does the wrapper ensure all my data appears?

TCPClient don't have a Receive method like Socket but the idea is same. You will have to use methods like GetStream to read the data it won't automagically appear for you

Can I use the TcpClient on both the server and the client to wrap the socket

Yes, you can safely use it on both client and server side

like image 177
Haris Hasan Avatar answered Nov 07 '22 00:11

Haris Hasan