Wordware.Publishing.Programming.Multiplayer.Games.eBook DDU

Programming
Multiplayer
Games

Andrew Mulholland
and Teijo Hakala

Wordware Publishing, Inc.

Library of Congress Cataloging-in-Publication Data
Mulholland, Andrew.
Programming multiplayer games / by Andrew Mulholland and Teijo Hakala.
p. cm.
Includes index.
ISBN 1-55622-076-6 (pbk.; companion cd-rom)
1. Computer games--Programming. I. Hakala, Teijo. II. Title.
QA76.76.C672M855 2004
794.8'1711—dc22
2003027637
CIP


© 2004, Wordware Publishing, Inc.
All Rights Reserved
2320 Los Rios Boulevard
Plano, Texas 75074
No part of this book may be reproduced in any form or by any means
without permission in writing from Wordware Publishing, Inc.
Printed in the United States of America

ISBN 1-55622-076-6
10 9 8 7 6 5 4 3 2 1
0403
UNIX is a registered trademark of The Open Group in the United States and other countries.
All brand names and product names mentioned in this book are trademarks or service marks of their respective
companies. Any omission or misuse (of any kind) of service marks or trademarks should not be regarded as intent to
infringe on the property of others. The publisher recognizes and respects all marks used by companies, manufacturers,
and developers as a means to distinguish their products.
This book is sold as is, without warranty of any kind, either express or implied, respecting the contents of this book and
any disks or programs that may accompany it, including but not limited to implied warranties for the book’s quality,
performance, merchantability, or fitness for any particular purpose. Neither Wordware Publishing, Inc. nor its dealers or
distributors shall be liable to the purchaser or any other person or entity with respect to any liability, loss, or damage

caused or alleged to have been caused directly or indirectly by this book.

All inquiries for volume purchases of this book should be addressed to Wordware Publishing,
Inc., at the above address. Telephone inquiries may be made by calling:
(972) 423-0090

Contents
About the Authors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv

Part I — Theory
Chapter 1 Introduction to Windows Programming . . . . . . . . . . 3
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Windows Messaging System . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Creating a Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Sending Information to Your Window . . . . . . . . . . . . . . . . . . . . . 8
Static Link Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Creating a Static Link Library . . . . . . . . . . . . . . . . . . . . . 10
Using a Static Link Library. . . . . . . . . . . . . . . . . . . . . . . 13
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Chapter 2 Using Databases . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . .
What Is MySQL? . . . . . . . . . . . . . . . . .
Installing MySQL . . . . . . . . . . . . . . . .
SQL Statements . . . . . . . . . . . . . . . . .
Data Definition Language . . . . . . . . . . . .
Creating and Dropping Databases . . . .
Creating a Database . . . . . . . .
Dropping a Database . . . . . . . .
Column (Field) Types in MySQL . . . .
Adding, Modifying, and Dropping Tables
Creating Tables. . . . . . . . . . .
Modifying Tables . . . . . . . . . .
Dropping (Removing) Tables . . .
Data Manipulation Language (DML) . . . . . .
Inserting Data . . . . . . . . . . . . . . .
Modifying Data . . . . . . . . . . . . . .
Removing (Deleting) Data . . . . . . . .
Using Select Statements . . . . . . . . .
Relational Databases. . . . . . . . . . . . . . .

Data Import Methods . . . . . . . . . . . . . .
Importing from a Text File . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

. .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .

. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. . .
. . . .

. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. . 15
. . . 15
. . . 15
. . . 16
. . . 19
. . . 19
. . . 19
. . . 20
. . . 21
. . . 22
. . . 24
. . . 24
. . . 26
. . . 28
. . . 29
. . . 30
. . . 32
. . . 34
. . . 35
. . . 40
. . . 44
. . . 44

iii

Contents

Importing from a Native Source . . . . . . . . . . . . . . . .
Backing Up and Restoring Data . . . . . . . . . . . . . . . . . . . .
Backing up a Database to a File . . . . . . . . . . . . . . . .
Restoring a Backed-Up Database . . . . . . . . . . . . . . .
MySQL C++ Interface . . . . . . . . . . . . . . . . . . . . . . . .
Example 1 — Connecting and Retrieving Data from MySQL
Example 2 — Updating Data in MySQL from an Application
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

45
47
47
49
50
50
53
55

Chapter 3 Creating Web-Based Server Interfaces . . . . . . . . . 57
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Setting Up an Apache 1.3.x Web Server . . . . . . . . . . . . . . . . . . . 57
Installing PHP4 for Apache 1.3.x . . . . . . . . . . . . . . . . . . . . . . . 60
Using PHP: Hypertext Preprocessor . . . . . . . . . . . . . . . . . . . . . 63
The Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Example 1 — index.php . . . . . . . . . . . . . . . . . . . . . 64
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Example 2 — index2.php . . . . . . . . . . . . . . . . . . . . 65
Operators and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Example 3 — index3.php . . . . . . . . . . . . . . . . . . . . 67
Conditional Statements. . . . . . . . . . . . . . . . . . . . . . . . . 68
Example 4 — index4.php . . . . . . . . . . . . . . . . . . . . 68
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Example 5 — index5.php . . . . . . . . . . . . . . . . . . . . 70
User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Example 6a — input.php. . . . . . . . . . . . . . . . . . . . . 71
Example 6b — output.php . . . . . . . . . . . . . . . . . . . . 72
The “Command” System . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Example 7a — core.php . . . . . . . . . . . . . . . . . . . . . 74
Example 7b — welcome.php . . . . . . . . . . . . . . . . . . 74
Example 8a — core.php . . . . . . . . . . . . . . . . . . . . . 76
Example 8b — welcome.php . . . . . . . . . . . . . . . . . . 76
Example 8c — page1.php . . . . . . . . . . . . . . . . . . . . 77
Example 8d — page2.php . . . . . . . . . . . . . . . . . . . . 77
Accessing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
MySQL Example 1 — Connecting and Disconnecting . . . . . . . . 78
MySQL Example 2 — Storing and Retrieving Data . . . . . . . . . 79
MySQL Example 3 — Updating and Removing Data . . . . . . . . . 84
Using FastTemplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Multiple Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Converting the Command Parser Example to FastTemplate . . . . 95
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Chapter 4 Introduction to TCP/IP. . . . . . . . . . . . . . . . . . 103
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

