Files
SLikeNet/Help/RakNet/documentation/tutorial.html
2025-11-24 14:19:51 +05:30

149 lines
9.2 KiB
HTML

<HTML>
<HEAD>
<TITLE>RakNet Tutorial</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></HEAD>
<link href="RaknetManual.css" rel="stylesheet" type="text/css">
<meta name="title" content="RakNet - Advanced multiplayer game networking API">
</HEAD>
<BODY BGCOLOR="#ffffff" LINK="#003399" vlink="#003399" alink="#003399" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0"">
<img src="RakNet_Icon_Final-copy.jpg" alt="Oculus VR, Inc." width="150" height="150"><BR>
<BR>
<table width="100%" border="0"><tr><td bgcolor="#2c5d92" class="RakNetWhiteHeader">
<img src="spacer.gif" width="8" height="1">Tutorial Project setup</td>
</tr></table>
<TABLE BORDER="0" CELLPADDING="10" CELLSPACING="0" WIDTH="100%"><TR>
<TD>
In this tutorial all images and interface references are for Visual Studio 2005, copyrighted by Microsoft. Use the equivalents for your own compiler.<BR>
<OL>
<LI>Create a new Win32 Console Project and name it ChatServer<BR>
<img src="tutorial1.jpg" width="804" height="542"><BR><BR>
<LI>Make it a console application, and an empty project. Windows application is also acceptable.<BR>
<img src="tutorial2.jpg" width="625" height="443"><BR><BR>
<LI>Add the RakNet source files to your project. You can do this by right clicking on the project name, selecting Add, and then Existing Item from the rollout. Navigate to where you downloaded RakNet, navigate to the Source directory, hold down left shift, select the first file in the list, then click the last file in the list. Then press OK.<br>
<img src="tutorial3.jpg" width="494" height="262"><BR><br>
<LI>Add a main file to your project. Do this by
right clicking on the project name, selecting Add, and then
new item. Under Visual C++ select code, then c++ file. Name the file main.cpp.<BR><img src="tutorial4.jpg" width="480" height="194"><BR><BR>
<LI>Since we put files in a directory other than the project, we need to include that directory in the include search path so we don't have to type out the entire path every time we #include something. Do this by right clicking the "Chat Server" project name in the project tab. Select properties. In the top pane, change the drop down menu to "All Configurations." Select C/C++ / General / Additional Include Directories. To that item, add the path to the Source directory of your RakNet download, and hit OK.
<br>
<img src="tutorial5.jpg" width="759" height="531"><br>
<br>
<LI>Next, link in ws2_32.lib, from the same dialog box.<br>
<img src="tutorial6.jpg" width="759" height="531"><br>
<br>
<LI>Also change the character set to not set. This is so regular array based strings are used, as opposed to unicode or wide character.
<img src="tutorial7.jpg" width="759" height="531"><BR><BR>
<LI>You are ready to begin adding code to main.cpp</OL>
</TD></TR></TABLE>
<table width="100%" border="0"><tr><td bgcolor="#2c5d92" class="RakNetWhiteHeader">
<img src="spacer.gif" width="8" height="1">Tutorial Code implementation</td>
</tr></table>
<TABLE BORDER="0" CELLPADDING="10" CELLSPACING="0" WIDTH="100%"><TR><TD>
1. Design
</p>
Lets make the chat server as basic as possible to begin with. It will have two main modes: server and client. The server will receive a client message. The client will send a message on startup. We'll hardcode most of the input variables so we don't clutter the code with non-networking stuff.
</TD></TR></TABLE>
<TABLE BORDER="0" CELLPADDING="10" CELLSPACING="0" WIDTH="100%"><TR>
<TD>
2. First compile
</p>
Create your main function. Query the user as to whether they want to run a client or server. Create the peer instance, and call Startup with the appropriate parameters for a Server or Client. Destroy the peer at the end.
<BR>Try writing it on your own first. When you are done,<BR>
<B><A HREF="tutorialsample1.html" target="_blank">Display code sample 1</A></B><BR>
<BR>
Hit F7 or the equivalent to build. It should build successfully at this point. If it doesn't, refer to the <A HREF="faq.html">FAQ</A> which gives many reasons for why something won't build and how to fix it. If that doesn't answer your question, post a question in the <A HREF="http://www.jenkinssoftware.com/forum">forum</A>.
</TD></TR></TABLE>
<TABLE BORDER="0" CELLPADDING="10" CELLSPACING="0" WIDTH="100%"><TR>
<TD>
3. Adding functionality
</p>
Now that we have a client and server instantiated, we need to know what it can do. The best way to find out is to go to the source: RakPeerInterface.h. It contains all the functions for the class, plus detailed comments on each function. See the comments for the Startup and Connect functions. You should also take a look at SetMaximumIncomingConnections. <BR>
<BR>
In the code, after the server was created, add code to start the server. That takes certain parameters - set whatever you wish, based on the description provided in the comments.<BR><BR>
Do something similar with the client. After the code where it is created, add code to connect it. It takes an IP - add code to read an IP. For the server port, either put code to read the port, or hardcode the server port you entered above. For the client port, either put code to read it, or put 0 to automatically choose.<BR><BR>
This is all you need to do to start a server or connect a client. To determine if the connection was successful, we need to be able to read messages from the network system. In RakPeerInterface.h you'll find a Receive function. This function returns a "Packet" structure, which is defined in RakNetTypes.h. It encapsulates one message and is quite simple. Go look at that now.<BR>
<BR>
As you can see from the "char *data" member, all packets contain an array of bytes. These bytes can be anything you want. The length of the array is indicated by the length and bitSize fields. The convention RakNet uses is the first byte is always an identifier that tells you what the rest of the data is. These identifiers are defined in <A HREF="networkmessages.html">MessageIdentifiers.h</A>. Go look at that now.<BR>
<BR>
You'll see there are quite a few pre-defined enumerations. You should quickly read the comments on each of them. We only care about the connectivity enumerations for now. So your next programming step is as follows:
<OL>
<LI>Create a loop for the main body of your program.
<LI>In that loop, call Receive and store the pointer returned in a pointer variable of type Packet.
<LI>If the packet variable is not 0 (which means no packets to read), check the first byte of Packet::data. See which of the connectivity related enumerations this byte matches (a switch/case would be handy here).
<LI>Print out the comment that goes along with that enumeration.
<LI>As specified in the comments, when you are done with the Packet pointer deallocate it by passing it to the DeallocatePacket method.
</OL>
Try writing it on your own first. When you are done,<BR>
<B><A HREF="tutorialsample2.html" target="_blank">Display code sample 2</A></B><BR>
<BR>
At this point you should be able to run two instances (In Visual Studio, hit ctrl-F5 twice) and connect to each other. If you cannot connect, then refer to the <A HREF="faq.html">FAQ</A> or post in the <A HREF="http://www.jenkinssoftware.com/forum">forum</A>.
<BR>
This is the output from my version of the sample:<BR>
<BR><B>Server output</B><BR>
(C)lient or (S)erver?<BR>
s<BR>
Starting the server.<BR>
A connection is incoming.<BR>
<BR>
<BR><B>Client output</B><BR>
(C)lient or (S)erver?<BR>
c<BR>
Enter server IP or hit enter for 127.0.0.1<BR>
127.0.0.1<BR>
Starting the client.<BR>
Our connection request has been accepted.<BR>
<BR>
We are now ready to send input.<BR>
Your next programming steps are:
<OL><LI>Add a user defined enumeration to send as the first byte of your game messages.
<LI>When the client successfully connects, send a string using RakString(). Read it on the server, and print it out.
</OL>
<p>When you are done,<BR>
<B><A HREF="tutorialsample3.html" target="_blank">Display code sample 3</A></B><BR>
<BR>
The client output should be similar to before. The server output should also print "Hello World"<BR>
<BR>
(C)lient or (S)erver?<BR>
s<BR>
Starting the server.
<BR>
A connection is incoming.<br>
Hello World<BR>
<BR>
See the index for a list of major systems not covered here. For further information on setting up your project, see the next page: <A HREF="compilersetup.html">Compiler setup</A>
</TD></TR></TABLE>
<table width="100%" border="0"><tr><td bgcolor="#2c5d92" class="RakNetWhiteHeader">
<img src="spacer.gif" width="8" height="1">See Also</td>
</tr></table>
<TABLE BORDER="0" CELLPADDING="10" CELLSPACING="0" WIDTH="100%"><TR><TD>
<A HREF="index.html">Index</A><BR>
<A HREF="bitstreams.html">BitStreams</A><BR>
<A HREF="creatingpackets.html">Creating Packets</A><BR>
<A HREF="faq.html">FAQ</A><BR>
<A HREF="networkmessages.html">Network Messages</A><BR>
<A HREF="networkidobject.html">NetworkIDObject.h</A><BR>
<A HREF="systemaddresses.html">SystemAddress</A><BR>
<A HREF="receivingpackets.html">Receiving Packets</A><BR>
<A HREF="remoteprocedurecalls.html">Remote Procedure calls</A><BR>
<A HREF="sendingpackets.html">Sending Packets</A><BR><BR>
</TD></TR></TABLE>
</BODY>
</HTML>