Tải bản đầy đủ (.pdf) (609 trang)

Developer guide for multiplayer game

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (12.93 MB, 609 trang )


Developer’s Guide to
Multiplayer Games

Andrew Mulholland
and

Teijo Hakala

Wordware Publishing, Inc.


Library of Congress Cataloging-in-Publication Data
Mulholland, Andrew.
Developer’s guide to multiplayer games / by Andrew Mulholland and Teijo Hakala.
p. cm.
ISBN 1-55622-868-6 (pbk.)
1. Computer games--Programming. I. Hakala, Teijo. II. Title.
QA76.76.C672 M85
795.8'15--dc21

2001
2001046789
CIP

© 2002, 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-868-6
10 9 8 7 6 5 4 3 2 1
0109

OpenGL is a registered trademark of Silicon Graphics, Inc.
Windows is a registered trademark of Microsoft Corporation.
Other product names mentioned are used for identification purposes only and may be trademarks of their respective companies.

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
Theory Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1 Creating Windows Applications in Visual Studio . . . . . . . . 3
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Windows Messaging System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Creating a Window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Sending Information to Your Window . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Introduction to Static Link Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Creating a Static Link Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Using a Static Link Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Final Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Chapter 2 Internet-Based Database Systems . . . . .
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . .

Installing MySQL. . . . . . . . . . . . . . . . . . . . . . . .
Linux Specific . . . . . . . . . . . . . . . . . . . . . .
Windows Specific . . . . . . . . . . . . . . . . . . . .
Overview of MySQL . . . . . . . . . . . . . . . . . . . . . .
Creating a Database in MySQL . . . . . . . . . . . . . . . .
Adding Tables to a Database. . . . . . . . . . . . . . .
Adding Data to Tables . . . . . . . . . . . . . . . . . .
Viewing Data in a Table . . . . . . . . . . . . . . . . .
Adding Extra Fields to Tables . . . . . . . . . . . . . .
Updating Data in a Field . . . . . . . . . . . . . . . . .
Ordering Output . . . . . . . . . . . . . . . . . . . . .
Retrieving the Last Data Entered . . . . . . . . . . .
Limiting Output Data . . . . . . . . . . . . . . . . . .
Deleting Data from a Table . . . . . . . . . . . . . . .
Deleting Tables and Databases . . . . . . . . . . . . .
Relational Databases . . . . . . . . . . . . . . . . . . . . . .
Creating Our Relational Database . . . . . . . . . . .
Player Data . . . . . . . . . . . . . . . . . . . . .
Payment Info . . . . . . . . . . . . . . . . . . . .
Notes . . . . . . . . . . . . . . . . . . . . . . . .
Rel Friends . . . . . . . . . . . . . . . . . . . . .
Rel Enemies . . . . . . . . . . . . . . . . . . . .
Adding Relational Data . . . . . . . . . . . . . . . . .
Manipulating the Friend and Enemy Relational Tables

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

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

.
.
.
.
.
.
.
.

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

.
.
.
.
.
.
.
.
.
.
.

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

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

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

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

.
.
.
.
.
.
.
.
.

. . 15
. . . 15
. . . 16
. . . 16
. . . 17
. . . 18
. . . 18
. . . 19
. . . 22
. . . 24
. . . 28
. . . 30
. . . 36
. . . 38
. . . 40
. . . 40
. . . 43
. . . 46
. . . 47
. . . 48

. . . 50
. . . 52
. . . 53
. . . 55
. . . 57
. . . 70

iii


Contents
Other Methods of Data Input . . . . . . . . . . . . . . . . . . . . .
Text File Input . . . . . . . . . . . . . . . . . . . . . . . . . .
Native Database Input . . . . . . . . . . . . . . . . . . . . . .
Backup and Restoration of Databases . . . . . . . . . . . . . . . . .
Backup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Restoration . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MySQL C++ Interface . . . . . . . . . . . . . . . . . . . . . . . .
Example 1 — Connecting and Retrieving Data from MySQL.
Example 2 — Updating Data in MySQL from an Application .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.