iv

Contents

What Is a Protocol? . . . . . . . . . .
OSI Model. . . . . . . . . . . . . . .
OSI Model Layers . . . . . . .
Internet Protocol . . . . . . . . . . .
Introduction to the Transport Layer
Transmission Control Protocol
User Datagram Protocol . . .
Ports . . . . . . . . . . . . . . . . . .
Introduction to Sockets . . . . . . .
Socket Types . . . . . . . . . .
Address. . . . . . . . . . . . .
Platforms . . . . . . . . . . . .
History of WinSock . . . . . .
Summary . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

Chapter 5 Basic Sockets Programming . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . .
WinSock Initialization . . . . . . . . . . . . . . . . . . . . .
WSAStartup Function (Win32) . . . . . . . . . . . . .
WSACleanup Function (Win32). . . . . . . . . . . . .
WSAEnumProtocols Function (Win32) . . . . . . . .
WinSock Initialization Function . . . . . . . . . . . .
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . .
WSAGetLastError Function (Win32) . . . . . . . . .
Sockets Data Types. . . . . . . . . . . . . . . . . . . . . . .
Platform-specific Data Types . . . . . . . . . . . . . .
Address Structures . . . . . . . . . . . . . . . . . . .
IPv4 Address Structure . . . . . . . . . . . . .
IPv6 Address Structure . . . . . . . . . . . . .
Generic Address Structure. . . . . . . . . . . .
Basic Sockets Functions . . . . . . . . . . . . . . . . . . . .
socket Function (Unix, Win32) . . . . . . . . . . . . .
bind Function (Unix, Win32) . . . . . . . . . . . . . .
connect Function (Unix, Win32) . . . . . . . . . . . .
listen Function (Unix, Win32) . . . . . . . . . . . . .
accept Function (Unix, Win32) . . . . . . . . . . . . .
close Function (Unix)/closesocket Function (Win32) .
Input/Output Functions . . . . . . . . . . . . . . . . . . . .
send Function (Unix, Win32) . . . . . . . . . . . . . .
recv Function (Unix, Win32) . . . . . . . . . . . . . .
sendto Function (Unix, Win32) . . . . . . . . . . . . .
recvfrom Function (Unix, Win32). . . . . . . . . . . .
Address Data Conversion Functions . . . . . . . . . . . . .
inet_aton Function (Unix, Win32) . . . . . . . . . . .
Client/Server Programming . . . . . . . . . . . . . . . . . .
Server Methods . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

103
104
104
106
108
108
109
109
110
111
112
112
113
113

. .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. 115
. . 115
. . 115
. . 115
. . 116
. . 117
. . 117
. . 120
. . 120
. . 121
. . 121
. . 121
. . 121
. . 122
. . 123
. . 124
. . 124
. . 125
. . 126
. . 127
. . 128
. . 129
. . 129
. . 129
. . 131
. . 132
. . 133
. . 133
. . 134
. . 134
. . 134

v

Contents

Clients . . . . . . . . . . . . . .
Byte Ordering. . . . . . . . . . . . . .
Creating a Server . . . . . . . . . . . .
TCP. . . . . . . . . . . . . . . .
UDP . . . . . . . . . . . . . . .
Simple Echo TCP Server . . . .
main Function . . . . . . .
InitSockets Function . . .
ServerProcess Function .
Simple Echo UDP Server . . . .
InitSockets Function . . .
ServerProcess Function .
Creating a Client . . . . . . . . . . . .
TCP. . . . . . . . . . . . . . . .
UDP . . . . . . . . . . . . . . .
Simple Echo TCP Client . . . .
main Function . . . . . . .
InitSockets Function . . .
ClientProcess Function. .
Simple Echo UDP Client . . . .
InitSockets Function . . .
ClientProcess Function. .
Running the Simple Echo Application.
Summary . . . . . . . . . . . . . . . .

vi

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

135
136
136
137
138
139
142
142
145
146
148
149
150
150
151
151
153
154
155
157
159
159
160
161

