什么是使用的TcpClient直接上一个Socket的好处? [英] What are the benefits of using TcpClient over a Socket directly?
问题描述
据我所知,TcpClient的是周围的插座类的包装,如果使用的TcpClient我可以访问底层套接字,但究竟是什么包装呢?
在使用的TcpClient我需要不断的打电话接收()像我做一个插座还是包装保证我的数据会出现?
最后,我可以使用服务器和客户端包裹插座
究竟包装做什么?
块引用>
让我用一个例子来说明这一点。你必须在C#
File.ReadAllLines
的方法。它会读取你的文件中的所有行。现在,你也可以通过实现的FileStream相同的结果
类或任何其它类读取文件..但是..包装即File.ReadAllLines
,让你实现用较少的代码行相同。包装总是被抽象出来的底层细节提高生产率
在使用的TcpClient我需要不断的打电话接收()像我这样做
,其中A座还是包装确保我的所有数据将出现?
块引用>
的TcpClient
没有接收
方法如插座
但这个想法是一样的。你将不得不使用类似的方法GetStream
阅读也不会出现自动的为你
$ B数据$ b我可以使用TcpClient的服务器和客户端上包裹
座
块引用>
是的,你可以放心地使用它在客户端和服务器端
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)
解决方案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 viaFileStream
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 detailsWhen 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 aReceive
method likeSocket
but the idea is same. You will have to use methods likeGetStream
to read the data it won't automagically appear for youCan 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
这篇关于什么是使用的TcpClient直接上一个Socket的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!