C++ Chat Server Tutorial Part I - Taywils
C++ Chat Server Tutorial Part I - Taywils
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
Our client code for the Chat application will rely upon the
use of threads to divide our code into three distinct subroutines.
A thread for displaying chat messages
Another thread for sending messages to the Chat server
And a third thread for receiving messages routed by the
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
Producer-Consumer setup.
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
/*boostChatClient.cpp*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
The above library includes are fairly basic for a C++ console
application but unfamiliar to most are probably the Boost
includes.
boost/thread Multithreading support
boost/bind A library for functional programming but
used here to create sub-rountines for threads
boost/asio System socket and network programming
library
boost/algorithm/string Pretty self-explainatory; gives
use some new string methods
I will be using namespace aliasing in this application, it can be
a pain sometimes to read code without namespace aliasing
so lets at least make an effort to strive for clean human
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
//TCPsocketforconnectingtos
constint//Maximumsizeforinputbuffer
//Terminalpromptdisplayedtochatusers
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
/*boostChatClient.cpp*/
//FunctionPrototypes
bool
void
void
void
string
//EndofFunctionPrototypes
try
new
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
catch
return
Function Definitions
The first function buildPrompt is a function which handles
the display of the terminal input for clients. Its fairly simple
in that it takes a string of the clients name and assigns it to
the value of the prompt pointer we declared earlier.
/*boostChatClient.cpp*/
string
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
constint
char
char
stringnewstring
string
return
int
char
for
if
newstring
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
char
string
for
string
if
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
//Thestringforquittingtheapplication
//Ontheserversidethereisalsoacheckfor"quit"toterminatetheTCPsock
ifstring
exit//Replacewithcleanupcodeifyouwantbutforthistutorial
for
if
//Canyourefactorthiscodetohandlemultipleuserswiththesamepro
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
if
ifstring
else
returntrue
returnfalse
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
ERRVW&KDW&OLHQWFSSKRVWHGZLWKE\*LW+XE
YLHZUDZ
KWWSZZZWD\ZLOVPHERRVWFKDWFOLHQWKWPO
&&KDW6HUYHU7XWRULDO3DUW,_7D\ZLOVPH
&RPPHQWV
WD\ZLOVPH
6KDUH
5HFRPPHQG
/RJLQ
6RUWE\%HVW
-RLQWKHGLVFXVVLRQ
/HOHN DPRQWKDJR
&RXOG\RXKHOSZLWKFRPSLOLQJ#8EXQWX"
5HSO\ 6KDUH
:DVHHP$KPDG1DHHP PRQWKVDJR
1RWXVHIXO,1HHG7XWRULDO1RW6RXUFH&R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