Click here to Skip to main content
15,893,401 members
Articles / Desktop Programming / Win32

Chat Client Server

Rate me:
Please Sign up or sign in to vote.
4.21/5 (72 votes)
18 May 2007CPOL1 min read 335.3K   43K   79   44
An implementation of chat client and server with win32 sockets
Sample Image - chat_client_server.jpg - Click to enlarge image

Introduction

Recently I got myself introduced to socket programming. I read through many books and articles to get an insight into the subject. After learning a bit, I decided to share my knowledge with the world. For this purpose, I created a simple console based chat application. I hope this will be a good reference code for C++ socket programming. I am going to introduce the following concepts to you here.

  • Sockets
  • Multi threading

What is Chat Client Server?

The demo project given along with this article consists of two console-based applications.

Chat Server

Chat server is an application which does the following operations:

  • Listens for incoming calls from clients. Client running in any PC can connect to the server if IP address of the server is known.
  • Listens for messages from all the connected clients.
  • Broadcasts the message from clients to all the clients connected to the server.
  • You can also type-in messages in the server, which will be broadcasted to all the clients.

Chat Client

Chat client does the following things:

  • Sends messages to server as well as to all the connected clients.
  • Views the messages from all the clients and server.

Please download the PDF document to read the rest of the documentation. I have created a PDF because the article consists of 2-3 diagrams.

License

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


Written By
Software Developer (Senior) DWS
Australia Australia

Comments and Discussions

 
GeneralRe: i have an idea Pin
Boby Thomas P9-May-06 21:22
Boby Thomas P9-May-06 21:22 
Generalintegration of project Pin
dimas757-May-06 9:54
dimas757-May-06 9:54 
GeneralRe: integration of project Pin
mysterious_caveman10-Jun-07 10:43
mysterious_caveman10-Jun-07 10:43 
Generalthanks Pin
raul_sfk7-May-06 0:48
raul_sfk7-May-06 0:48 
GeneralRe: thanks Pin
Boby Thomas P7-May-06 8:56
Boby Thomas P7-May-06 8:56 
Generalinfo required Pin
SunithaC7-May-06 0:43
SunithaC7-May-06 0:43 
GeneralRe: info required Pin
Boby Thomas P7-May-06 8:56
Boby Thomas P7-May-06 8:56 
GeneralRe: info required Pin
roninttk9-May-06 22:13
roninttk9-May-06 22:13 
To create communication between 2 networked computers, you will either need to setup a Client/Server model (like this demo program) or a Peer-to-Peer model. The Client/Server model establishes one program as the "Server" to which all other "Clients" connect to and receive directions from the server. The Peer-to-Peer model is a loosely connected group of network aware programs, none of which control the interation between each other.

Another important factor to consider is which protocol to implement: TCP/IP (used by this demo) or UDP/IP. TCP is a "connection oriented" socket communications, in which data is buffered, sent in sequential order, with guaranteed delivery. UDP is "connection-less" in that it packages up data and sends it off without caring if it is received by that addressee or not, nor in what order the packets are received. However, UDP strengths are reduced overhead (28 bytes to TCP's 40 bytes) and generally a higher throughput rate of message traffic.

TCP or UDP brings you up to Layer 4 on the OSI model. To establish your own communications between your 2 programs, you will essentially be creating your own messaging protocol which is contained within TCP segment or UDP datagram. So on one end you will create your message and send it within a WinSock call <send((char*)pmessage, msgsize)="">, if sent by TCP. The receiving program will get the message in memory buffer <recv((char*)pbuffer, &bytesreceived)="">, and then need to process the message contained in the buffer.

Therefore, many considerations are involved based on the type of communication requirements your program has:
1. Network I/O architecture (Client/Server vs. Peer-to-Peer)
2. Underlying protocols used (TCP vs. UDP - 2 most common)
I have a couple of books at the office I use for reference. One is "Network Programming for Microsoft Windows" by Microsoft Press. I also use MSDN's site pretty heavily for specific details about various function calls, but not the best read for learning.

Once you are past the preliminaries, then the real fun begins! You will get to learn about Blocking Mode vs. all the Non-Blocking Mode communications... but that is another story, for another time.

Best of luck,

Tobin Kaneshiro

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.