73
73
75
77
77
81
83
84
87
90

Chapter 3 Communicating with the Internet . . . . . . . . . . . . . . . 91
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Debugging Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
More Text Output Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Using Files and Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Reading in a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Writing to a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Using Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Using Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Command Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Retrieving the Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Installing the Perl Database Interface (DBI) . . . . . . . . . . . . . . . . . . . . . 120
Connecting and Disconnecting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Retrieving and Displaying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Adding New Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Using the Unique ID Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
DisplayData Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

AddData Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
DoAddData Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Modify Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
DoModifyData Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
DeleteData Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Adding a Search Facility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Chapter 4 Introduction to TCP/IP . .
Introduction . . . . . . . . . . . . . . .
What is a Protocol? . . . . . . . . . . .
OSI Model . . . . . . . . . . . . . . . .
OSI Model Layers . . . . . . . .
Internet Protocol . . . . . . . . . . . .
Introduction to the Transport Layer . .
Transmission Control Protocol .
User Datagram Protocol . . . . .
Ports . . . . . . . . . . . . . . . . . . .

iv

.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

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

. .
. .

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

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

.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

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

.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

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

. . .

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

. 181
. . 181
. . 181
. . 182
. . 183
. . 184

. . 186
. . 186
. . 187
. . 187


Contents
Introduction to Sockets . . .
Socket Types . . . . .
Address . . . . . . . .
Platforms . . . . . . .
History of WinSock . .
Choosing the Platform
Summary . . . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.
.

188
189
189
190
190
191
191

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. . . . . . . . . . . . . . . . . . . . . .
Clients . . . . . . . . . . . . . . . . . . . . . . . . . . .
Byte Ordering . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Server . . . . . . . . . . . . . . . . . . . . . . . .
TCP . . . . . . . . . . . . . . . . . . . . . . . . . . . .
UDP . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simple Echo TCP Server . . . . . . . . . . . . . . . .

Main Function . . . . . . . . . . . . . . . . . . .
InitSockets Function . . . . . . . . . . . . . . . .

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

.
.
.
.
.
.
.
.
.
.
.

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

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

.
.
.
.
.
.
.
.

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

.

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

.
.
.
.
.
.
.
.
.
.
.

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

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

.
.
.
.
.
.
.
.

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

.

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

.
.
.
.
.
.
.
.
.
.
.

. 193
. . 193
. . 193
. . 193
. . 194
. . 195
. . 195
. . 198
. . 198
. . 199
. . 199
. . 199
. . 199
. . 200
. . 201
. . 202
. . 202
. . 203

. . 204
. . 205
. . 206
. . 207
. . 207
. . 207
. . 209
. . 210
. . 211
. . 212
. . 212
. . 212
. . 212
. . 214
. . 214
. . 215
. . 216
. . 217
. . 218
. . 221
. . 222

v


Contents
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

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

.

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

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

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

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

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

.
.
.

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

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


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

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

.
.
.
.

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

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

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

.
.
.
.

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

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


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

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

.
.
.

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

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

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

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

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

.

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

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

.
.

224
225
228
228
230
230
230
231
233
234
235
237
239
239
240
241

Chapter 6 I/O Operations . . . . . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . .
Detecting Network Events . . . . . . . . . . . . . . . . .
Select Function (Unix, Win32) . . . . . . . . . . . .
Macros . . . . . . . . . . . . . . . . . . . . .
WSAAsyncSelect Function (Win32) . . . . . . . . .
WSAEventSelect Function (Win32) . . . . . . . . .
WSAWaitForMultipleEvents Function (Win32). . .
Event Object . . . . . . . . . . . . . . . . . .
Multithreading . . . . . . . . . . . . . . . . . . . . . . .

