![]() |
In this tutorial all images and interface references are for Visual Studio 2005, copyrighted by Microsoft. Use the equivalents for your own compiler.
|
![]() |
1. Design Lets make the chat server as basic as possible to begin with. It will have two main modes: server and client. The server will relay client messages. The client will send chat messages. Both of them will display chat messages as they arrive. We'll hardcode most of the input variables so we don't clutter the code with non-networking stuff. This could be done by writing the chat data directly to a packet. However, we'll use RPC calls to avoid any chance of chat characters being mistaken for system messages. |
2. First compile
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. This requires 2 of the files you copied earlier: -RakNetworkFactory which creates peer instances. -RakPeerInterface, which encompasses everything you might want to do with a peer Try writing it on your own first. When you are done, Display code sample 1 Hit F7 or the equivalent to build. It should build successfully at this point. If it doesn't, refer to the FAQ 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 forum. |
3. Adding functionality
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. 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. 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. 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. 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 MessageIdentifiers.h. Go look at that now. 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:
Display code sample 2 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 FAQ or post in the forum. This is the output from my version of the sample: Server output (C)lient or (S)erver? s Starting the server. A connection is incoming. Client output (C)lient or (S)erver? c Enter server IP or hit enter for 127.0.0.1 127.0.0.1 Starting the client. Our connection request has been accepted. We are now ready to send user input. As specified early on, we will use remote procedure calls for this. Read that page to learn how to implement them and what the parameters mean. You can also refer to the sample at \Samples\Remote Procedure Calls. Lets make an RPC function called "PrintMessage". All it does is print "Hello World". We'll deal with user input later. Your next programming steps are:
When you are done,
Display code sample 4 Did you remember to multiply the number of bytes by 8 to pass the number of bits? If so, congratulations, you are more careful than I am, who didn't read the documentation and wasted several minutes figuring out why only garbage appeared. As per our original design, we want the server to relay messages. When the server gets the string, it should send the string back to the client. You have enough information to do this already so give it a shot. (Hint) The PrintMessage function needs access to the server. (Hint) If it isn't apparent what the systemAddress sender field is for, refer to the section on SystemAddresses. When done, Display code sample 5 This should be the output. It's the same as before, except the client now also shows the message. Client Output: (C)lient or (S)erver? c Enter server IP or hit enter for 127.0.0.1 127.0.0.1 Starting the client. Our connection request has been accepted. Enter a string to show on the server: This was typed on the client. This was typed on the client. Server Output: (C)lient or (S)erver? s Starting the server. A connection is incoming. This was typed on the client. Almost done. In practice, we don't want the person typing to see their own messages. Also, we should provide a mechanism where you can type anytime, not just when you first connect. Try it out, then Display final code sample 6 You'll have to run three instances of the program: 1 server and 2 clients to see anything happen. While gets() is blocking no network messages will be parsed so you might have to hit enter to bypass it to see other messages. In a real application you would use a nonblocking input function. The included samples use kbhit() to detect keyboard input. Some additional things you can try out are:
|
![]() |
Index BitStreams Creating Packets FAQ Network Messages NetworkIDObject.h SystemAddress Receiving Packets Remote Procedure calls Sending Packets |