Chapter 6 I/O Operations . . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . .
Detecting Network Events . . . . . . . . . . .
select (Unix, Win32). . . . . . . . . . . .
Macros . . . . . . . . . . . . . . . . . . .
WSAAsyncSelect (Win32) . . . . . . . .
WSAEventSelect (Win32). . . . . . . . .
WSAWaitForMultipleEvents (Win32) . .
Event Object . . . . . . . . . . . . . . . .
Multithreading . . . . . . . . . . . . . . . . . .
What Is Multithreading? . . . . . . . . .
CreateThread (Win32) . . . . . . . . . .
pthread_create (Unix) . . . . . . . . . . .
I/O Strategy . . . . . . . . . . . . . . . . . . . .
Blocking I/O . . . . . . . . . . . . . . . .
Non-blocking I/O . . . . . . . . . . . . .
Signal-driven I/O . . . . . . . . . . . . .
Multiplexing I/O . . . . . . . . . . . . . .
I/O Control . . . . . . . . . . . . . . . . . . . .
ioctl (Unix)/ioctlsocket (Win32) . . . . .
setsockopt/getsockopt (Unix, Win32) . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. 163
. . 163
. . 163
. . 163
. . 164
. . 165
. . 165
. . 166
. . 166
. . 167
. . 167
. . 168
. . 169
. . 169
. . 169
. . 170
. . 170
. . 171
. . 172
. . 172
. . 173

Contents

shutdown (Unix, Win32)
Broadcasting . . . . . . . . . .
Searching for Servers . .
Broadcast Function . . .
Summary . . . . . . . . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

174
175
175
176
177

Tutorial 1 Using 2DLIB . . . . . . . . . . . . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Configuring Visual Studio . . . . . . . . . . . . . . . . . . . . . .
Creating a Skeleton Project . . . . . . . . . . . . . . . . . . . . .
Creating the Workspace. . . . . . . . . . . . . . . . . . . .
Adding the Static Libraries . . . . . . . . . . . . . . . . . .
Adding the Source File . . . . . . . . . . . . . . . . . . . .
Creating a Basic Windowed Application with 2DLIB . . . .
The WinMain Function . . . . . . . . . . . . . . . . .
The Windows Procedure . . . . . . . . . . . . . . . .
The Complete Code . . . . . . . . . . . . . . . . . .
Using the 2DLIB Graphics Routines . . . . . . . . . . . . . . . .
2D Positions on the Screen . . . . . . . . . . . . . . . . . .
Use of Colors . . . . . . . . . . . . . . . . . . . . . . . . .
Plotting a Single Pixel. . . . . . . . . . . . . . . . . . . . .
Drawing a Line . . . . . . . . . . . . . . . . . . . . . . . .
Drawing a Rectangle/Filled Rectangle . . . . . . . . . . . .
Drawing a Triangle/Filled Triangle . . . . . . . . . . . . . .
Graphic Loading Functions . . . . . . . . . . . . . . . . . .
Graphics Display (Blitting) Function . . . . . . . . . . . . .
Keyboard Input Method . . . . . . . . . . . . . . . . . . . .
2DLIB Example 1 — Moving Primitives with the Cursor Keys .
Complete Code Listing for Example 1 . . . . . . . . . . . .
2DLIB Example 2 — Loading and Rotating Graphics . . . . . . .
Complete Code Listing for Example 2 . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. 181
. . 181
. . 181
. . 182
. . 182
. . 183
. . 183
. . 184
. . 184
. . 185
. . 187
. . 190
. . 190
. . 190
. . 191
. . 191
. . 191
. . 191
. . 192
. . 192
. . 193
. . 195
. . 197
. . 200
. . 201
. . 203

Tutorial 2 Creating Your Network Library . .
Introduction . . . . . . . . . . . . . . . . . . . .
Why Create a Network Library of Our Own? . .
Planning the Structure . . . . . . . . . . . . . .
Planning the Functionality . . . . . . . . . . . .
Identifying Hosts . . . . . . . . . . . . .
Sending Data to Hosts . . . . . . . . . .
Pinging — Calculating Network Latency
Timing Out . . . . . . . . . . . . . . . . .
Building the Library . . . . . . . . . . . . . . .
Windows . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

. 205
. . 205
. . 206
. . 206
. . 207
. . 207
. . 208
. . 209
. . 209
. . 209
. . 210

Part II — Tutorials

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

. .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .

vii

Contents

