Click here to Skip to main content
13,356,318 members (61,449 online)
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 12 Nov 2009

Creating a Client - Server app - Making a Chat

Rate this:
Please Sign up or sign in to vote.
Making a typical client - server chat application


This article shows how we can create a typical client / server application, in this case, a chat.

Main chat's form

This project is divided by two parts, two solutions.

On the one hand, we have the server solution. This is a Windows Service Project. If we want to use this app, we must install first in order to join with other services on our computer. To do that, it must be installed with the installutil.exe tool.
The sentence is as follows:

installutil.exe [PATH]\svcChat.exe

In case we want to uninstall the service, we must use the following:

installutil.exe /u [PATH]\svcChat.exe 

The installer of this service is configured to start manually. If you want that service to start automatically, change the property on its constructor. You can do the same with the Service Account property.

On the other hand, we have a simple chat app made as a Windows Form Project.


There are some actions used to communicate between the server and the client:

  1. #NICK#
    This action is used when client requests a change of his nickname.
  2. #JOIN#
    This action is used when client tries to join to the chat.
  3. #MSG#
    This action is used by client and server to send a message.
  4. #BYE#
    This action is used by client and server to notify that the connection will be closed.
    This action is used by the server to notify the complete list of nicknames that are currently connected.
    This action is used by the server to notify that the name that it is trying to change is not available.

Using the Code

This is the interesting part.


The server app has two main classes: Connection and ConHandler.
Connection contains all properties and methods in order to communicate with the client, whilst ConHanlder handles all the connections that are in use. You can set the service's port number, editing the configuration file.

Also, there is a class called Logger. You can configure it through the configuration file, in order to debug the application. You just must set the file path where the service ought to write all content about its trace.

When the service starts, it throws a thread in order to listen to a new connection request. When a new connection is established, it is put into the collection of connections, and the thread throws a delegate for listening to messages from the client.

Listening to New Connections

private void AcceptingSockets()
        while (true)
            Socket socket = this.tcpListener.AcceptSocket();
            Connection con = new Connection(socket);
            ListenDelegate lDel = this.Recieve;
            AsyncCallback lCallBack = new AsyncCallback(this.ListenCallBack);
            lDel.BeginInvoke(con, ListenCallBack, null);
    catch { }

Receiving Data from the Client

private void Recieve(Connection _con)
    while (_con.IsConnected)
            string msg = _con.ReadLine();
            this.DataHandler(msg, ref _con);
            if (this.log != null)
                this.log.WriteLine(_con.NickName + ": " + msg);
        catch (Exception ex)
            if (this.log != null)
                this.log.WriteLine("ERROR: " + ex.Message);


Once data from the client has been received, it is handled by its corresponding method.

Handling Data

private void DataHandler(string _data, ref Connection _con)
    string[] array = _data.Split(' ');
    if (array.Length > 0)
        switch (array[0])
            case "#NICK#":
            case "#JOIN#":
                string nickName = array[1];
                if (!this.ExistsNickname(nickName))
                    this.AddOrChangeNick(nickName, ref _con);
                    this.SendMessage("#NOTNICKNAME# " + nickName, _con);
            case "#MSG#":
                this.SendMsgToEveryBody(_data, _con.NickName);
            case "#BYE#":


This is a simple client app.
The main class is Connection. It provides all necessary methods and properties in order to establish a connection with the server and write and receive data.
Through the connection's form, we can establish a connection with the server, indicating the server's IP address, the server's port number and the nickname that we wish to use.
When the connection is established, the app throws a thread in order to listen to all messages from the server and be handled by its corresponding method.

Connection settings form

Connecting with the Server

public bool Connect()
        this.tcpClient = new TcpClient(this.serverIp, this.portConnection);
        if (this.tcpClient.Connected)
            this.netStream = this.tcpClient.GetStream();
            this.streamWriter = new System.IO.StreamWriter(this.netStream);
            this.streamReader = new System.IO.StreamReader(this.netStream);
    catch (Exception ex)
        throw new Exception(ex.Message);
    return this.IsConnected;

The other option that we have available is the possibility of changing our nickname using the nickname's form.

Changing nickname

Of course, if you wish to add more features, you can modify the project by adding new methods on the server service and the client app.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gerard Castelló Viader
Software Developer
Spain Spain

You may also be interested in...

Comments and Discussions

Questioninstallutil.exe [PATH]\svcChat.exe what i write in path Pin
1604198429-Nov-12 3:40
member1604198429-Nov-12 3:40 
AnswerRe: installutil.exe [PATH]\svcChat.exe what i write in path Pin
Gerard Castelló Viader29-Nov-12 22:33
memberGerard Castelló Viader29-Nov-12 22:33 
QuestionHey, the server refuses to start up! Pin
Stutcov11-Jul-12 14:05
memberStutcov11-Jul-12 14:05 
AnswerRe: Hey, the server refuses to start up! Pin
Gerard Castelló Viader14-Jul-12 10:07
memberGerard Castelló Viader14-Jul-12 10:07 
QuestionSend Message to somebody Pin
w78gj28-Nov-10 17:19
memberw78gj28-Nov-10 17:19 
QuestionsvcChat.exe WebServise Pin
Member 83208621-Apr-10 0:21
memberMember 83208621-Apr-10 0:21 
AnswerRe: svcChat.exe WebServise Pin
Gerard Castelló Viader21-Apr-10 9:24
memberGerard Castelló Viader21-Apr-10 9:24 
GeneralRe: svcChat.exe WebServise Pin
Member 83208622-Apr-10 6:26
memberMember 83208622-Apr-10 6:26 
GeneralRe: svcChat.exe WebServise Pin
Gerard Castelló Viader23-Apr-10 5:19
memberGerard Castelló Viader23-Apr-10 5:19 
GeneralRe: svcChat.exe WebServise Pin
Member 83208626-Apr-10 21:34
memberMember 83208626-Apr-10 21:34 
GeneralRe: svcChat.exe WebServise Pin
Member 83208626-Apr-10 23:14
memberMember 83208626-Apr-10 23:14 
GeneralRe: svcChat.exe WebServise Pin
Gerard Castelló Viader27-Apr-10 2:14
memberGerard Castelló Viader27-Apr-10 2:14 
AnswerRe: svcChat.exe WebServise Pin
Member 83208627-Apr-10 5:16
memberMember 83208627-Apr-10 5:16 
GeneralThanks Pin
geswan16-Nov-09 21:14
membergeswan16-Nov-09 21:14 
GeneralTry and Catch Pin
morisson_c16-Nov-09 21:12
membermorisson_c16-Nov-09 21:12 
GeneralRe: Try and Catch Pin
Gerard Castelló Viader17-Nov-09 3:29
memberGerard Castelló Viader17-Nov-09 3:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 16 Nov 2009
Article Copyright 2009 by Gerard Castelló Viader
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid