2D online game development (network) (5)
Author: akinggw
In the fourth article, we have learned how to use Raknet to connect, in this article, we will explain how to make clients and servers communicate, such as chatting.
Ok, in fact, I already know that you can't wait, let us start.
The program code and "2D online game development (network) (4)" are just, but we need to add some content therein.
Before entering the loop, we need to define an information variable to store the information we will send.
Char Message [2048];
Enter the cycle, before the start of the cycle, it is to add the following code before receiving the information:
IF (kbhit ())
{
Gets (Message);
IF (RakserverInterface)
{
RakserverInterface-> Send (message, (const Int) Strlen (Message) 1, high_priority, reliable_pered, 0, unassigned_player_id, true);
}
Else
{
RakClientInterface-> send (Message, (int) Strlen (Message) 1, high_priority, reliable_ord, 0);
}
}
Ok, let's analyze the above code.
KBHIT () is used to monitor if there is a button, if not, get information, save it in Message, and monitor the server or client.
If it is a server, use the server to send the data, the client sends data with the client.
Let's take a specific analysis of the servers' send () function:
The first parameter pointing to the data you want to send;
The second parameter, the size of the data you sent, maybe you will ask, why do you add one 1? That's because our data is sent according to the data stream. If you don't leave spaces between data, the network engine cannot distinguish data, so we have to add a space after each data. It is also equivalent to cutting a line into a cut.
The third parameter is used to set the importance of you sending data, a total of three parameters:
HIGH_PRIORITYMEDIUM_PRIORITYLOW_PRIORITY
They are highly important, medium, and bottom. When the network engine is sending data, first queue data, according to his importance, the importance is high, otherwise it will be sent.
Fourth parameter, reliability, one has five parameters:
Unreliable - 5, 1, 6Unreliable_sequenced - 5reliable - 5, 1, 4, 6, 2, 3reliable_ordred - 1, 2, 3, 4, 5, 6reliable_sequenced - 5, 6
What is the reliability represent? That is, when the network engine sends data, if you select reliable transmission data, then the data will arrive according to the correct step, and if the choice is unreliable, the data may be continuously reached.
Usually used
Reliable - 5, 1, 4, 6, 2, 3
with
Reliable_Ordered - 1, 2, 3, 4, 5, 6
.
The fifth parameters are usually selected to be 0, not very important.
Sixth parameters, receiver ID, direct use
Unassigned_player_id is set.
Seventh parameters, whether broadcasting, there are two parameters
0
with
1
,
0
Indicates no broadcast,
1
Indicates all clients that send this information to and server connections.
The Send function of RakClientInterface is the same, just the last two.
Finally, we have to modify the following:
in
DEFAULT:
Below, the program is modified as follows:
Printf ("% SN", Packet-> DATA);
IF (RakserverInterface)
{
Sprintf (Message, "% S", Packet-> DATA);
RakserverInterface-> Send (message, (const Int) Strlen (Message) 1, high_priority, reliable_pered, 0, unassigned_player_id, true);
}
The first statement is to print the data we have received;
If we build the server, then the data we receive is converted into a char format, (because the packet-> data of the packet-> DATA is unsigned char), then broadcast this data to all the clients in the server.
Here, our procedure is completed, the operation is as follows:
server:
Figure 1
Client 1:
Figure 2
Client 2
Figure 3
OK, today's content is here, I wish you a happy weekend and have fun!
For more information, please visit the Golden Bridge Science website (http://popul.jqcq.com) Game development section, such as you need a book development, please refer to Jinqiao Book City Game Channel (http://book.jqcq.com/category / 1_70_740.html). If you have any good suggestions when you read this article, please let me, my e_mail: akinggw@126.com. If you have any questions when using SDL, please go to the Jinqiao Scot website (http: // popul. JQCQ.com) Game Development section, I will answer your questions in detail.