Unix/Linux . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Independent Code . . . . . . . . . . . . . . . . . . . . .
Creating Definitions for Data Types . . . . . . . . . . . . .
Log System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
StartLogConsole Function . . . . . . . . . . . . . . . . . .
dreamConsole Constructor . . . . . . . . . . . . . . . . . .
dreamConsole Destructor . . . . . . . . . . . . . . . . . .
println Function . . . . . . . . . . . . . . . . . . . . . . . .
StartLog Function . . . . . . . . . . . . . . . . . . . . . . .
LogString Function . . . . . . . . . . . . . . . . . . . . . .
StopLog Function . . . . . . . . . . . . . . . . . . . . . . .
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Up Source and Header Files . . . . . . . . . . . . .
dreamSock.h File . . . . . . . . . . . . . . . . . . . .
dreamMessage Class . . . . . . . . . . . . . . . . . .
dreamClient Class . . . . . . . . . . . . . . . . . . .
dreamServer Class . . . . . . . . . . . . . . . . . . .
Global Setup Functions . . . . . . . . . . . . . . . . . . . .
dreamSock_Initialize . . . . . . . . . . . . . . . . . .
dreamSock_InitializeWinSock . . . . . . . . . . . . .
dreamSock_Shutdown . . . . . . . . . . . . . . . . .
Global Socket Functions . . . . . . . . . . . . . . . . . . .
dreamSock_Socket Function . . . . . . . . . . . . . .
dreamSock_SetNonBlocking Function . . . . . . . .
dreamSock_SetBroadcasting Function . . . . . . . .
dreamSock_StringToSockaddr Function. . . . . . . .
dreamSock_OpenUDPSocket Function . . . . . . . .
dreamSock_CloseSocket Function. . . . . . . . . . .
dreamSock_GetPacket Function . . . . . . . . . . . .
dreamSock_SendPacket Function . . . . . . . . . . .
dreamSock_Broadcast Function . . . . . . . . . . . .
dreamSock_GetCurrentSystemTime Function . . . .
dreamSock_Linux_GetCurrentSystemTime Function
dreamSock_Win_GetCurrentSystemTime Function .
Retrieving Error Values . . . . . . . . . . . . . . . . . . . .
Summary of Global Functions . . . . . . . . . . . . . . . .
Creating dreamSock Network Library . . . . . . . . . . . . . . .
dreamMessage Class Member Variables. . . . . . . . . . .
dreamMessage Class Functionality . . . . . . . . . . . . .
Init Function . . . . . . . . . . . . . . . . . . . . . .
Clear Function . . . . . . . . . . . . . . . . . . . . .
GetNewPoint Function . . . . . . . . . . . . . . . . .
AddSequences Function . . . . . . . . . . . . . . . .
Write Function . . . . . . . . . . . . . . . . . . . . .
WriteByte Function. . . . . . . . . . . . . . . . . . .
WriteShort Function . . . . . . . . . . . . . . . . . .

viii

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

211
212
212
213
214
215
215
215
216
217
218
219
219
220
225
226
228
229
229
230
231
232
238
239
239
240
241
243
243
244
245
246
246
247
248
248
248
249
249
253
253
254
255
255
255
256

Contents

WriteLong Function . . . . . . . . . . . .
WriteFloat Function . . . . . . . . . . . .
WriteString Function . . . . . . . . . . . .
BeginReading Function. . . . . . . . . . .
Read Function. . . . . . . . . . . . . . . .
ReadByte Function . . . . . . . . . . . . .
ReadShort Function . . . . . . . . . . . .
ReadLong Function . . . . . . . . . . . . .
ReadFloat Function . . . . . . . . . . . . .
ReadString Function . . . . . . . . . . . .
dreamMessage Summary . . . . . . . . .
dreamClient Class Member Variables . . . . . .
dreamClient Class Functionality . . . . . . . . .
dreamClient Constructor. . . . . . . . . .
dreamClient Destructor . . . . . . . . . .
Initialize Function. . . . . . . . . . . . . .
Uninitialize Function . . . . . . . . . . . .
Reset Function . . . . . . . . . . . . . . .
DumpBuffer Function . . . . . . . . . . .
System Messages vs. User Messages. . .
SendConnect Function . . . . . . . . . . .
SendDisconnect Function . . . . . . . . .
SendPing Function . . . . . . . . . . . . .
ParsePacket Function. . . . . . . . . . . .
GetPacket Function. . . . . . . . . . . . .
SendPacket Function (Internal Message) .
SendPacket Function (External Message)
dreamClient Summary . . . . . . . . . . .
dreamServer Class Member Variables . . . . . .
dreamServer Class Functionality . . . . . . . . .
dreamServer Constructor . . . . . . . . .
dreamServer Destructor . . . . . . . . . .
Initialize Function. . . . . . . . . . . . . .
Uninitialize Function . . . . . . . . . . . .
SendAddClient Function . . . . . . . . . .
SendRemoveClient Function. . . . . . . .
SendPing Function . . . . . . . . . . . . .
AddClient Function . . . . . . . . . . . . .
RemoveClient Function . . . . . . . . . .
ParsePacket Function. . . . . . . . . . . .
CheckForTimeout Function . . . . . . . .
GetPacket Function. . . . . . . . . . . . .
SendPackets Function . . . . . . . . . . .
dreamServer Summary. . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

256
256
257
257
257
258
258
259
259
259
260
260
262
268
268
268
269
270
270
270
271
272
272
273
275
276
278
279
280
280
289
289
290
290
290
292
293
293
295
297
299
301
303
304
304

ix

Contents