What is Multithreading? . . . . . . . . . . . . . . .
CreateThread Function (Win32) . . . . . . . . . . .
Pthread_Create Function (Unix). . . . . . . . . . .
I/O Strategy . . . . . . . . . . . . . . . . . . . . . . . . .
Blocking I/O. . . . . . . . . . . . . . . . . . . . . .
Non-blocking I/O . . . . . . . . . . . . . . . . . . .
Signal-Driven I/O . . . . . . . . . . . . . . . . . . .
Multiplexing I/O . . . . . . . . . . . . . . . . . . .
I/O Control . . . . . . . . . . . . . . . . . . . . . . . . .
Ioctl (Unix)/IoctlSocket Function (Win32) . . . . .
SetSockOpt/GetSockOpt Function (Unix, Win32) .
Shutdown Function (Unix, Win32). . . . . . . . . .
Broadcasting. . . . . . . . . . . . . . . . . . . . . . . . .
Searching for Servers . . . . . . . . . . . . . . . .
Broadcast Function . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . .

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

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

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

.
.
.
.
.
.
.
.
.
.

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

.
.
.
.
.
.

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

. .
. .
. .

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


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

. .
. . .

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

.
.
.
.
.

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

.
.
.
.
.
.
.
.

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

. 243
. . 243
. . 243
. . 243
. . 244
. . 245
. . 245
. . 246
. . 247
. . 247
. . 248

. . 248
. . 249
. . 250
. . 250
. . 250
. . 251
. . 252
. . 252
. . 252
. . 253
. . 255
. . 256
. . 256
. . 256
. . 258


Contents

Tutorial Introduction . . . . . . . . . . . . . . . . . . . . . . . 259
Tutorial 1
Using Our 2D Library . . . . . . . . . . . . . . . .
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 . . . . . . . . . . . . . . . . . . .
Graphical Functions . . . . . . . . . . . . . . . . . . . . . . . .
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

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

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

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

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

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

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

. 261
. . 261
. . 261
. . 262
. . 262
. . 263
. . 263
. . 264
. . 264
. . 266
. . 268
. . 270
. . 270
. . 270
. . 271
. . 271
. . 271

. . 271
. . 272
. . 272
. . 273
. . 273
. . 275
. . 277
. . 280
. . 281
. . 283

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 . . . . . . . . . . .
Building the Library . . . . . . . . . . . . . . .
Windows. . . . . . . . . . . . . . . . . . .
Unix/Linux . . . . . . . . . . . . . . . . .
Creating Independent Code . . . . . . . . . . .
Creating Definitions for Data Types . . . .
Protocol Independence . . . . . . . . . . .
Transport Protocol Independence . .
Internet Protocol Independence . . .
Log System . . . . . . . . . . . . . . . . . . . .
Getting Started . . . . . . . . . . . . . . . . . .


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

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

.
.
.
.
.

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

. 285
. . 285
. . 285
. . 286
. . 287
. . 287

. . 287
. . 288
. . 288
. . 289
. . 290
. . 290
. . 291
. . 291
. . 292
. . 292
. . 294

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


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

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

.
.
.
.

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

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

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

.

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

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

. .
. .
. .

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

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

vii


Contents
Setting Up Source and Header Files . . . .
NET_Socket Class . . . . . . . . . . . . . .
Individual Setup Functions . . . . . . . . . .
Dependence . . . . . . . . . . . . . . . . . . . . .
NET_Socket Class Initialization Functions .
Constructor . . . . . . . . . . . . . . .
Destructor . . . . . . . . . . . . . . .
Shutdown . . . . . . . . . . . . . . . . . . .
Closing Connections . . . . . . . . . .
Closing Input Event Handle . . . . . .
Setting the Local Host Offline . . . . .
Setting Blocking/Non-Blocking . . . . . . .
Message System . . . . . . . . . . . . . . .
Sending/Receiving Data . . . . . . . . . . .
Read Function . . . . . . . . . . . . .
ReadFrom Function . . . . . . . . . .

Write Function . . . . . . . . . . . . .
WriteTo Function . . . . . . . . . . . .
WriteAll Function. . . . . . . . . . . .
Host ID System. . . . . . . . . . . . . . . .
Finding Next Free Host Index . . . . .
Giving Local ID . . . . . . . . . . . . .
Creating the Server . . . . . . . . . . . . .
CreateServer Function . . . . . . . . .
CreateWinServer Function . . . . . .
CreateTCPServer Function . . . . . .
CreateWinTCPServer Function . . . .
Accepting a Connection . . . . . . . . . . .
Connecting to the Server . . . . . . . . . .
ConnectToServer Function . . . . . .
CloseConnection Function . . . . . . .
Multithreading . . . . . . . . . . . . . . . .
Handling Network Events . . . . . . . . . .
WinHandleEvents Function . . . . . .
ProcessMessages Function . . . . . .
Message Recording. . . . . . . . . . . . . .
Complete Independent Version . . . . . . . . . .
NET_Socket Class . . . . . . . . . . . . . .
Constructor/Destructor . . . . . . . .
RemoteHost Class . . . . . . . . . . .
Initializing/Uninitializing . . . . . . . . . . .
Blocking/Non-blocking I/O . . . . . . . . . .
Sending/Receiving Data . . . . . . . . . . .
Read Function . . . . . . . . . . . . .
ReadFrom Function . . . . . . . . . .
Write Function . . . . . . . . . . . . .

WriteTo Function . . . . . . . . . . . .

viii

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

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

.
.
.
.
.
.
.
.

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

.
.
.
.
.
.
.
.
.

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

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

.
.

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

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

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

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

.
.
.

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

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

.
.
.
.
.
.
.
.

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

.
.
.
.
.
.
.
.
.

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

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

.
.

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

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

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

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

.
.
.

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

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

.
.
.
.
.
.
.
.

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

.
.
.
.
.
.
.
.
.

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

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

.
.

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

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

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

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

.
.
.

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

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

.
.
.
.
.
.
.
.

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

.
.
.
.
.
.
.
.
.

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

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

294
295

298
301
301
301
301
302
302
303
303
303
304
306
306
309
311
313
315
318
318
319
320
320
321
324
324
325
329
329
336
340

344
345
347
351
351
352
353
354
355
355
356
356
358
359
361


Contents
WriteAll Function. . . . . . . . . . . . . . . . . . . .
Message Buffer System . . . . . . . . . . . . . . . . . . .
Message Buffer Class . . . . . . . . . . . . . . . . .
ReadToBuffer Function. . . . . . . . . . . . . . . . .
ProcessMessageBuffer Function . . . . . . . . . . .
ProcessMessages Function . . . . . . . . . . . . . .
Buffered Function Calls . . . . . . . . . . . . . . . . . . .
WinHandleEvents Function (Win32) . . . . . . . . .
Platform-Dependent Functions . . . . . . . . . . . . . . .
NET_WinIOThreadFunc (Win32) . . . . . . . . . . .
NET_WinUDPServerIOThreadFunc (Win32) . . . .
NET_UnixIOThreadFunc (Unix) . . . . . . . . . . .

