networking in .net/C#

前端 未结 3 1828
青春惊慌失措
青春惊慌失措 2021-02-03 16:12

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

相关标签:
3条回答
  • 2021-02-03 16:32

    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.

    TcpClient/UdpClient

    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.

    Socket - Select

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

    Socket - APM Style

    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:

    1. Poll for completion on the returned IAsyncResult (hardly used).
    2. Use the WaitHandle from the IAsyncResult to wait for the method to complete.
    3. Pass the BeginXXX method a callback method that will be executed when the method completes.

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

    Some links:

    • MSDN Magazine Article on Sockets
    • A Jeffery Richter Article on the Asynchronous Programming Model

    .NET 3.5 High Performance Sockets

    .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 Magazine: Get Connected With The .NET Framework 3.5
    • MSDN Information with technique for pre-allocating memory

    Summary

    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.

    0 讨论(0)
  • 2021-02-03 16:50

    In .NET 3.5 world you should definitely learn Windows Communication Foundation - .NET framework for networking.

    Useful link:

    Synchronous and Asynchronous Operations (in WCF)

    You can find useful WCF information on StackOverflow browsing posts tagged with WCF tag

    0 讨论(0)
  • 2021-02-03 16:50

    It depends on what you want to focus on.

    If you want to focus on functionality and leave the plumbing to the framework, then start with Windows Communication Foundation.

    If you're looking to build your own plumbing, then use System.Net.Sockets.Socket class.

    0 讨论(0)
提交回复
热议问题