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
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.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Net;

using Proshot.UtilityLib.CommonDialogs;



namespace ChatClient

{

    public partial class frmLogin : Form

    {

        private bool canClose;

        private Proshot.CommandClient.CMDClient client;

        public Proshot.CommandClient.CMDClient Client

        {

            get { return client; }

        }

        public frmLogin(IPAddress serverIP,int serverPort)

        {

            InitializeComponent();

            this.canClose = false;

            Control.CheckForIllegalCrossThreadCalls = false;

            this.client = new Proshot.CommandClient.CMDClient(serverIP , serverPort , "None");

            this.client.CommandReceived += new Proshot.CommandClient.CommandReceivedEventHandler(CommandReceived);

            this.client.ConnectingSuccessed += new Proshot.CommandClient.ConnectingSuccessedEventHandler(client_ConnectingSuccessed);

            this.client.ConnectingFailed += new Proshot.CommandClient.ConnectingFailedEventHandler(client_ConnectingFailed);

        }



        private void client_ConnectingFailed(object sender , EventArgs e)

        {

            frmPopup popup = new frmPopup(PopupSkins.SmallInfoSkin);

            popup.ShowPopup("Error" , "Server Is Not Accessible !" , 200 , 2000 , 2000);

            this.SetEnablity(true);

        }



        private void client_ConnectingSuccessed(object sender , EventArgs e)

        {

           this.client.SendCommand(new Proshot.CommandClient.Command(Proshot.CommandClient.CommandType.IsNameExists,this.client.IP,this.client.NetworkName));

        }



        void CommandReceived(object sender , Proshot.CommandClient.CommandEventArgs e)

        {

            if ( e.Command.CommandType == Proshot.CommandClient.CommandType.IsNameExists )

            {

                if ( e.Command.MetaData.ToLower() == "true" )

                {

                    frmPopup popup = new frmPopup(PopupSkins.SmallInfoSkin);

                    popup.ShowPopup("Error" , "The Username is already exists !" , 300 , 2000 , 2000);

                    this.client.Disconnect();

                    this.SetEnablity(true);

                }

                else

                {

                    this.canClose = true;

                    this.Close();

                }

            }

        }



        private void LoginToServer()

        {

            if ( this.txtUsetName.Text.Trim() == "" )

            {

                frmPopup popup = new frmPopup(PopupSkins.SmallInfoSkin);

                popup.ShowPopup("Error" , "Username is empty !" , 1000 , 2000 , 2000);

                this.SetEnablity(true);

            }

            else

            {

                this.client.NetworkName = this.txtUsetName.Text.Trim();

                this.client.ConnectToServer();

            }

        }

        private void btnEnter_Click(object sender , EventArgs e)

        {

            this.SetEnablity(false);

            this.LoginToServer();

        }

        private void SetEnablity(bool enable)

        {

            this.btnEnter.Enabled = enable;

            this.txtUsetName.Enabled = enable;

            this.btnExit.Enabled = enable;

        }



        private void btnExit_Click(object sender , EventArgs e)

        {

            this.canClose = true;

        }



        private void frmLogin_FormClosing(object sender , FormClosingEventArgs e)

        {

            if ( !this.canClose )

                e.Cancel = true;

            else

                this.client.CommandReceived -= new Proshot.CommandClient.CommandReceivedEventHandler(CommandReceived);

        }

    }

}

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)

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 | Mobile
Web01 | 2.8.140721.1 | Last Updated 31 Jan 2006
Article Copyright 2006 by ALLEXY
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid