Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C#

A Complete TCP Server/Client Communication and RMI Framework in C# .NET - Implementation

Rate me:
Please Sign up or sign in to vote.
4.91/5 (150 votes)
12 Jun 2011CPOL47 min read 1M   32.4K   348  
In this article, I will explain the implementation of an Open Source lightweight framework (named Simple Client Server Library (SCS)) that is developed to create client/server applications using a simple Remote Method Invocation mechanism over TCP/IP.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using Hik.Samples.Scs.IrcChat.Arguments;
using Hik.Samples.Scs.IrcChat.Client;
using Hik.Samples.Scs.IrcChat.Controls;

namespace Hik.Samples.Scs.IrcChat.Windows
{
    /// <summary>
    /// Interaction logic for PrivateChatWindow.xaml
    /// </summary>
    public partial class PrivateChatWindow : Window, IMessagingAreaContainer
    {
        #region Public properties

        /// <summary>
        /// Nick of the current user.
        /// </summary>
        public string CurrentUserNick { set; get; }

        /// <summary>
        /// Sets the status of the current user.
        /// </summary>
        public UserStatus CurrentUserStatus
        {
            set { SetStatus(lblCurrentUserStatus, value); }
        }

        /// <summary>
        /// Sets the avatar picture of current user.
        /// </summary>
        public ImageSource CurrentUserAvatar
        {
            set { imgCurrentUserAvatar.Source = value; }
        }

        /// <summary>
        /// Gets/Sets the nick of the remote user.
        /// </summary>
        public string RemoteUserNick
        {
            set
            {
                _remoteUserNick = value;
                Title = _remoteUserNick;
            }

            get { return _remoteUserNick; }
        }
        private string _remoteUserNick;

        /// <summary>
        /// Sets the status of the remote user.
        /// </summary>
        public UserStatus RemoteUserStatus
        {
            set
            {
                _remoteUserStatus = value;
                SetStatus(lblRemoteUserStatus, _remoteUserStatus);
            }
        }
        private UserStatus _remoteUserStatus;

        /// <summary>
        /// Sets the avatar picture of the remote user.
        /// </summary>
        public ImageSource RemoteUserAvatar
        {
            set { imgRemoteUserAvatar.Source = value; }
        }

        #endregion

        #region Private fields

        /// <summary>
        /// Reference to chat controller to send private messages to remote user.
        /// </summary>
        private readonly IChatController _controller;

        /// <summary>
        /// WindowInteropHelper object that is used to get a Handle to this window.
        /// </summary>
        private readonly WindowInteropHelper _windowInteropHelper;

        #endregion

        #region Contructor and initializing methods

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="controller">Reference to chat controller to send private messages to remote user</param>
        public PrivateChatWindow(IChatController controller)
        {
            _controller = controller;
            _windowInteropHelper = new WindowInteropHelper(this);
            InitializeComponent();
            InitializeControls();
        }

        private void InitializeControls()
        {
            MessageHistory.MessagingAreaContainer = this;
            MessageHistory.IsTextStyleChangingEnabled = false;
        }

        #endregion

        #region Public methods

        /// <summary>
        /// This method is used to add a new message to message history of that window.
        /// </summary>
        /// <param name="message">Message</param>
        public void MessageReceived(ChatMessage message)
        {
            MessageHistory.MessageReceived(_remoteUserNick, message);
            if (!IsActive)
            {
                //Flash taskbar button if this window is not active
                WindowsHelper.FlashWindow(_windowInteropHelper.Handle, WindowsHelper.FlashWindowFlags.FLASHW_TRAY, 1, 1000);
                ClientHelper.PlayIncomingMessageSound();
            }
        }

        /// <summary>
        /// This method is called when remote user has logged off.
        /// </summary>
        public void UserLoggedOut()
        {
            Title = RemoteUserNick + " - offline";
            MessageHistory.IsReadOnly = true;
            lblRemoteUserStatus.Content = "Offline";
            lblRemoteUserStatus.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFDDDDDD"));
        }

        /// <summary>
        /// This method is called when remote user has logged in again.
        /// </summary>
        public void UserLoggedIn()
        {
            Title = RemoteUserNick;
            MessageHistory.IsReadOnly = false;
        }
        
        /// <summary>
        /// This method is called by MessagingAreaControl to send messages.
        /// </summary>
        /// <param name="message">Message to be sent</param>
        public void SendMessage(ChatMessage message)
        {
            _controller.SendPrivateMessage(RemoteUserNick, message);
            MessageHistory.MessageReceived(CurrentUserNick, message);
        }

        #endregion

        #region Private methods

        /// <summary>
        /// Sets status of a user to a label.
        /// </summary>
        /// <param name="statusLabel">Label to show user status</param>
        /// <param name="status">New status of the user</param>
        private static void SetStatus(Label statusLabel, UserStatus status)
        {
            switch (status)
            {
                case UserStatus.Busy:
                    statusLabel.Content = "Busy";
                    statusLabel.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFF4E4E"));
                    break;
                case UserStatus.Out:
                    statusLabel.Content = "Out";
                    statusLabel.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF3179FE"));
                    break;
                default: //Default: Available
                    statusLabel.Content = "Available";
                    statusLabel.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF2BE400"));
                    break;
            }
        }

        #endregion
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Volosoft
Turkey Turkey
I have started programming at 14 years old using Pascal as hobby. Then I interested in web development (HTML, JavaScript, ASP...) before university.

I graduated from Sakarya University Computer Engineering. At university, I learned C++, Visual Basic.NET, C#, ASP.NET and Java. I partly implemented ARP, IP and TCP protocols in Java as my final term project.

Now, I am working on Windows and web based software development mostly using Microsoft technologies in my own company.

My open source projects:

* ABP Framework: https://abp.io
* jTable: http://jtable.org
* Others: https://github.com/hikalkan

My personal web site:

https://halilibrahimkalkan.com

Comments and Discussions