联网.NET / C#NET

2023-09-04 01:38:12 作者:一表人渣╯

可能有人请点我朝着正确的方向学习如何做网络在C#/。NET 3.5? code的例子和说明,欢迎选购。基本上,我在寻找如何做异步/多线程的服务器/客户端模式。

Could somebody please point me in the right direction for learning how to do networking in C#/.net 3.5? Code samples and explanations are welcome. Basically I am looking for how to do asynchronous/multithreaded server/client models.

我相当熟悉如何做到这一点在C ++基础用的WinSock但尽管我所有的研究似乎无法掌握这个概念在C#。

I am fairly comfortable with the basics in how to accomplish this in C++ with WinSock but though all of my research cannot seem to grasp this concept in C#.

感谢任何可以提供帮助:)

Thanks for any assistance you can provide :)

推荐答案

如果WCF满足您的需求,这是值得期待的。 ZeroC 和其他替代更高级别的图书馆存在。否则,有几种不同的方式来工作,更接近插座的水平,如果这就是你所需要的。

If WCF meets your needs, it's worth looking at. ZeroC and other alternative higher level libraries exist. Otherwise there are several different ways to work closer to the socket level if that's what you need.

这些提供底层插座周围比较薄的包装。它主要提供了一个流过的插座。您可以使用上的NetworkStream(BeginRead的,等等)的异步方法。我不喜欢这个,因为包装不提供那么多,它往往是多了几分尴尬不是直接使用插座。

These provide a relatively thin wrapper around the underlying sockets. It essentially provides a Stream over the socket. You can use the async methods on the NetworkStream (BeginRead, etc.). I don't like this one as the wrapper doesn't provide that much and it tends to be a little more awkward than using the socket directly.

这提供了经典的选择技术复用多个插座IO到一个单独的线程。不推荐下去了。

This provides the classic Select technique for multiplexing multiple socket IO onto a single thread. Not recommended any longer.

异步编程模型(又名IAsyncResult的,开始/结束样式)的插座是主要技术使用套接字异步。有几个变种。从本质上讲,你调用一个异步方法(例如,BeginReceive),并执行下列操作之一:

The Asynchronous Programming Model (AKA IAsyncResult, Begin/End Style) for sockets is the primary technique for using sockets asynchronously. And there are several variants. Essentially, you call an async method (e.g., BeginReceive) and do one of the following:

在民意调查上返回的IAsyncResult(很少使用)完成。 使用的WaitHandle从IAsyncResult的等待方法来完成。 传递的BeginXXX方法的方法完成时将要执行的回调方法。

的最佳方法是#3,因为它是最方便的。如果有疑问,可使用此方法。

The best way is #3 as it is the usually the most convenient. When in doubt, use this method.

某些链接:

MSDN杂志文章上接 上的异步编程模型的杰弗里里氏文章 MSDN Magazine Article on Sockets A Jeffery Richter Article on the Asynchronous Programming Model

.NET 3.5引入了异步套接字的新模式,它使用的事件。它采用了简化异步模式(例如,Socket.SendAsync)。给人一种回调相反,你订阅的​​,而不是一个IAsyncResult事件的完成和,你得到的SocketAsyncEventArgs。我们的想法是,你可以重用的SocketAsyncEventArgs和$ P $套接字IO对分配内存。在高性能的情况下,这可能是非常有效的使用APM风格。此外,如果你做pre-分配内存,你会得到一个稳定的内存占用量,减少了垃圾收集,从钉住等存储器孔需要注意的是担心这个应该只在最高效的方案是一个考虑因素。

.NET 3.5 introduced a new model for async sockets that uses events. It uses the "simplified" async model (e.g., Socket.SendAsync). Instead of giving a callback, you subscribe to an event for completion and instead of an IAsyncResult, you get SocketAsyncEventArgs. The idea is that you can reuse the SocketAsyncEventArgs and pre-allocate memory for socket IO. In high performance scenarios this can be much more efficient that using the APM style. In addition, if you do pre-allocate the memory, you get a stable memory footprint, reduced garbage collection, memory holes from pinning etc. Note that worrying about this should only be a consideration in the most high performance scenarios.

MSDN杂志:取得连接与.NET Framework 3.5 MSDN信息与技术pre-分配内存 MSDN Magazine: Get Connected With The .NET Framework 3.5 MSDN Information with technique for pre-allocating memory

在大多数情况下使用APM风格的回调方法,除非你preFER中的SocketAsyncEventArgs /异步方法的风格。如果你已经在使用的WinSock CompletionPorts,你应该知道,这两种方法的引擎盖下使用CompletionPorts。

For most cases use the callback method of the APM style unless you prefer the style of the SocketAsyncEventArgs / Async method. If you've used CompletionPorts in WinSock, you should know that both of these methods use CompletionPorts under the hood.

 
精彩推荐
图片推荐