Tutorial 3 Creating a Basic Network Application with
dreamSock . . . . . . . . . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . .
Planning the Functionality . . . . . . . . . . . . . . . . . .
Catching Exceptions . . . . . . . . . . . . . . . . .
Creating a Basic Client Application . . . . . . . . . . . . .
signin.h File . . . . . . . . . . . . . . . . . . . . . .
CSignin Class . . . . . . . . . . . . . . . . . . . . .
network.h File . . . . . . . . . . . . . . . . . . . . .
main.h File . . . . . . . . . . . . . . . . . . . . . . .
common.h File . . . . . . . . . . . . . . . . . . . . .
main.cpp File. . . . . . . . . . . . . . . . . . . . . .
Global Variables . . . . . . . . . . . . . . . . . . . .
CreateAccountDialogProc Function . . . . . . . . .
WinMain Function . . . . . . . . . . . . . . . . . . .
signin.cpp File — CSignin Class Methods . . . . . .
CSignin Constructor . . . . . . . . . . . . . .
CSignin Destructor . . . . . . . . . . . . . . .
ReadPackets Function . . . . . . . . . . . . .
AddClient Function . . . . . . . . . . . . . . .
RemoveClient Function . . . . . . . . . . . .
RemoveClients Function . . . . . . . . . . . .
SendSignIn Function . . . . . . . . . . . . . .
SendKeepAlive Function . . . . . . . . . . . .
Connect Function . . . . . . . . . . . . . . . .
Disconnect Function . . . . . . . . . . . . . .
RunNetwork Function . . . . . . . . . . . . .
Creating a Basic Server Application. . . . . . . . . . . . .
signin.h File . . . . . . . . . . . . . . . . . . . . . .
CSigninServer Class . . . . . . . . . . . . . . . . .
network.h File . . . . . . . . . . . . . . . . . . . . .
common.h File . . . . . . . . . . . . . . . . . . . . .
main.cpp File. . . . . . . . . . . . . . . . . . . . . .
Global Variables . . . . . . . . . . . . . . . . . . . .
WindowProc Function . . . . . . . . . . . . . . . . .
WinMain Function . . . . . . . . . . . . . . . . . . .
daemonInit Function . . . . . . . . . . . . . . . . .
keyPress Function . . . . . . . . . . . . . . . . . . .
main Function . . . . . . . . . . . . . . . . . . . . .
signin.cpp File — CSigninServer Class Methods . .
CSigninServer Constructor . . . . . . . . . .
CSigninServer Destructor . . . . . . . . . . .
InitNetwork Function . . . . . . . . . . . . .
ShutdownNetwork Function . . . . . . . . . .
ReadPackets Function . . . . . . . . . . . . .
SendExitNotification Function . . . . . . . . .

x

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. 305
. . 305
. . 306
. . 306
. . 306
. . 307
. . 308
. . 309
. . 309
. . 310
. . 310
. . 316
. . 316
. . 319
. . 323
. . 329
. . 329
. . 329
. . 333
. . 334
. . 335
. . 336
. . 336
. . 337
. . 337
. . 337
. . 338
. . 338
. . 339
. . 340
. . 340
. . 341
. . 347
. . 347
. . 347
. . 351
. . 352
. . 353
. . 355
. . 361
. . 361
. . 362
. . 362
. . 362
. . 366

Contents

AddClient Function . . . .
RemoveClient Function .
RemoveClients Function .
RunNetwork Function . .
Summary . . . . . . . . . . . . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

366
367
368
369
369

Tutorial 4 Creating the Game Lobby . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . .
Creating the Lobby Client Application . . . . . . . . . .
Creating the Dialogs . . . . . . . . . . . . . . . . . . . .
Lobby Dialog . . . . . . . . . . . . . . . . . . . . .
Create Game Dialog . . . . . . . . . . . . . . . . .
Create View Players Dialog. . . . . . . . . . . . .
Join Game Dialog . . . . . . . . . . . . . . . . . .
Lobby System Code . . . . . . . . . . . . . . . . . . . .
Lobby Client Code . . . . . . . . . . . . . . . . . . . . .
lobby.h File . . . . . . . . . . . . . . . . . . . . . .
network.h File . . . . . . . . . . . . . . . . . . . .
main.h File . . . . . . . . . . . . . . . . . . . . . .
main.cpp File. . . . . . . . . . . . . . . . . . . . .
CreateViewPlayersDialogProc Function . .
CreateGameDialogProc Function . . . . . .
JoinGameDialogProc Function . . . . . . . .
LoginDialogProc Function . . . . . . . . . .
LobbyDialogProc Function . . . . . . . . . .
WinMain Function . . . . . . . . . . . . . .
lobby.cpp File — CLobby Class Methods . . . . .
RefreshPlayerList Function . . . . . . . . .
ReadPackets Function . . . . . . . . . . . .
RequestGameData Function . . . . . . . . .
SendChat Function . . . . . . . . . . . . . .
SendCreateGame Function. . . . . . . . . .
SendRemoveGame Function . . . . . . . . .
SendStartGame Function. . . . . . . . . . .
Connect Function . . . . . . . . . . . . . . .
Disconnect Function . . . . . . . . . . . . .
RunNetwork Function . . . . . . . . . . . .
Unimplemented Functions . . . . . . . . . .
Lobby Server Code . . . . . . . . . . . . . . . . . . . . .
lobby.h File . . . . . . . . . . . . . . . . . . . . . .
network.h File . . . . . . . . . . . . . . . . . . . .
main.cpp File. . . . . . . . . . . . . . . . . . . . .
lobby.cpp File — CLobbyServer Class Methods .
ReadPackets Function . . . . . . . . . . . .
Unimplemented Functions . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. 371
. . 371
. . 371
. . 372
. . 372
. . 375
. . 376
. . 376
. . 377
. . 377
. . 377
. . 379
. . 379
. . 380
. . 380
. . 380
. . 382
. . 382
. . 384
. . 386
. . 389
. . 398
. . 399
. . 401
. . 402
. . 402
. . 402
. . 403
. . 403
. . 403
. . 404
. . 405
. . 406
. . 406
. . 407
. . 407
. . 408
. . 418
. . 425
. . 425

xi

Contents