NET_UnixUDPServerIOThreadFunc (Unix) . . . . .
Give Local ID Number . . . . . . . . . . . . . . . . . . . .
GiveLocalIdTCP Function . . . . . . . . . . . . . . .
GiveLocalIdUDP Function . . . . . . . . . . . . . . .
Creating a Server . . . . . . . . . . . . . . . . . . . . . . .
CreateServer Function . . . . . . . . . . . . . . . . .
CreateWinServer Function . . . . . . . . . . . . . .
CreateUnixServer Function . . . . . . . . . . . . . .
CreateTCPServer and CreateUDPServer Functions.
CreateWinTCPServer Function . . . . . . . . . . . .
CreateUnixTCPServer Function . . . . . . . . . . .
NET_UnixPollAcceptFunc Function . . . . . . . . .
CreateWinUDPServer Function . . . . . . . . . . . .
CreateUnixUDPServer Function . . . . . . . . . . .
AcceptConnection Function . . . . . . . . . . . . . .
Connecting to a Server . . . . . . . . . . . . . . . . . . . .
ConnectToServer Function . . . . . . . . . . . . . .
CloseConnection Function . . . . . . . . . . . . . . .
Shutdown Function . . . . . . . . . . . . . . . . . . .
Final Thoughts . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Tutorial 3
Creating the Login System . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . .
Creating the Database . . . . . . . . . . . . . . . .
Creating Our Basic Client Application. . . . . . . .
Integrating the Login System (Part 1). . . . . . . .
Creating Our Dialogs . . . . . . . . . . . . . .
Coding the Login System . . . . . . . . . . .
Creating the Login Server . . . . . . . . . . . . . .

Messages . . . . . . . . . . . . . . . . . . . .
LobbyLogin Message. . . . . . . . . . .
LobbyLoginFeedback Message . . . . .
LobbyCreateAccount Message . . . . .
LobbyCreateAccountFeedback Message

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

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

. .
. .

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

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


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

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

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

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

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

.
.
.
.
.
.
.
.

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

.
.
.
.

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


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

.
.
.

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

.
.
.
.
.
.
.

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

.
.
.
.
.
.
.
.
.
.
.

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

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

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

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

.
.
.
.
.
.
.
.
.
.

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

362
364
365
365
366
370

372
372
373
373
375
383
386
390
390
391
394
394
395
398
399
400
400
401
402
403
404
406
406
413
416
416
417

. .
. .

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

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

.
.
.
.

.
.
.
.
.
.
.
.
.

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

.
.
.
.
.
.

.
.
.
.
.
.
.

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

. 419
. . 419
. . 419
. . 421
. . 429
. . 429
. . 433
. . 438

. . 439
. . 439
. . 440
. . 440
. . 441

ix


Contents

x

The Header File . . . . . . . . . . . . . . . .
The Main Server File. . . . . . . . . . . . . .
Server Application — Complete Code Listing
Integrating the Login System (Part 2). . . . . . . .
Client Application — Complete Code Listing
Executing the Login System . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.

441
442
453
462
472
483
486

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 . . . . . . . . . . . . .
Game Code . . . . . . . . . . . . . . . . . . .
Main.cpp/Main.h . . . . . . . . . . . . .
Netcommon.h . . . . . . . . . . . . . . .
Init.cpp . . . . . . . . . . . . . . . . . .
Chat System . . . . . . . . . . . . . . . . . .
TutChatClass . . . . . . . . . . . . . . .

SendMessage Function. . . . . . . . . .
Handling Chat Messages . . . . . . . . . . . .
HandleChatMessages Function . . . . .
Player Identification . . . . . . . . . . . . . .
Logging In. . . . . . . . . . . . . . . . . . . .
LoginToServer Function . . . . . . . . .
Game Lobby. . . . . . . . . . . . . . . . . . .
Lobby Dialog . . . . . . . . . . . . . . .
LogoutFromServer Function . . . . . .
Handling Remove Player Message . . . . . .
RemovePlayer Function . . . . . . . . .
LobbyRefreshPlayerList Function. . . .
Creating a New Game . . . . . . . . . . . . .
CreateGame Function . . . . . . . . . .
CreateGameDialogProc Function . . . .
DoCreateGame Function. . . . . . . . .
CreateViewPlayers Function . . . . . .
CreateViewPlayersDialogProc Function
DoDestroyGame Function . . . . . . . .
Joining a Game . . . . . . . . . . . . . . . . .
JoinGame Function . . . . . . . . . . . .
DoJoinGame Function . . . . . . . . . .
JoinGameDialogProc Function. . . . . .
DoLogoff Function . . . . . . . . . . . .
Updating the Lobby Lists . . . . . . . . . . .

