NOTE — This library uses Remoting to implement client-server communication. Remoting is not supported in .NET Core and later. If you need to perform message broadcasting or exchange on all .NET platforms, consider using the MixedComms library created in February 2023 which contains simple classes that wrap standard cross-platform techniques for communication across threads, processes and machines.
Overview
The LiteComms library contains two lightweight classes that allow client-server communication, either on the same machine or between different machines on a local area network. The library is intended to be as simple to use as possible and to have a tiny runtime footprint.
The client and server classes communicate using a very simple protocol implemented as a single server method that takes a string request and returns a string response. Although this protocol is simple, applications are free to exchange strings between the client and server in any convenient or suitable format they choose. Exchanging strings of XML for example is an easy way to create an extensible general purpose contract between client and server.
The LiteComms library is intended for use where network communications between application components must be implemented simply and easily, with no configuration and no complicated dependencies. The library internally uses a TCP/IP Remoting channel for communication.
Server
The application component that is acting as a Lite server creates an instance of a LiteServer
class and calls its Start
and Stop
methods. A Func<string,string>
callback method must be provided to implement the server logic where an incoming string request is transformed into a string response. Each server communicates using a channel that is identified by a port number and channel name. A client and a server must use the same port number and channel name to allow communication.
The server's callback function runs on worker threads, so it is the hosting application's responsibility to enforce thread safety.
int port = 19876;
string channelName = "Example1";
var server = new LiteServer();
:
server.Start(port, channelName, (arg) =>
{
// This is the callback where the incoming string
// request is transformed into a string response.
// Remember that this callback function runs on
// a worker thread.
return string.Format("This is a response to: {0}", arg);
});
:
server.Stop();
Client
The application component that is acting as a Lite client creates an instance of a LiteClient
class and calls its Send
method to send a string request to a server and receive a string response. The constructor requires the DNS name or IP address of the server that it will communicate with, as well as the port number and channel name that the server is using.
Network communication may fail in unpredictable ways, so clients should prepare to catch errors when sending requests to a server.
string serverAddress = "192.168.1.20";
int port = 19876;
string channelName = "Example1";
:
var client = new LiteClient(serverAddress, port, channelName);
:
// You should try-catch around this call
string response = client.Send("Please respond to this!");
Trace("The response is: {0}", response);
:
client.Dispose();
The client class also provides a SendAsync
method which returns a Task<string>
so callers can retrieve the server response asynchronously. This method is convenient in Forms or WPF applications that must not block while waiting for a response from the server.
Code documentation generated by Sandcastle Help File Builder can be found here:
📘 https://orthoprog.blob.core.windows.net/dochelp/litecomms/index.html