Tutorial 5 Creating Your Online Game . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . .
Designing the Functionality . . . . . . . . . . . . . . . . .
Frame Time . . . . . . . . . . . . . . . . . . . . . .
Compressing Messages . . . . . . . . . . . . . . . .
Dead Reckoning . . . . . . . . . . . . . . . . . . . .
Frame History . . . . . . . . . . . . . . . . . . . . .
Handling Messages . . . . . . . . . . . . . . . . . .
Game Server Code . . . . . . . . . . . . . . . . . . . . . .
server.h File . . . . . . . . . . . . . . . . . . . . . .
network.h File . . . . . . . . . . . . . . . . . . . . .
main.cpp File. . . . . . . . . . . . . . . . . . . . . .
network.cpp File — CArmyWarServer Class Part 1
InitNetwork Function . . . . . . . . . . . . .
ReadPackets Function . . . . . . . . . . . . .
SendCommand Function . . . . . . . . . . . .
ReadDeltaMoveCommand Function . . . . . .
BuildMoveCommand Function. . . . . . . . .
BuildDeltaMoveCommand Function. . . . . .
server.cpp File — CArmyWarServer Class Part 2 .
GenerateRandomMap Function . . . . . . . .
CalculateVelocity Function . . . . . . . . . . .
CalculateHeading Function. . . . . . . . . . .
CalculateBulletVelocity Function . . . . . . .
MovePlayer Function. . . . . . . . . . . . . .
CheckFlagCollisions Function . . . . . . . . .
Frame Function . . . . . . . . . . . . . . . . .
lobby.cpp File . . . . . . . . . . . . . . . . . . . . .
AddGame Function . . . . . . . . . . . . . . .
RemoveGame Function . . . . . . . . . . . .
RemoveGames Function . . . . . . . . . . . .
Summary of Server Code . . . . . . . . . . . . . . .
Game Client Code . . . . . . . . . . . . . . . . . . . . . .
client.h File . . . . . . . . . . . . . . . . . . . . . .
network.h File . . . . . . . . . . . . . . . . . . . . .
common.h File . . . . . . . . . . . . . . . . . . . . .
main.cpp File. . . . . . . . . . . . . . . . . . . . . .
VectorLength and VectorSubtract Functions .
ApplicationProc Function. . . . . . . . . . . .
Dialog Procedures . . . . . . . . . . . . . . .
Main Loop. . . . . . . . . . . . . . . . . . . .
network.cpp File . . . . . . . . . . . . . . . . . . . .
StartConnection Function . . . . . . . . . . .
SendCommand Function . . . . . . . . . . . .
SendStartGame Function. . . . . . . . . . . .
SendRequestNonDeltaFrame Function . . . .

xii

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. 427
. . 427
. . 428
. . 428
. . 428
. . 429
. . 430
. . 430
. . 430
. . 431
. . 436
. . 436
. . 437
. . 449
. . 449
. . 451
. . 452
. . 453
. . 454
. . 458
. . 467
. . 468
. . 469
. . 470
. . 472
. . 475
. . 477
. . 478
. . 478
. . 479
. . 481
. . 481
. . 481
. . 482
. . 487
. . 488
. . 488
. . 488
. . 489
. . 489
. . 489
. . 490
. . 502
. . 502
. . 504
. . 504

Contents

Connect Function . . . . . . . . . . . . . . . . . . .
Disconnect Function . . . . . . . . . . . . . . . . .
ReadMoveCommand Function . . . . . . . . . . . .
ReadDeltaMoveCommand Function . . . . . . . . .
BuildDeltaMoveCommand Function. . . . . . . . .
RunNetwork Function . . . . . . . . . . . . . . . .
client.cpp File . . . . . . . . . . . . . . . . . . . . . . . .
CArmyWar Constructor and Destructor Functions.
InitializeEngine Function. . . . . . . . . . . . . . .
Shutdown Function . . . . . . . . . . . . . . . . . .
DrawMap Function . . . . . . . . . . . . . . . . . .
Frame Function . . . . . . . . . . . . . . . . . . . .
CheckVictory Function . . . . . . . . . . . . . . . .
KillPlayer Function . . . . . . . . . . . . . . . . . .
GetClientPointer Function . . . . . . . . . . . . . .
CheckKeys Function . . . . . . . . . . . . . . . . .
CheckPredictionError Function . . . . . . . . . . .
CheckBulletPredictionError Function. . . . . . . .
CalculateVelocity Function . . . . . . . . . . . . . .
CalculateHeading Function. . . . . . . . . . . . . .
PredictMovement Function . . . . . . . . . . . . .
MoveObjects Function . . . . . . . . . . . . . . . .
lobby.cpp File . . . . . . . . . . . . . . . . . . . . . . . .
RefreshGameList Function. . . . . . . . . . . . . .
RefreshJoinedPlayersList Function . . . . . . . . .
Other Unimplemented Functions . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

504
505
505
506
508
509
511
525
526
527
528
530
531
532
532
532
533
534
535
536
537
539
541
541
541
542
542

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543

xiii

About the Authors
Andrew Mulholland has a BSc (Hons) in Computer Games Technology and is a partner in a
games development company based in Scotland
called Hunted Cow Studios (www.huntedcow.com). The company’s current project is an
online gaming web site called CowPlay.com,
which currently offers free multiplayer games.

Andrew Mulholland

