Skip to content

Instantly share code, notes, and snippets.

@yallie
Last active August 24, 2018 10:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yallie/483f5773ef2372a3cc493a2b3a43a9e2 to your computer and use it in GitHub Desktop.
Save yallie/483f5773ef2372a3cc493a2b3a43a9e2 to your computer and use it in GitHub Desktop.
Minimal GenuineChannels console application example with programmatic configuration
// Compile using: csc gtcptest.cs /r:GenuineChannels.dll
// First run — start server
// Second run — start client
using System;
using System.Collections.Generic;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using Belikov.GenuineChannels.GenuineTcp;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
try
{
RunServer();
}
catch
{
RunClient();
}
}
private static void RegisterChannel(bool server = false)
{
var props = new Dictionary<string, string>
{
{ "name", "GTCP1" },
{ "priority", "100" }
};
if (server)
{
props["port"] = "8737";
}
var channel = new GenuineTcpChannel(props, null, null);
ChannelServices.RegisterChannel(channel, false);
}
private static void RunServer()
{
RegisterChannel(server: true);
var service = new Service();
var objref = RemotingServices.Marshal(service, "Service");
Console.WriteLine("Server started. Press ENTER to quit");
Console.ReadLine();
}
internal class Service : MarshalByRefObject, IService
{
public string Greeting(string s)
{
var result = $"Hello, {s}!";
Console.WriteLine("Replying to client: {0}", result);
return result;
}
}
public interface IService
{
string Greeting(string s);
}
private static void RunClient()
{
Console.WriteLine("Connecting to server...");
RegisterChannel();
var proxy = (IService)Activator.GetObject(typeof(IService), "gtcp://localhost:8737/Service");
var greeting = proxy.Greeting("World");
Console.WriteLine("Server replies: {0}", greeting);
Console.WriteLine("Press ENTER to quit.");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment