Click here to Skip to main content
15,896,467 members
Articles / Programming Languages / C#

.NET Remoting Message Redirection Channel Sinks

Rate me:
Please Sign up or sign in to vote.
3.38/5 (20 votes)
20 Jun 20033 min read 112.6K   1.7K   27  
An upper logic layer transparent way to redirect .NET remoting calls, enabling exposure of .NET remoting services behind firewall/NAT, to anywhere.
using System;
using System.Runtime.Remoting;
using System.Threading;

using KnownObjects;

namespace Client
{
	/// <summary>
	/// ChatClient demostrates simple client application.
	/// </summary>
	class ChatClient : MarshalByRefObject, IChatClient
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			RemotingConfiguration.Configure("Client.exe.config");
					
			for(;;)
			{
				try
				{
					Reachability.ServerSinkProvider.StartWaitRedirectedMsg();

					Console.WriteLine(".NET Remoting has been configured from the Client.exe.config file.");
					ChatRoom chatRoom = new ChatRoom();

					ChatClient c = new ChatClient();
					ObjRef or = RemotingServices.Marshal(c as MarshalByRefObject);
					chatRoom.AttachClient(c);

					for(;;)
					{
						Console.WriteLine("Enter a message to send or an empty string to exit.");

						string str = Console.ReadLine();
						
						if (str.Length <= 0)
							return ;

						int numberOfReceivers = chatRoom.SendMessage(str);
						Console.WriteLine("Message \"{0}\" has been sent. {1} receivers registered at the server.", str, numberOfReceivers);
					}
				}
				catch(Exception ex)
				{
					Console.WriteLine(ex.ToString());
				}
				
				Console.WriteLine("Next attempt to connect to the server will be in 5 seconds.");
				Console.ReadLine();
			}
		}

		/// <summary>
		/// Message receiver.
		/// It receives messages async and writes them separately from the main thread.
		/// But it does not matter for console application.
		/// </summary>
		/// <param name="message">The message.</param>
        public void ReceiveMessage(string message)
		{
			Console.WriteLine("Message \"{0}\" has been received from the server.", message);
		}
		public bool Function()
		{
			Console.WriteLine("Function");
			return true;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
zhi
Researcher
United States United States
My name is Zhiheng Cao. I major in Eletronics Engineering, in University of Tokyo. I do part time jobs of computer programming and teaching high school students English and Mathematics.

CodeProject provided me with valuable information for my programming job many times in the past. I hope I can do my part by providing goodies I have.


Comments and Discussions