Click here to Skip to main content
15,895,606 members
Articles / Programming Languages / C#

A Simple Multi-Threaded Server Client Instant Messenger Application

Rate me:
Please Sign up or sign in to vote.
4.32/5 (26 votes)
4 Jun 2006CPOL2 min read 158.6K   4.6K   109  
A Simple Multi-Threaded Server Client Instant Messenger Application that uses TCP Listener and TCP Client. Allows Personal message and Conference communication between Clients and with the Server
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Net;
using System.Net.Sockets;
using System.Threading;

using ChatMessage = ChatLibrary.Message;

namespace Client
{
	public class ClientWindow : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox txtMessages;
		private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.TextBox txtSend;

		private System.ComponentModel.Container components = null;


		public ClientWindow()
		{
			InitializeComponent();

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.txtMessages = new System.Windows.Forms.TextBox();
			this.btnSend = new System.Windows.Forms.Button();
			this.txtSend = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// txtMessages
			// 
			this.txtMessages.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtMessages.BackColor = System.Drawing.Color.White;
			this.txtMessages.Location = new System.Drawing.Point(8, 8);
			this.txtMessages.Multiline = true;
			this.txtMessages.Name = "txtMessages";
			this.txtMessages.ReadOnly = true;
			this.txtMessages.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.txtMessages.Size = new System.Drawing.Size(344, 152);
			this.txtMessages.TabIndex = 0;
			this.txtMessages.Text = "";
			this.txtMessages.TextChanged += new System.EventHandler(this.txtMessages_TextChanged);
			// 
			// btnSend
			// 
			this.btnSend.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnSend.Enabled = false;
			this.btnSend.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnSend.Location = new System.Drawing.Point(296, 168);
			this.btnSend.Name = "btnSend";
			this.btnSend.Size = new System.Drawing.Size(56, 40);
			this.btnSend.TabIndex = 2;
			this.btnSend.Text = "Send";
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// txtSend
			// 
			this.txtSend.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtSend.Location = new System.Drawing.Point(8, 168);
			this.txtSend.Multiline = true;
			this.txtSend.Name = "txtSend";
			this.txtSend.Size = new System.Drawing.Size(280, 40);
			this.txtSend.TabIndex = 1;
			this.txtSend.Text = "";
			this.txtSend.TextChanged += new System.EventHandler(this.txtSend_TextChanged);
			// 
			// ClientWindow
			// 
			this.AcceptButton = this.btnSend;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.ClientSize = new System.Drawing.Size(360, 213);
			this.Controls.Add(this.txtSend);
			this.Controls.Add(this.txtMessages);
			this.Controls.Add(this.btnSend);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "ClientWindow";
			this.Text = "Client";
			this.Load += new System.EventHandler(this.ClientForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnSend_Click(object sender, System.EventArgs e)
		{
			

			ChatMessage sendMessage		= new ChatLibrary.Message();
			sendMessage.Sender			= ClientLibrary.mainMessenger.clientSocket.Name;
			sendMessage.Receiver		
				=  this.Text.Equals(ChatLibrary.Common.Conference)? ChatLibrary.Common.All : this.Text;
			sendMessage.MessageCommand	
				= this.Text.Equals(ChatLibrary.Common.Conference)? ChatLibrary.Command.Conference: ChatLibrary.Command.PersonalMessage;
			sendMessage.MessageDetail	= txtSend.Text;

			ClientLibrary.mainMessenger.clientSocket.SendMessage(sendMessage);
			if (!this.Text.Equals(ChatLibrary.Common.Conference))
			{
				WriteMessage(sendMessage);
			}
			txtSend.Clear();
			txtSend.Focus();
		}

		private void clientSocket_MessageReceived(object sender, ChatLibrary.Message receivedMessage)
		{
			if (receivedMessage.MessageCommand.Equals(ChatLibrary.Command.PersonalMessage))
			{
				WriteMessage(receivedMessage);
			}
			if (receivedMessage.MessageCommand.Equals(ChatLibrary.Command.Logout))
			{
                ClientLibrary.mainMessenger.clientSocket.Stop();
			}
		}

		public string WriteMessage (ChatMessage receivedMessage)
		{
			string strWriteMessage = string.Empty;
			if (receivedMessage.MessageDetail.StartsWith("\r\n") ) // ang daya mo!
			{
				strWriteMessage =	string.Format("{0}", receivedMessage.MessageDetail);
			}
			else
			{
				strWriteMessage =	string.Format("\r\n{0} {1}: {2}", GetTime(), receivedMessage.Sender,  receivedMessage.MessageDetail);
			}

			txtMessages.Text += strWriteMessage;
			return strWriteMessage;
		}

		private string GetTime ()
		{
			return DateTime.Now.Hour + ":" +DateTime.Now.Minute + ":" + DateTime.Now.Second;

		}

		private void txtSend_TextChanged(object sender, System.EventArgs e)
		{
			
			btnSend.Enabled = (!txtSend.Text.Equals(string.Empty) && ClientLibrary.mainMessenger.clientSocket != null )
				?true: false;
		}

		private void ClientForm_Load(object sender, System.EventArgs e)
		{
		
		}

		private void txtMessages_TextChanged(object sender, System.EventArgs e)
		{
			txtMessages.SelectionStart = txtMessages.Text.Length;
			txtMessages.ScrollToCaret();
			txtSend.Focus();
		}
	}

	class ClientLibrary 
	{
		internal static MessengerForm  mainMessenger; 
	}

}

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
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions