CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  How to C# Chat Client

The Microsoft .NET framework provides two namespaces, System.Net and System.Net.Sockets for managed implementation of Internet protocols that applications can use to send or receive data over the Internet . The C# Chat Client here is a Windows based Application and its main function is to send message to the Chat Server.

In the previous section C# Multi Threaded Socket Program we saw a C# Multithreaded Server Socket Program communicate with more than one Client at the same time . If you don't know the basics of Socket programming , take a look at the section C# Socket Programming before you start this section.

The C# Chat Server Program has two sections.

1. C# Chat Server

2. Chat Client

csharp-chat-client

Chat Client

The Chat Client here is to connect the PORT 8888 of the C# Chat Server in " 127.0.0.1 " . Here we give " 127.0.0.1 " , because Chat Server and Chat Client are running on the same machine . When we start the Chat Client program , we have to enter a User Name for identifying the client in Server side . The Client program connect to the Chat Server and start a Thread for receive the messages from Server side client . Here we implement an infinite loop in the function getMessage() and call this function in a Thread .

Create a new C# Windows based project and put the source code in it.

         C# Source Code Download           Print Source Code
         How to C# Chat Client - Download
        
C# Tutorial

using System;
using System.Windows.Forms;
using System.Text;
using System.Net.Sockets ;
using System.Threading;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);
        string readData = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            readData = "Conected to Chat Server ...";
            msg();
            clientSocket.Connect("127.0.0.1", 8888);
            serverStream = clientSocket.GetStream();

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox3.Text + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            Thread ctThread = new Thread(getMessage);
            ctThread.Start();
        }

        private void getMessage()
        {
            while (true)
            {
                serverStream = clientSocket.GetStream();
                int buffSize = 0;
                byte[] inStream = new byte[10025];
                buffSize = clientSocket.ReceiveBufferSize;
                serverStream.Read(inStream, 0, buffSize);
                string returndata = System.Text.Encoding.ASCII.GetString(inStream);
                readData = "" + returndata;
                msg();
            }
        }

        private void msg()
        {
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(msg));
            else
                textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + readData;
        } 

    }
}

The C# Chat Server Program has two sections.
1. C# Chat Server

2. Chat Client

How to run Chat Server program ?

Create the C# Chat Server and C# Chat Client are two separate C# projects and compile and build the program. Open a DOS Prompt and run the Server Program first and then run the Client program .

In the Client program, Enter a Chat name and click " Connect to Server " button . Then you can see the message in the Server program User "Joined Chat Room" . Similarly you can connect more than one Clients at the same time and start chatting each other. If you plan to run more than one client, it is better to copy the .exe file in separate folders and run from the .exe file.


CSharp Communications Related Contents
*     How to send email from C#
*     How to send email with attachment from C#
*     How to send html email from C#
*     How to send cdo email from C#
*     How to find hostname of a computer
*     How to find IP Adress of a computer
*     How to read URL Content from webserver
*     How to C# Socket programming
*     C# Server Socket program
*     C# Client Socket program
*     C# Multi threaded socket programming
*     C# Multi threaded Server Socket programming
*     C# Multi threaded Client Socket programming
*     How to C# Chat server programming
*     How to C# Chat Server


   Home      VB.NET      C#
CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.