C# 中的 TCPClient 与 Socket [英] TCPClient vs Socket in C#

查看:37
本文介绍了C# 中的 TCPClient 与 Socket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看TCPClient的使用不多,但是Socket却有很多?它们之间的主要区别是什么?您何时会使用它们?

I don't see much use of TCPClient, yet there is a lot of Socket? What is the major difference between them and when would you use each?

我知道 .NET Socket 是写在 WINSOCK 之上的,而 TCPClient 是对 Socket 类的封装.因此 TCPClient 是链的上游,并且可能效率低下.如果我错了,请纠正我.

I understand that .NET Socket is written on top of WINSOCK, and TCPClient is a wrapper over Socket class. Thus TCPClient is way up the chain, and possibly inefficient. Correct me if I am wrong.

推荐答案

使用 TcpClient 和 TcpListener 只是意味着少了几行代码.正如您所说,它只是 Socket 类的包装器,因此它们之间没有性能差异,这纯粹是一种风格选择.

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.

更新:自从发布此答案以来,.Net 源代码已经可用.它确实表明 TcpClient 是一个非常轻的Socket 类的包装器,它本身就是一个包装器在本机 WinSock2 API* 之上.

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*.

  • 在 Windows 上.其他平台上的 .Net Standard/Core 等会有所不同.

这篇关于C# 中的 TCPClient 与 Socket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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