.
.
.
.

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

.
.
.
.

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

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

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

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

.
.
.
.
.

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

.
.
.
.

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

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

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

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

.
.
.
.
.
.

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

.
.
.

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

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

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

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

.
.
.
.
.
.

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

.
.
.

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

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

. 487
. . 487
. . 487
. . 488
. . 488
. . 491
. . 492
. . 492
. . 493
. . 493
. . 494
. . 494
. . 495
. . 495
. . 495
. . 497

. . 497
. . 497
. . 498
. . 498
. . 498
. . 498
. . 503
. . 505
. . 506
. . 506
. . 507
. . 507
. . 507
. . 510
. . 510
. . 511
. . 511
. . 512
. . 512
. . 513
. . 513
. . 514
. . 514


Contents
LobbyRefreshJoinedPlayerList Function
LobbyRefreshGameList Function . . . .
Creating the Lobby Server Application . . . . . . .
PlayerServed_t Structure . . . . . . . . . . .

ArmyServerClass Init Function . . . . . . . .
ArmyServerClass Shutdown Function . . . .
Handling Login Messages . . . . . . . . . . .
Handling Logout Messages . . . . . . . . . .
LogoutPlayer Function . . . . . . . . . .
HandlePlayerSync Function . . . . . . .
HandleGameSync Function . . . . . . .
Creating a Game . . . . . . . . . . . . . . . .
AddGame Function . . . . . . . . . . . .
DestroyGame Function . . . . . . . . .
RemoveGame Function . . . . . . . . .
Joining a Game . . . . . . . . . . . . . . . . .
JoinGame Function . . . . . . . . . . . .
LogOff Game . . . . . . . . . . . . . . . . . .
LogOffGame Function . . . . . . . . . .
Handling Game Messages on the Server . . .
Handling Game Messages on the Client . . .
Handling the Game Host . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.

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

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

.
.
.
.
.
.
.

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

.

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

.
.
.
.

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

.
.
.
.
.
.
.
.
.
.

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

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

.
.
.
.
.
.
.

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

.

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

.
.
.
.

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

.
.
.
.
.
.
.
.
.
.

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

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

.
.
.
.
.
.
.

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

.

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

.
.
.
.

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

.
.
.
.
.
.
.
.
.
.

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

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

.
.
.
.
.
.
.

514
515
516
516
516
517
518
519
520
523
525
527
528
528
529
530
530
530
531
531
532
535

536

Tutorial 5
Creating Your Online Game. . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Game . . . . . . . . . . . . . . . . . . . . . .
Starting a Game. . . . . . . . . . . . . . . . . . . .
StartGame Function . . . . . . . . . . . . . .
Receiving the Start Game Message . . . . . . . . .
Handling Main Application Window Messages . . .
ApplicationProc Function . . . . . . . . . . .
Updating the Game Screen . . . . . . . . . . . . .
DoStartGame Function. . . . . . . . . . . . .
Game Engine . . . . . . . . . . . . . . . . . . . . .
Player Synchronization . . . . . . . . . . . . . . . .
Local Player Movement Variables . . . . . . .
Data Structures . . . . . . . . . . . . . . . . . . . .
Player Data . . . . . . . . . . . . . . . . . . .
Bullet Data . . . . . . . . . . . . . . . . . . .
Creating the Game World . . . . . . . . . . . . . .
ENGINE_Init Function. . . . . . . . . . . . .
ENGINE_GenerateRandomMap Function . .
ENGINE_AddPlayer Function . . . . . . . . .
ENGINE_Shutdown Function . . . . . . . . .
Updating the Game World . . . . . . . . . . . . . .
ENGINE_Render Function . . . . . . . . . .

.
.
.

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

.
.
.
.
.
.
.
.
.

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

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

.
.
.
.
.
.
.

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

. .

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

.
.
.
.

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

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

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

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

.
.
.
.
.
.
.

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

.

