The Moosader Community

Visit the IRC at irc.freenode.net #Moosader! Community dedicated to programming and game development! Moosader.com
It is currently Tue Dec 10, 2019 8:57 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 13 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Fri Dec 10, 2010 1:33 am 
Offline
Smushed Goomba
Smushed Goomba

Joined: Mon Mar 22, 2010 4:14 am
Posts: 7
This is the 1st part of probably many, on the topic of Game Networking.

Who Am I? I am a 19 year old C++ programmer who has been writing C++ for about 5 years, I have done lots and lots of game networking for MMORPGs, specializing in the creation of server/client architectures. I will explain in hopefully easy to understand ways, all about what goes into networking, and socket programming, giving you the best ways in which to create a high-performance server/client architecture.

This subject can go by many names, networking, network programming, socket programming, internet programming, among others. For the course of this series, I will refer to it as socket programming.
In order to best understand how we can design and implement a network architecture into a game, we must first learn how networks actually work. First let us create a definition for the word Network.

Code:
Network: A hub of interconnected computers, in which each computer has the ability to talk to any other computer


The key thing about a network is the fact that they are connected, this gives the computers the ability to communicate in a universal way. What I mean by that is, the computers never have to change mediums of communication regardless of the data being sent. To give an example of a network would be really easy. You are reading this over a network, you are on the internet of course; and all the internet is, is a very large network.
Now that we know what a Network is, let us begin to examine how and what happens when computers “talk” to one another. We must now define a few new words.

Code:
Protocol: A way in which computers communicate; a set of “rules” of how the communication takes place.

Code:
Packet: A “holding” cell of information

Code:
Port: A medium in which computers pass packets through.


Now that we defined those three new words, let’s talk about each on a little more in-depth.

Protocols act as a rule set in which computers must follow to pass information between each other, a good example of this, one in which everybody knows even if they don’t realize it, is the http protocol. The http protocol pretty much states how the computers will communicate, meaning what kind of information they can send and receive.

Packets are what hold this information that gets sent or received from various places. They are quite often comprised of various things, from different types of headers, and data, to maybe a packet that holds multiple headers.

The port is then the medium in which the packet is sent through, what port is used is usually determined by the protocol in use. So meaning, instead of having all the information pass in and out in one way, we can send information through various numbered ways. This helps prevent bottlenecks, and keeps everything separate, so you don’t have data crashing into each other as it comes into your computer.


Top
 Profile  
 
PostPosted: Fri Dec 10, 2010 3:21 am 
Offline
Smushed Goomba
Smushed Goomba

Joined: Mon Mar 22, 2010 4:14 am
Posts: 7
Ok now that we know what the basics of a network, we can begin to talk about how it plays into game networking. In order to begin discussing all of it, we first must define a new word.

Code:
Socket: A medium that we pass entire packets through, contains at least an IP Address and port


Sockets are the integral part of…you guessed it, socket programming, after all they are in the name! Sockets provide a means of transmission for us, as they allow us to connect to other computers, and have a way of listening for other computers to connect to us. Sockets contain at the very least an IP address that we connect to or are listening on, and a port in which we know where to exactly send the data we want. There are two main protocols which are very different for sockets, which we will define and then go into detail.

Code:
TCP: Transmission Control Protocol, one of the main rule sets that we can use to send packets through sockets.


TCP is known as a connection based protocol, meaning for every socket we have, we must connect to a listener socket; which is waiting for us. Now this is good because this allows us to keep track of all the stuff we are sending and receiving, as well as for the server to keep track of all its clients. This is the most commonly used protocol for game networking, the other is used at times, but this is the main one. This is for a few reasons.

Code:
IMPORTANT INFORMATION!
1) TCP uses a connection based system, allowing programmers to easily track and use the various connected sockets.
2) TCP’s rule set states that ALL packets will arrive IN ORDER and COMPLETE
3) TCP allows easy sending and receiving of data


These may make it seem like TCP is the greatest thing, and that there shouldn’t be a second protocol, that TCP handles all the business. Well it does…for a price; you see TCP checks everything to make sure that it is all in order and complete, and if it isn’t…well…then it has to request a new packet. All of this costs lots of time and performance, so a much faster protocol was created to deal with some of these problems.
The second socket protocol is defined as follows:

Code:
 UDP: User Datagram Protocol, a connectionless protocol, utilizing speed and performance to deliver packets.


Yes you read it right, UDP uses a connectionless approach. What this means is, instead of connecting to a client or server; we automatically assume we will have access, and simply just send the information along.
UDP uses a type of packet that we will define.

Code:
Datagram:  a packet send from an “unreliable” source


You are probably asking, “what do you mean unreliable?” well it is just that, the reason is in the word. UDP is unreliable, meaning that packets cannot be guaranteed to get there complete, or even at all. As sometimes we may lose datagram, or it may arrive incomplete. So why use it at all if it is unreliable? The answer is simple…performance.
You see, since we are not doing all those costly checks to make sure that the packet is complete, or if it arrived at all, or if it even arrived in order, we gain something…Performance. This is the reason why people use the UDP protocol. So let us go over some of the information that is here.

Code:
IMPORTANT INFORMATION!
1) UDP uses datagrams, giving it a connectionless transmission layer
2) UDP is considered “unreliable” due to there being no checks on datagrams
3) UDP has better performance than the TCP protocol, because it is “unreliable”


Here is a small comparison between the two protocols

Code:
Protocol                       TCP                               UDP
Connection Type        Connection based                   Connection-less
Reliability            Packets arrive complete         Packets may not arrive
Ordering                     As sent                   May arrive not in order
Weight                     Heavyweight                       Lightweight
Packet Type               Byte Streams                       Datagrams


Top
 Profile  
 
PostPosted: Fri Dec 10, 2010 4:47 am 
Offline
Smushed Goomba
Smushed Goomba

Joined: Mon Mar 22, 2010 4:14 am
Posts: 7
Now that we understand the basics of what a network is, what sockets are, and we have information about two of the most popular transmission protocols (TCP and UDP). We can now begin to take a look at a basic implementation of Sockets.

Code:
ADVANCED WARNING
I will ONLY be covering Win32 socket stuff, much later in the series I will go over some multi-platform libraries like Boost, but until then, I will only talk about Winsock

Winsock comes in two flavors, the 1.1 edition, and the 2.0 edition. For this series, we will use the 2.0 edition. Not only because it is newer, but it also supports newer protocols, and is simpler to use.
This part will outline some of the high level functions that we will be using in the creation of our network structure.
Starting off, we need to know what file we need to include so we can use all this socket goodness.
Code:
#include <winsock2.h>

there we are, as simple as that.

In Winsock we have a struct called WSAData, that is our basic implementation of the Winsock architecture.

WSAData looks like this:


Code:
typedef struct WSAData {
        WORD                    wVersion;
        WORD                    wHighVersion;
#ifdef _WIN64
        unsigned short          iMaxSockets;
        unsigned short          iMaxUdpDg;
        char FAR *              lpVendorInfo;
        char                    szDescription[WSADESCRIPTION_LEN+1];
        char                    szSystemStatus[WSASYS_STATUS_LEN+1];
#else
        char                    szDescription[WSADESCRIPTION_LEN+1];
        char                    szSystemStatus[WSASYS_STATUS_LEN+1];
        unsigned short          iMaxSockets;
        unsigned short          iMaxUdpDg;
        char FAR *              lpVendorInfo;
#endif


Most of this you don’t need to worry about at all, and I have only shown it here, to show you what it looks like.
In the beginning of our program, we will want to create an object of type WSAData, for those who better understand by seeing it means this.
Code:
WSAData data;
where the variable data can obviously be anything you want it to be.
After we make our object, we will want to make a call to Winsock’s startup function.
Code:
 WSAStartup(__in WORD wVersionRequested, __out LPWSADATA lpWSAData )


Now don’t let that confusing looking argument list get to you, it is actually really simple.
The first argument it asks for is simply the version of winsock you want to use, it could be 1.1, 2.0, or the even newer 2.2! The second argument simply asks for our WSAData that we created the line before. An example of this call would be.
Code:
WSAStartup(MAKEWORD(2,0), &data)


What is that MAKEWORD!? Relax, all it does is turn the two numbers we give it into a WORD datatype. Also im sure you noticed the big & infront of data, meaning yes we must pass the WSAData object in by reference! If you do it any other way, you WILL get compile errors

Now that we know that, lets dive in just a bit further with WSAStartup, you see it has a return type of int, meaning it will give us a number based on what has happened. WSAStartup will ALWAYS return a value of 0 (zero) on a successful startup, it will return a different value based on the error. For now all you need to worry about is if it returned a 0 or not.
So let’s see everything in action!
Code:
#include <winsock2.h>
#include <iostream>
int main()
{
WSAData data;

if(WSAStartup(MAKEWORD(2,0), &data) == 0)
   std::cout << “Winsock started successfully!”
else
{
   std::cout << “Winsock failed to start!”
   return 0;
}
WSACleanup();
}


oh? You saw that WSACleanup() eh? Well its job shouldn’t be a mystery, after all it is in the name.
Code:
IMPORTANT INFORMATION
You MUST call WSACleanup() at the end of your program if you are using Winsock

We will begin discussing the SOCKET data type in part 4, along with the basics of sending and receiving information.


Top
 Profile  
 
PostPosted: Fri Dec 10, 2010 11:33 am 
Offline
Niccolo
Niccolo
User avatar

Joined: Thu May 06, 2010 3:20 pm
Posts: 326
Just a suggestion, this should be 3 posts in a single topic rather than 3 topics.

_________________
Please note that any and all of this user's opinions and/or data may or may not be complete and utter bullshit.
Ducky wrote:
Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.


Top
 Profile  
 
PostPosted: Fri Dec 10, 2010 3:48 pm 
Offline
Smushed Goomba
Smushed Goomba

Joined: Mon Mar 22, 2010 4:14 am
Posts: 7
sure, when i see moosader, ill let her know to do that


Top
 Profile  
 
PostPosted: Sun Dec 19, 2010 4:59 pm 
Offline
Goblin
Goblin

Joined: Tue May 04, 2010 2:01 pm
Posts: 112
*waits patiently for the good parts* ... :D Can't wait for a synchronization tutorial, or something like that... and maybe some tip/tricks.


Top
 Profile  
 
PostPosted: Sun Dec 19, 2010 6:04 pm 
Offline
Eater of Comics
Eater of Comics
User avatar

Joined: Wed Mar 31, 2010 10:06 pm
Posts: 1015
adikid89 wrote:
*waits patiently for the good parts* ... :D Can't wait for a synchronization tutorial, or something like that... and maybe some tip/tricks.

/me be waiting for a continuation as well, didn't want to say anything because it was progressing at a great rate, and I didn't want to impose....

_________________
mv2112 wrote:
O.o

BlackPhantom wrote:
"And the lord said let there be videos! And he saw it was awesome." -Awsome 1:12


Top
 Profile  
 
PostPosted: Tue Dec 21, 2010 7:16 pm 
Offline
Fighter
Fighter
User avatar

Joined: Mon Dec 28, 2009 1:00 am
Posts: 198
Location: Glen Burnie, Maryland
I merged the three topics for you, CMB.Thank you so much for posting all of this, can't wait for more! :D

_________________
"Come on, lets save the world Its not like you to turn your back."


Top
 Profile  
 
PostPosted: Sun Dec 26, 2010 2:26 am 
Offline
Smushed Goomba
Smushed Goomba

Joined: Mon Mar 22, 2010 4:14 am
Posts: 7
There IS more coming :) i know i have been a little slow with the continuation of it, but ive been rather busy since its the holidays :) but I GUARANTEE YOU that more IS ON ITS WAY :D


Top
 Profile  
 
PostPosted: Sun Dec 26, 2010 5:43 am 
Offline
Goblin
Goblin

Joined: Tue May 04, 2010 2:01 pm
Posts: 112
CMB wrote:
There IS more coming :) i know i have been a little slow with the continuation of it, but ive been rather busy since its the holidays :) but I GUARANTEE YOU that more IS ON ITS WAY :D

Hooray! :D . And thanks for putting time and effort into making these. I really appreciate it!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron