In a peer-to-peer application, clients communicate over a network (or the network of networks, the Internet). You can select the protocol used for communication and the encoding used for messages. Most other models for distributed applications aren't nearly as flexible.
One approach for peer-to-peer communication in the .NET world is Remoting, a high-level abstraction that wraps networking code. Remoting is an attractive choice for communication in a peer-to-peer system because it's flexible, reliable, and easy to configure. With Remoting, the common language runtime (CLR) takes care of basic infrastructure chores such as releasing unneeded objects, creating and closing connections, and managing simultaneous requests with a pool of threads. Remoting also has some limitations—namely, because of the way it's designed, it works better for brokered communication with a coordination server than for decentralized peer-to-peer applications.
In this chapter, you'll learn all the Remoting basics that you need to create a peer-to-peer application such as the Messenger program presented in the next two chapters. You'll learn how objects communicate out-of-process, how to serialize data that must be sent across the network, and how to handle concurrent access. You'll also learn the ins and outs of some trickier aspects of Remoting, such as bidirectional communication, callbacks and events, and object lifetime. You'll also see why Remoting code is used differently in a peer-to-peer application than in a typical .NET enterprise system.
But before we begin, it helps to take a broad look at what Remoting is, how it fits into the grand scheme of distributed application technologies, and what its advantages and shortcomings are.