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.
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 :)
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.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
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:
The best way is #3 as it is the usually the most convenient. When in doubt, use this method.
Some links:
.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.
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.