. 537
. . 537
. . 538
. . 538
. . 538
. . 538
. . 540
. . 540
. . 542
. . 542
. . 543
. . 543
. . 544
. . 544
. . 544
. . 545
. . 545
. . 545
. . 550
. . 551
. . 551
. . 552
. . 552

xi


Contents

ENGINE_DrawMap Function . . . . . . .
ENGINE_ProcessInput Function . . . . .
ENGINE_UpdatePlayers Function . . . .
ENGINE_UpdateBullets Function . . . .
ENGINE_CheckBulletCollisions Function
ENGINE_CheckFlagCollisions Function .
Handling Player Messages . . . . . . . . . . . .
ENGINE_AddBullet Function . . . . . . .
Game Server . . . . . . . . . . . . . . . . . . .
Running the Server on Unix . . . . . . . . . . .
Main Function . . . . . . . . . . . . . . .
Daemon_init Function . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . .
Appendix A Byte Ordering Functions
htons Function (Unix, Win32) . .
htonl Function (Unix, Win32) . .
ntohs Function (Unix, Win32) . .
ntohl Function (Unix, Win32) . .

.
.
.
.
.

.
.
.
.
.


.
.
.
.

. .
. .
. .
. .
. .

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

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

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

.
.
.

.
.
.
.
.
.
.
.
.
.

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

.
.
.
.
.

.
.
.
.
.
.
.
.

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

.
.
.
.
.
.
.

.
.
.
.
.
.

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

.
.
.
.
.
.
.
.
.

.
.
.
.

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

.
.
.
.
.
.
.
.
.
.
.

.
.

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

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


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

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

.

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

553
555
561
565
566
567
569
571
572
573
573
573
574

. .
. .
. .

. .
. .

.
.
.
.
.

.
.
.
.
.

.
.
.
.

. .
. .
. .
. .
. .

.
.
.
.

.

.
.
.
.
.

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

.
.
.
.
.

.
.
.
.
.

. 575
. . 575
. . 575
. . 575

. . 576

Appendix B NetLib.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585

xii


Introduction
With Internet technology developing rapidly, Internet gaming has become ever
more popular, while documentation on how to actually make Internet games
remains inadequate. Developer’s Guide to Multiplayer Games provides in-depth
coverage of all the major topics associated with online game programming, as
well as giving you easy-to-follow, step-by-step tutorials on how to create your
own fully functional network library, back-end MySQL database, and complete,
working online game!
The book contains two main areas. The first is dedicated to explaining practical
theory on how to utilize MySQL, Perl, sockets, and basic Windows dialog programming. The second section consists of five extensive tutorials, leading you
through the stages of creating a working online game that 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-ROM 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!

xiii



About the Authors
Andrew Mulholland is currently working as lead
programmer at a software development company
in Scotland. He is also an undergraduate at the
University of Abertay in Dundee studying computer games technology. His e-mail address is


Andrew Mulholland

Teijo Hakala is currently studying software engineering at Jyväskylä Polytechnic in Finland and
specializes in network design, programming, and
optimization. He also has a wide variety of work
experience with computer technology. His e-mail
address is

Teijo Hakala

xiv


Theory Introduction
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
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 Perl to create
a back-end database for your game, allowing you to interact with game data
directly from a web browser. Then we 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.
First, let’s look at how to create dialog-based Windows applications…

1



Chapter 1

Creating Windows
Applications in
Visual Studio
Introduction
The most essential knowledge anyone can have is the basics. If you already know
how to create dialog-based Windows applications, skip past this chapter. If not,
this chapter will give you a quick and easy introduction to it so that you will find
the rest of this book more accessible.
The reason for learning this is to be able to create our server applications for
the Microsoft Windows operating system.

Windows Messaging System
Windows controls everything by the use of its messaging system. This is a fundamental idea to grasp if you wish to create any Windows-based applications. Within
this messaging system, 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

3


4


