什么是使用的TcpClient直接上一个Socket的好处? [英] What are the benefits of using TcpClient over a Socket directly?

查看:331
本文介绍了什么是使用的TcpClient直接上一个Socket的好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,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 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

这篇关于什么是使用的TcpClient直接上一个Socket的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