2D online game development (network) (3)

xiaoxiao2021-04-05  279

2D online game development (network) (3)

Author: akinggw

In the previous chapter, we have explained what Raknet is, how to configure Raknet in the DC and tes our first programs.

In this article, we will explain the Raknet function and will write our first procedure.

Because Raknet is based on

Berkeley Sockets

with

Winsock

Developed, so it supports

Windows

System and

Linux

system. You can run on a local area network, the Internet.

Most of today's game support two modes of network connections: peer mode and customer service

/

Server mode. In fact, in today's online casual games, these two modes are supported.

Raknet supports the above two modes.

On the network, our transmission information is generally relying on TCP / IP protocol, while the protocols transmitted in the TCP / IP protocol also include TCP and UDP.

What is TCP mean? It refers to the connected virtual circuit protocol. That is, it is necessary to establish a connection with the user before sending data, and keep and user connections, then send data, and constantly ask if the user receives the correct data, if it is not correct, it is reissued until it is correct.

What does UDP refer to? It refers to the user's datagram protocol, which has been connected before sending data, and then connects to the user, and the connection is established, and does not keep the connection and the user's connection, then send data, or whether the other party receives the data, then close the connection.

It can be seen from the above description that TCP is a relatively reliable connection method, but it is not suitable for the game. You can think about it, if a player and server establish a TCP connection, then wait until this user is disconnected and the server is connected, other users can use the server. This is obviously not possible.

UDP can communicate with multiple users. For example, a player wants his personal information, then he issues a request to the server, the server answers him with UDP and turn off his connection; the server can then handle other players.

We use three headers in this case:

RakClientInterface.h

RaknetworkFactory.h

RakserverInterface.h

The first header file contains the information required to establish the customer service, including the establishment, connection, and data transmission and reception of the customer service.

The second header file is used to manage the classes we used in the program, including the release of class memory allocation, and class memory.

The third header file is used to establish the information required for the server, including the establishment, connection, and data transmission and reception of the server.

Char Str [512];

RakClientInterface * rakclientInterface;

RakserverInterface * rakserverinterface;

STR [512] is used to determine that we are to build a server or a client. Next, a client instance and a server instance are declared.

Printf ("(c) Customer Service (S) Server? N");

Gets (STR);

IF (STR [0] == 'c')

{

RakClientInter>

RakserverInter>

Printf ("The customer service has been established.");

}

Else

{

RakClientInter>

RakserverInter>

Printf ("Server has been established.");

}

Get an input value. If the input value is c, establish the client, then set the server instance to empty, if the input value is other, set the server, and then set the client instance to empty. RaknetworkFactory :: getrakclientinterface (); initialize a client instance to assign memory for it;

RakNetworkFactory :: getrakserverinterface (); initialize a server instance, assign memory for it;

Finally, the program is executed, and we need to release the memory we just allocated.

IF (RakClientInterface)

RaknetworkFactory :: destroyrakclientinterface (rakclientinterface);

Else IF (Rak ServerInterface)

RaknetWorkfactory :: DestroyrakServerInterface (RakserverInterface);

The complete program code is as follows:

#include "stdio.h"

#include "conoe.h"

#include "Raknet / RakClientInterface.h"

#include "raknet / raknetworkfactory.h"

#include "raknet / rakserverinterface.h"

Int main (void)

{

Char Str [512];

RakClientInterface * rakclientInterface;

RakserverInterface * rakserverinterface;

Printf ("

(C) Customer Service (S) Server? N ");

Gets (STR);

IF (STR [0] == 'c')

{

RakClientInter>

RakserverInter>

Printf ("The customer service has been established.");

}

Else

{

RakClientInter>

RakserverInter>

Printf ("Server has been established.");

}

// Todo - Add Code Body Here

Getch ();

IF (RakClientInterface)

RaknetworkFactory :: destroyrakclientinterface (rakclientinterface);

Else IF (Rak ServerInterface)

RaknetWorkfactory :: DestroyrakServerInterface (RakserverInterface);

Return 0;

}

The execution result is as shown in the figure, you can build a client or server:

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.

转载请注明原文地址:https://www.9cbs.com/read-132155.html

New Post(0)