网络编程低级还是类抽象?

发布于 2024-09-29 14:53:50 字数 131 浏览 5 评论 0原文

我看到很多关于网络编程主题的问题。尽管有所有的问题和答案,我只是不知道哪种方式最好开始。是从最低级别开始更好,还是立即使用 .NET C# 工作而不深入抽象下面的细节?在 Linux 中使用 Winsock 或 BSD Socket 编程哪个更好?

I see a lot of questions on the topic of network programming. Despite all the questions and answers I just do not know which way is best to start. Is it better to start from the lowest level, or immediately to work in .NET C #, without going into details below abstraction? Is it better to go with Winsock or BSD Socket programming in Linux?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睡美人的小仙女 2024-10-06 14:53:50

您仍然可以使用 C# 进行低级 TCP 或 UDP 编程,因此此时您是否要使用 C、C# 等编写网络代码实际上只是一个选择问题...如果您想做的只是学习如何编写网络代码,我会认为该语言更多是个人选择,因为底层网络概念保持不变。

You can still do low-level TCP or UDP programming in C#, so at that point it is really just a matter of choice whether you want to write network code in C, C#, etc... If all you are trying to do is learn how to write network code, I would consider the language more of a personal choice as the underlying network concepts remain the same.

不必在意 2024-10-06 14:53:50

.NET C#

您拥有:

  • 使用 TCP、UDP 等的低级 API。
  • .NET 远程处理
  • WCF(http、tcp、命名管道、MSMQ)

我建议使用最后一个选项,但这取决于您想要学习的具体内容:
构建分布式应用程序或低级套接字 API 的具体细节。

.NET C#

You have:

  • low level API to work with TCP, UDP etc.
  • .NET Remoting
  • WCF (http, tcp, named pipes, MSMQ)

I would recommended last option but depends on what exaclty you are trying to learn:
to build distributed apps or the nitty gritty details of low level socket API.

寄居人 2024-10-06 14:53:50

这完全取决于您现有的编程技能。如果没有对异步编程的基本了解,我不会从较低级别开始,例如 Socket 类(或 TcpClient/UdpClient)。

由于 Read 方法会阻塞,因此许多开始进行套接字编程的人都会启动一个单独的线程来进行读取。这是解决问题的一种非常无效的方法,尤其是在服务器中。 BeingRead/EndRead 是正确的选择。

下一步是设计一种传输协议,因为 TCP 不保证一次传送完整的消息。它仅保证您的消息按正确的顺序到达。

套接字编程的下一件大事是如何处理传入的数据。新手会犯的一个错误是开始附加字符串,这会导致服务器应用程序使用大量内存。使用 byte[] 缓冲区和缓冲池(享元模式)来管理传入数据(如果您制定了精心设计的协议,这应该是一项简单的任务)。

正如您所看到的,对于没有任何经验的人来说,这是一项相当艰巨的任务。 WCF 是一个更好的选择,因为它可以为您处理大部分内容。

It all depends on your existing programming skills. I would not start with the lower levels such as the Socket class (or TcpClient/UdpClient) without having at least basic understanding of asynchronous programming.

A lot of people who starts with socket programming launches a separate thread for the reading since the Read method blocks. It's a very noneffective way to solve the problem, especially in servers. BeingRead/EndRead is the way to go.

Next up is designing a transfer protocol since TCP doesn't guarantee that the complete message is delivered at one. It only guarantees that your messages arrive in the correct order.

The next big thing with socket programming is how to handle incoming data. A newbie mistake is to start appending strings which would result in a lot of memory usage in server applications. Use byte[] buffers and a buffer pool (flyweight pattern) to manage incoming data (should be a easy task if you've made a well designed protocol).

As you can see, it' quite a big task to take on with no prior experience. WCF is a much better option since it handles most of that stuff for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文