Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

TCP/IP Chat Application Using C#

, 30 Jan 2006 CPOL
This is a LAN chat application with TCP/IP socket programming technology in C#. This application is a multi thread network application and works in a non-blocking way. Public and private chat is also implemented in this code.
demo.zip
Demo
ChatClient.exe
CommandClient.dll
CommandServer.dll
ConsoleServer.exe
P_0_0_0.U_0.dll
source.zip
CommandServer
CommandServer
bin
Debug
Properties
ConsoleServer
bin
Debug
obj
Debug
Refactor
ResolveAssemblyReference.cache
TempPE
Properties
CommandClient
ChatClient
bin
Debug
ChatClient.csproj.user
CSS
Graphics
arrow.gif
back.gif
face.gif
Icons
Enter.ico
Exit.ico
Private.ico
Save.ico
SendMessage.ico
Smiely.ico
Images
arrow.gif
Enter.png
Exit.png
face.gif
MNUBack.png
Private.png
Save.png
Smiely.png
Properties
Settings.settings
SoundResources
Door.wav
Knock.wav
Message.wav
Pow.wav
CommandClient
bin
Debug
obj
Debug
TempPE
Properties
P_0_0_0.U_0.dll
��using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using Proshot.CommandClient;

using System.Net;

using System.Runtime.InteropServices;





namespace ChatClient

{

    internal enum FlashMode

    {

        FLASHW_CAPTION = 0x1 ,

        FLASHW_TRAY = 0x2 ,

        FLASHW_ALL = FLASHW_CAPTION | FLASHW_TRAY

    }



    internal struct FlashInfo

    {

        public int cdSize;

        public System.IntPtr hwnd;

        public int dwFlags;

        public int uCount;

        public int dwTimeout;



    }



    public partial class frmPrivate : Form

    {



        private CMDClient remoteClient;

        private IPAddress targetIP;

        private string remoteName;

        private bool activated;

        public string RemoteName

        {

            get { return this.remoteName; }

        }

        protected override bool ProcessCmdKey(ref Message msg , Keys keyData)

        {

            if ( keyData == Keys.Enter )

                this.SendMessage();

            if ( this.txtMessages.Focused && !ShareUtils.IsValidKeyForReadOnlyFields(keyData) )

                return true;

            return base.ProcessCmdKey(ref msg , keyData);

        }

        public frmPrivate(CMDClient cmdClient,IPAddress friendIP,string name,string initialMessage)

        {

            InitializeComponent();

            this.remoteClient = cmdClient;

            this.targetIP = friendIP;

            this.remoteName = name;

            this.Text += " With " + name;

            this.txtMessages.Text = this.remoteName + ": " + initialMessage +Environment.NewLine;

            this.remoteClient.CommandReceived += new CommandReceivedEventHandler(private_CommandReceived);

        }



        public frmPrivate(CMDClient cmdClient , IPAddress friendIP , string name)

        {

            InitializeComponent();

            this.remoteClient = cmdClient;

            this.targetIP = friendIP;

            this.remoteName = name;

            this.Text += " With " + name;

            this.remoteClient.CommandReceived += new CommandReceivedEventHandler(private_CommandReceived);

        }



        private void private_CommandReceived(object sender , CommandEventArgs e)

        {

            switch ( e.Command.CommandType )

            {

                case ( CommandType.Message ):

                    if ( !e.Command.Target.Equals(IPAddress.Broadcast) && e.Command.SenderIP.Equals(this.targetIP))

                    {

                        this.txtMessages.Text += e.Command.SenderName + ": " + e.Command.MetaData + Environment.NewLine;

                        if ( !this.activated)

                        {

                            if(this.WindowState == FormWindowState.Normal || this.WindowState == FormWindowState.Maximized)

                                ShareUtils.PlaySound(ShareUtils.SoundType.NewMessageReceived);

                            else

                                ShareUtils.PlaySound(ShareUtils.SoundType.NewMessageWithPow);

                            this.Flash(this.Handle , FlashMode.FLASHW_ALL , 3);

                        }

                    }

                    break;

            }    

        }



        [DllImport("user32.dll")]

        private static extern int FlashWindowEx(ref FlashInfo pfwi);

        private void Flash(System.IntPtr hwnd , FlashMode flashMode , int times)

        {

            unsafe

            {

                FlashInfo FlashInf = new FlashInfo();

                FlashInf.cdSize = sizeof(FlashInfo);

                FlashInf.dwFlags = (int)flashMode;

                FlashInf.dwTimeout = 0;

                FlashInf.hwnd = hwnd;

                FlashInf.uCount = times;

                FlashWindowEx(ref FlashInf);

            }

        }



        private void btnSend_Click(object sender , EventArgs e)

        {

            this.SendMessage();

        }



        private void SendMessage()

        {

            if ( this.remoteClient.Connected && this.txtNewMessage.Text.Trim() != "")

            {

                this.remoteClient.SendCommand(new Proshot.CommandClient.Command(Proshot.CommandClient.CommandType.Message , this.targetIP , this.txtNewMessage.Text));

                this.txtMessages.Text += this.remoteClient.NetworkName + ": " + this.txtNewMessage.Text.Trim() + Environment.NewLine;

                this.txtNewMessage.Text = "";

                this.txtNewMessage.Focus();

            }

        }



        private void frmPrivate_FormClosing(object sender , FormClosingEventArgs e)

        {

            this.remoteClient.CommandReceived -= new CommandReceivedEventHandler(private_CommandReceived);

        }



        private void mniExit_Click(object sender , EventArgs e)

        {

            this.Close();

        }



        private void mniSave_Click(object sender , EventArgs e)

        {

            SaveFileDialog saveDlg = new SaveFileDialog();

            saveDlg.Filter = "HTML Files(*.HTML;*.HTM)|*.html;*.htm";

            saveDlg.FilterIndex = 0;

            saveDlg.RestoreDirectory = true;

            saveDlg.CheckPathExists = true;

            saveDlg.OverwritePrompt = true;

            saveDlg.FileName = this.Text;

            if ( saveDlg.ShowDialog() == DialogResult.OK )

                ShareUtils.SaveAsHTML(saveDlg.FileName , this.txtMessages.Lines , this.Text);

        }



        private void frmPrivate_Load(object sender , EventArgs e)

        {

            this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width , Screen.PrimaryScreen.WorkingArea.Height - this.DesktopBounds.Height);

        }



        private void frmPrivate_Activated(object sender , EventArgs e)

        {

            this.activated = true;

        }



        private void frmPrivate_Deactivate(object sender , EventArgs e)

        {

            this.activated = false;

        }



    }

}

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)

Share

About the Author

ALLEXY
Software Developer (Senior)
Australia Australia
MCPD
MCTS: ASP.NET Applications
MCTS: ADO.NET Applications
MCTS: Windows Forms Applications
MCTS: Windows Communication Foundation Applications
MCPD: Enterprise Application Developer

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 31 Jan 2006
Article Copyright 2006 by ALLEXY
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid