Distributed .NET: Remoting with TCP Channels
With the release of the .NET Framework 1.0, Microsoft has given us a powerful alternative to DCOM: .NET Remoting. While everyone is talking about Web Services (SOAP/HTTP), the real performance junkies know that for internal distributed systems, binary serialization over a raw TCP channel is where it's at.
Defining the Remote Object
The object you want to share must inherit from MarshalByRefObject. This tells the .NET runtime that the object stays on the server, and the client gets a "transparent proxy."
public class RemoteCalculator : MarshalByRefObject {
public int Add(int a, int b) {
Console.WriteLine("Calculation requested: {0} + {1}", a, b);
return a + b;
}
}
Setting up the Server
We need to register a TcpChannel and then register our service. We'll use WellKnownObjectMode.Singleton so all clients talk to the same instance.
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
public class Server {
public static void Main() {
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(RemoteCalculator),
"CalculatorService",
WellKnownObjectMode.Singleton
);
Console.WriteLine("Server started. Press Enter to exit...");
Console.ReadLine();
}
}
The Client Side
The client just needs to connect to the URL and cast the resulting proxy.
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel);
RemoteCalculator calc = (RemoteCalculator)Activator.GetObject(
typeof(RemoteCalculator),
"tcp://localhost:8080/CalculatorService"
);
int result = calc.Add(10, 20);
Console.WriteLine("Result: " + result);
Why Binary over TCP?
SOAP/HTTP is great for interoperability, but parsing XML is slow and the HTTP protocol adds significant header overhead. By using the BinaryFormatter and raw TCP, we reduce the payload size by up to 70% and eliminate the latency of a web server like IIS. If you're building a high-frequency trading platform or a heavy-duty back-office system on Windows, Remoting is your best friend.
Just be careful with the firewall-port 8080 isn't always open!
Aunimeda builds production-grade backend systems - APIs, microservices, real-time applications, and system integrations.
Contact us for backend engineering services. See also: Custom Software Development, Web Development