A simple Chat application based on .NET Remoting

-------------------------------------------------------
Goto blog home
Visit my new blog dedicated to Internet of Things, Embedded Programming & Automation
-------------------------------------------------------
It is good to be back writing again! This time on a simple chat application (windows console based). So here goes the C# code, just one single file named Server.cs, that is all you will need. Note that there are no validations done. Just take the exe and run from two client machines and enjoy chatting. Hope you will find it exciting. (Disclaimer: Tested over a LAN with host computers in the same domain. Not tested over the open Internet.)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Net;

namespace KChat
{
public class Server : MarshalByRefObject
{
private static Server _objRecipientServer = null;
private static Server _objCallingServer = null;
private static string _strCallingClientChatName = string.Empty;
private static string _strCallingClientIPAddress = string.Empty;
private static string _strCommandText = string.Empty;
private static string _strRecipientIPAddress = string.Empty;
private static string _strSenderChatName = string.Empty;
private static string _strSenderIPAddress = string.Empty;

static void Main(string[] args)
{
Server _objServer = new Server();
_objServer.StartSelfAsServer();
_strCommandText = Console.ReadLine();
bool _bContinue = true;

while (_bContinue)
{
if (_strCommandText.ToUpper() == "QUIT")
{
_bContinue = false;
break;
}
else if (_strCommandText.ToUpper() == "CONNECT")
{
Console.WriteLine("--------------------");
Console.Write("Enter your chat name: ");
_strSenderChatName = Console.ReadLine();
Console.Write("Enter your IP Address: ");
_strSenderIPAddress = Console.ReadLine();
Console.Write("Enter recipient IP Address: ");
_strRecipientIPAddress = Console.ReadLine();
Console.WriteLine("Chat Agent>> Initializing connection...");
_objServer.InitializeRecipient(_strRecipientIPAddress, _strSenderChatName, _strSenderIPAddress);
Console.WriteLine("Chat Agent>> Connected to " + _strRecipientIPAddress + "...");
}
else
{
if (_strSenderChatName.Length == 0)
{
Console.Write("Enter your chat name: ");
_strSenderChatName = Console.ReadLine();
}
Console.WriteLine("Chat Agent>> Sending...");
_objServer.DispatchMessage(_strCommandText);
Console.WriteLine("Chat Agent>> Message sent...");
}
_strCommandText = Console.ReadLine();
}
}

public void StartSelfAsServer()
{
HttpChannel _objChannel = new HttpChannel(1095);
ChannelServices.RegisterChannel(_objChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(KChat.Server), "Server", WellKnownObjectMode.Singleton);
Console.WriteLine("Chat Agent>> Chat server started.");
}

public void Listener(string strMessage)
{
Console.WriteLine(strMessage);
}

public void InitializeRecipient(string strRecipientIPAddress, string strCallingClientChatName, string strCallingClientIPAddress)
{
if (_objRecipientServer == null)
{
_objRecipientServer = (Server)Activator.GetObject(typeof(KChat.Server), "http://" + strRecipientIPAddress + ":1095/Server");
_objRecipientServer.RegisterCallingClient(strCallingClientChatName, strCallingClientIPAddress);
}
}

public void RegisterCallingClient(string strCallingClientChatName, string strCallingClientIPAddress)
{
Console.WriteLine("Chat Agent>> Incoming connection from person named '" + strCallingClientChatName + "' on client IP=" + strCallingClientIPAddress + " waiting for your acceptance...");
Console.WriteLine("You can start replying below...");
_strCallingClientChatName = strCallingClientChatName;
_strCallingClientIPAddress = strCallingClientIPAddress;

if (_objCallingServer == null)
{
_objCallingServer = (Server)Activator.GetObject(typeof(KChat.Server), "http://" + strCallingClientIPAddress + ":1095/Server");
}
}

public void DispatchMessage(string strMessage)
{
_objRecipientServer.Listener(_strSenderChatName + ">>" + strMessage);
}
}
}