Teijo Hakala is a software engineer
from Jyväskylä, Finland, who specializes in network programming, game
programming, and optimization. He
also has wide work experience with
computer technology.

Teijo Hakala

xiv

Introduction
With Internet technology developing rapidly and the use of broadband
Internet services increasingly common, Internet computer gaming has
become ever more popular, while documentation on how to develop
Internet games remains inadequate. Programming Multiplayer Games
provides in-depth coverage of all the major topics associated with
online game programming, as well as giving the programmer easy to
follow, step-by-step tutorials on how to create a fully functional network
library, back-end MySQL database, and a complete, working online
game.
The book contains two main parts. The first explains practical theory on how to utilize MySQL, PHP4, sockets, and basic Windows
programming. The second part consists of five extensive tutorials, leading you through the stages of creating a working online game, which
you can both learn from and expand upon.
After reading this book, you will have a solid knowledge of online
game programming and you will also be able to start making your own
online games. Also note that the companion CD contains all the source
code from the book and a ready-to-use version of the network library
you will create in the tutorial section.
We hope you enjoy reading and learning from this book as much as
we have enjoyed writing it!

xv

This page intentionally left blank.

Part I

Theory
The theory section of this book is full of practical information that will
help you understand how to make functional online games. We recommend that you read through this section thoroughly before attempting
the tutorial section, as there is a lot of knowledge that will benefit you
here.
This section first covers the basics of dialog-based Windows programming, which we will utilize in the tutorial section to create our
login and lobby system for the sample online game. Then we cover how
to use MySQL and PHP to create a back-end database for your game,
allowing you to interact with game data directly from a web browser.
We also give an introduction to TCP/IP and sockets, followed by how to
get started with sockets programming. Finally, we learn about different
ways to send data and how to modify the behavior of our sockets.

1

This page intentionally left blank.

Chapter 1

Introduction to
Windows
Programming
Introduction
The most essential knowledge anyone can have is the basics. If you
already know how to create dialog-based Windows applications, you can
skip this chapter, but if you do not, this chapter will give you a quick
and easy introduction so that you will find the rest of this book more
accessible.

Windows Messaging System
Windows controls everything through the use of its messaging system.
This is a fundamental idea to grasp if you wish to create any Windowsbased applications. Tasks to be processed by the operating system are
stored in a queue. For example, when a user clicks a button in a window, a message is added to the queue and is then sent to the
appropriate window to inform it that the button has been pressed.

3

4

Chapter 1 / Introduction to Windows Programming

When the operating system creates a window, the window continually checks for messages being sent to it. If it receives a relevant
message, it will react accordingly; otherwise, it will send it back to the
operating system to be reprocessed.
Each window created is assigned a unique handle that is used to control and determine which messages are relevant to that window. In
code, this is defined as the HWND, or window handle.
The main reason behind this system is to allow for multitasking and
multithreading. This means that the operating system can run more
than one application in one instance, even though a processor can only
handle one task at a time.
There is a lot more to windows than this, but this should give you a
reasonable overview of how the system works.

Creating a Window
Load up Microsoft Visual Studio and select File, New…
The following dialog box is now visible in the center of the screen.
Figure 1-1

Select the Projects tab at the top of the dialog and then choose the
Win32 Application option on the main display. Select the location for
your project, enter your project’s name, and click OK.
Next, select the type of project you wish to create. Leave it on the
default option (An empty project) and click the Finish button. A project
information box is now visible; simply click OK in this box.
Now we are working with the Visual Studio main interface. Currently the ClassView is active, but we are interested in the FileView, so
select this tab.

Chapter 1 / Introduction to Windows Programming

5

Figure 1-2

The FileView is a list of all the C and C++ source and header files that
are active in your project. Currently we do not have any files in our project, so we need to add our main C++ source file.
Select File, New… as you did before, but this time we will be using
the Files tab instead of the Projects tab. The following dialog will be
visible.
Figure 1-3

Select the C++ Source File option as shown in Figure 1-3 and type in
the filename as main.cpp. Now click the OK button to add this empty
file to your project.
You now have your main source file in your project and it is visible in
the Visual Studio editor.
There are two main items required in a standard Windows program:
the entry point to your program, which is named WinMain, and the
Windows callback procedure, commonly named WndProc, which is
used to keep your Windows application up to date.
For what we require though, it is best to take the dialog approach,
making it even simpler to design and code. First, we need to add our
dialog, so click File, New… again, but this time you want to add a
resource script. Type in the filename as resource and click OK.
Once this is done, you will notice another tab has appeared between
the ClassView and FileView tabs. This tab is called the ResourceView;
it allows you to visually create and edit dialogs for use within your
program.

6

Chapter 1 / Introduction to Windows Programming

Figure 1-4

Once you select the ResourceView tab, you will be presented with the
resource editor. Right-click on resource.rc in the main view and then
left-click on the Insert option. You will then be presented with the following dialog box.
Figure 1-5

