Click here to Skip to main content
15,892,746 members
Articles / Web Development / ASP.NET

Chat Application using Web services in C#

Rate me:
Please Sign up or sign in to vote.
4.85/5 (38 votes)
15 May 2008CPOL5 min read 384.8K   41.8K   139  
This is a cool chat application created in DotNet using web services having all the functionalities.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Windows.Forms;

namespace ChatClient
{
    public partial class Configurator : Form
    {
        public delegate void IPChangeHandler(object sender, IPAddressInfoEventArgs e);
        public event IPChangeHandler IPChange;

        public Configurator()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OnIPChange(object sender, IPAddressInfoEventArgs e)
        {
            // Check if there are any Subscribers
            if (IPChange != null)
            {
                // Call the Event
                IPChange(sender, e);
            }
        }

        private void Configurator_Load(object sender, EventArgs e)
        {
            try
            {
                System.Reflection.Assembly Asm = System.Reflection.Assembly.GetExecutingAssembly();
                string path = Asm.Location + ".config";
                string strKeyNm = "ChatService";
                string strKeyVal = GetConfigData(path, strKeyNm);
                txtIP.Text = strKeyVal;
                btnApply.Enabled = false;
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error in reading the config file.");
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtIP.Text.Trim() == "")
                {
                    MessageBox.Show("Please specify the IP Address of the Chat Server.");
                }
                else
                {
                    System.Reflection.Assembly Asm = System.Reflection.Assembly.GetExecutingAssembly();
                    string path = Asm.Location + ".config";
                    string strKeyNm = "ChatService";
                    SaveConfigData(path, strKeyNm, txtIP.Text);
                    btnApply.Enabled = false;

                    IPAddressInfoEventArgs ipInfo = new IPAddressInfoEventArgs(txtIP.Text.Trim());
                    OnIPChange(this, ipInfo);                    
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Failed to save the data in the config file.");
            }
        }

        public string GetConfigData(string strFileName, string KeyName)
        {
            try
            {
                string strKeyValue = "";
                XmlDocument xdoc = new System.Xml.XmlDocument();
                xdoc.Load(strFileName);

                foreach (XmlNode xNode in xdoc["configuration"]["appSettings"])
                {
                    if (xNode.Name == "add")
                    {
                        if (xNode.Attributes.GetNamedItem("key").Value.ToUpper() == KeyName.ToUpper())
                        {
                            strKeyValue = xNode.Attributes.GetNamedItem("value").Value;
                        }
                    }
                }
                return (strKeyValue);
            }
            catch (Exception ex)
            {
                return (null);
            }
        }

        public void SaveConfigData(string strFileName, string KeyName, string KeyValue)
        {
            XmlDocument xdoc = new System.Xml.XmlDocument();
            xdoc.Load(strFileName);

            foreach (XmlNode xNode in xdoc["configuration"]["appSettings"])
            {
                if (xNode.Name == "add")
                {
                    if (xNode.Attributes.GetNamedItem("key").Value.ToUpper() == KeyName.ToUpper())
                    {
                        xNode.Attributes.GetNamedItem("value").Value = KeyValue;
                    }
                }
            }
            xdoc.Save(strFileName);
        }

        private void txtIP_TextChanged(object sender, EventArgs e)
        {
            btnApply.Enabled = true;
        }
    }
}

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
Team Leader
United States United States
He has a total experience of more than 6 years in programming on different technologies. Currently working as a Systems Analyst.

Comments and Discussions