Chapter 1 / Creating Windows Applications in Visual Studio

added to the queue and is then sent to the appropriate window to inform it that the
button has been pressed.
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
multi-tasking 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 Microsoft Visual Studio and select File, New…
The following dialog box should now be 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.


Chapter 1 / Creating Windows Applications in Visual Studio

5


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
appears; simply click OK in this box.
Now we are working with the Visual Studio main interface. Currently,
ClassView is active, but we are interested in FileView so select this tab.
Figure 1-2

FileView lists all of 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 select the Files tab instead
of Projects. The following dialog will be visible.
Figure 1-3

Select C++ Source File as shown in Figure 1-3 and type in the filename as
main.cpp. 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. Click File,


6

Chapter 1 / Creating Windows Applications in Visual Studio

New… again, but this time select 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 ResourceView and allows you to
visually create and edit dialogs for use within your program.
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 Insert Resource dialog box.
Figure 1-5

Select Dialog and click New. 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 that I will explain after the code below.
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.
OK, now it’s time to go back and do some code. We have our dialog template
that we can call from our code so let’s use it. Here is the code required to make
your dialog appear on the screen:
// Simple Windows Code
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK
ClientDlgProc(HWND DialogWindow, UINT Message, WPARAM wParam, LPARAM lParam)
{
// Process Messages



Chapter 1 / Creating Windows Applications in Visual Studio

7

switch(Message)
{
case WM_INITDIALOG:
return FALSE;
case WM_COMMAND:
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;
}


The OK button on the dialog can be pressed but will have no action, whereas the
Cancel button will close the dialog.
NOTE If you get an error and it 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 code above may look complex
and a little confusing. Welcome to the world of Windows! 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 callback procedure
(ClientDlgProc) 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


8

Chapter 1 / Creating Windows Applications in Visual Studio

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). 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

As well as being useful for debugging, being able to update information to a window is essential knowledge that 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 below 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 shown in Figure 1-6. Now double-click on the text you added to
the dialog box to display its properties.
Figure 1-6

The dialog box shown in Figure 1-7 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.


Chapter 1 / Creating Windows Applications in Visual Studio

9

Figure 1-7

Now we have some text, and 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 the following lines of code after the line that contains “case WM_INITDIALOG”:
SendDlgItemMessage(DialogWindow,IDC_SERVERSTATUS, WM_SETTEXT,NULL,(long)"Server
Online");

Therefore, 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 parameter is
not used for the WM_SETTEXT command so we simply set it to NULL. The
final parameter is used to declare the string with which we want to update the
static text, in this case “Server Online.” Also, note that the string must be typecast to a long.
TIP

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

Introduction to 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 will make it easier to
reuse the code for future projects. In addition to the reusability factor, static link
libraries also protect your source code, but allow others to use the functionality of
your code.
When creating a static link library, you do not require a WinMain function or a
Windows update function (WndProc). This is because we are not actually creating


10

Chapter 1 / Creating Windows Applications in Visual Studio

a program, 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 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 in the tutorial to follow.

Creating a Static Link Library
First, the best thing to do is to create a directory structure on your hard drive for
the library to be stored in. From our experience, we recommend the structure
shown in Figure 1-8.
Figure 1-8

n

n
n

n

n
n

Examples All example programs that display how to use your library are
stored in this folder. This is probably one of the most useful things that can
accompany your library since the source code is not visible to any other programmer using it.
Formats This is where you store any file formats specific to your static link
library (i.e., your own 3D model format).
Include This stores the entire collection of C/C++ header files that are

needed to use your library. This is one of the directories that you must set up
in Visual Studio to make your library work.
Lib This is where you actually store your complete library file and the other
directory required by Visual Studio. It is a good idea to include both the
release and debug versions of your library here so that it is possible to debug
and create final release applications with the libraries.
Source All source code related to your library must be kept in this folder.
Tools Any programming tools that are used alongside your library are stored
here (such as file format converters).


×