Select the Dialog option and click the New button. Now you will see a
sample dialog box in front of you. For now, we will not do much to it
except change the name of the title bar and its identifier, which I
explain in the following code.
Double-click on the sample dialog box that Visual Studio created.
Now a dialog properties box can be seen. All we are interested in here
is the ID, which will probably be set to IDD_DIALOG1, and the Caption, which should be Dialog. Let’s change the ID to IDD_CLIENT and
the Caption to Window Example.
It’s time to go back and do some code now. We have our dialog template that we can call from our code, so let’s do it. Here is the code
required to make your dialog window appear on the screen. The OK
button on the dialog can be pressed but will have no action, whereas
the Cancel button will close the dialog.
// Simple Windows Code
#include
#include "resource.h"
LRESULT CALLBACK
ClientDlgProc(HWND DialogWindow, UINT Message, WPARAM wParam, LPARAM lParam)
{
// Process Messages
switch(Message)
{
case WM_INITDIALOG:
return FALSE;
case WM_COMMAND:

Chapter 1 / Introduction to Windows Programming

7

switch(wParam)
{
case IDCANCEL:
EndDialog(DialogWindow, FALSE);
return FALSE;
default:
break;
}
break;
default:
break;
}
return FALSE;
}
int APIENTRY
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow)
{
DialogBox((HINSTANCE) hInstance, MAKEINTRESOURCE(IDD_CLIENT), NULL,
(DLGPROC) ClientDlgProc);
return 0;
}

NOTE If you get an error that tells you it can’t find afxres.h, you
need to install MFC support for Visual Studio, which comes with the
Visual Studio package.

If you have never seen Windows code before, the above code may look
complex and a little confusing. Welcome to the world of Windows! Well,
it’s not that bad, honest.
Let’s start with the WinMain function. This is simply the point at
which Windows starts executing your code. Do not worry about the
variables that are passed in; they are controlled by Windows and are
beyond the scope of this book.
The main issue here is the DialogBox function and the
ClientDlgProc callback procedure that creates our dialog window
on the screen. The first parameter is the instance of the application that
you simply take from the first parameter of the WinMain function.
Next is the identifier that we set when we created the template for our
dialog. The third parameter is of no interest to us so we set it to NULL,
but the final one is. This is a pointer to the update function for the dialog. Each dialog you create requires this update function (basically the
same idea as a Windows procedure). In this update function is where
you set the actions for buttons and other useful tools. So we set this
update function to our callback function for the dialog
(ClientDlgProc).

8

Chapter 1 / Introduction to Windows Programming

For example, the identifier for the Cancel button is IDCANCEL. As
you can see in the code, there is a case statement for the Cancel button
so when it is clicked, it will close the dialog window. Other buttons can
be easily added to the template using the toolbox on the template editor. Just remember that each button must contain a unique ID so you
can reference it from within your code.

Sending Information to Your Window
In addition to being useful for debugging, being able to update information to a window is essential knowledge and can be used in many
situations, such as displaying how many players are connected to the
game server.
First you have to add a static text string to the dialog window. To do
this you need to go back to the template editor by selecting the
ResourceView tab as before. Then you simply double-click on the
IDD_CLIENT text as seen in Figure 1-6 to bring up your dialog in the
main area.
Next, select the Aa button from the Controls toolbox and place it
somewhere on your dialog as seen in the figure. Now double-click on
the text you added to the dialog box to display its properties.
Figure 1-6

The following dialog will now be visible on the screen. All we need to
change here is the ID. Change the text IDC_STATIC to IDC_
SERVERSTATUS. This will give it more meaning when it comes to
adding it into the code.
Figure 1-7

Chapter 1 / Introduction to Windows Programming

9

Now that we have some text, we want to be able to set it to a value
from within our code. For example, if we want the text to read “Server
Online,” add this line of code after the line that contains “case
WM_INITDIALOG”:
SendDlgItemMessage(DialogWindow,IDC_SERVERSTATUS,
WM_SETTEXT,NULL,(long)"Server Online");

Then when the dialog box is initialized, Windows will send a message
to the dialog box to tell it to update the IDC_SERVERSTATUS text
with the string you supplied in the function. In this case, it would
update the text from “Static” to “Server Online.”
The first parameter is the handle to the dialog, which is the first
variable that is passed into the dialog update procedure. The second is
the identifier for what you want to change or update. Next comes the
command that you wish to send. There are many commands and the
best way to figure out how they work is just to experiment with them.
For now we are using WM_SETTEXT, which tells Windows to change
a static text string in a dialog box. The fourth variable is not used for
the WM_SETTEXT command, so we simply set it to NULL. The final
variable is used to declare the string that we want to update the static
text with, in this case “Server Online.” Also note that the string must
be typecast to a long.
TIP

Try experimenting with editable text. It works on the same principles, and you simply send a WM_GETTEXT message to retrieve
what the user entered.

Static Link Libraries
Later when we create our online tutorial game, we will be using static
link libraries to encapsulate all our network and graphics code, which
makes it easier to reuse for future projects. As well as the reusability
factor, static link libraries also protect your source code while allowing
others to use the functionality of your code.
When creating a static link library, a WinMain function or a Windows update function (WndProc) are not required. This is because we
are not actually creating a program but rather just a collection of functions that we can use from within our programs. The library is created
using standard C/C++ source and header files, but the output is a
library rather than a Windows executable.
When you compile your library, the compiler will generate a library
file with the extension .lib. To use the functions in your library, you are
required to include it in your project and include the header file that

10

Chapter 1 / Introduction to Windows Programming

was used to create the library (which contains all the external variables,
types, and function prototypes).
The easiest way to use your library is to create Lib and Include
folders for your library and include those directories in the Visual Studio directory settings that are explained